Package: interactions-wait-for
interactions-wait-forExtension for interactions.py which implementswait_forInstallationpip install -U interactions-wait-forwait_forBenefitsAn actualwait_forAsynchronous checksTimeoutsDoesn't overwrite any library codeSo what is this so-calledwait_for?wait_foris an awaitable future that waits for a specific event, and returns the result.Use cases:Waiting for an interaction or messageContinue commands after responseUnlike events:You keep data from your slash commandYou can listen for a response with a timer and a checkYou can do stuff when timed outOkay, but how do I use it?You import thewait_forlibrary like this:frominteractions.extimportwait_forHere is an example code which shows you how to wait for a message, with an asynchronous check and a timeout:frominteractionsimportClient,Messagefrominteractions.ext.wait_forimportwait_for,setupimportasynciobot=Client(token="...")# apply hooks to the classsetup(bot)@bot.command(name="test",description="this is just a test command.")asyncdeftest(ctx):awaitctx.send("grabbing a message...")# A simple example check function.# Returns True if the original author is the same as the user invoking the wait_for.# Returns False if another member is attempting to invoke the wait_forasyncdefcheck(msg):ifint(msg.author.id)==int(ctx.author.user.id):returnTrueawaitctx.send("I wasn't asking you")returnFalsetry:# Define the wait_for.# This particular example listens for the raw on_message_create event which then returns a Message object.# With this, you have the ability to read the content (if the privileged intent has been# approved in the Discord Dev dashboard), any attachments, stickers, etc.msg:Message=awaitwait_for(bot,"on_message_create",check=check,timeout=15)# Afterwards, here you can put your code to execute after the wait_for has been fulfilled,# the checks have passed, and the timeout has not been reached.exceptasyncio.TimeoutError:# If your specified timeout reaches its end, here you may add your code for that condition.returnawaitctx.send("You said nothing :(")bot.start()wait_for_componentWhat's the difference betweenwait_forandwait_for_component?While you could wait for a component click withwait_for,wait_for_componentis designed specifically to get a response from any one of many components that you can pass through as a list. You can also add messages to thewait_for_componentso that it will check if the component clicked is in any one of the messages specified.Okay, but how do I use it?Here is an example code which shows you how to wait for a message, with an asynchronous check and a timeout:frominteractionsimportClient,ComponentContext,Buttonfrominteractions.ext.wait_forimportsetupimportasynciobot=Client(token="...")# apply hooks to the classsetup(bot)@bot.command(name="test",description="this is just a test command.")asyncdeftest(ctx):button=Button(style=1,label="testing",custom_id="testing")awaitctx.send("grabbing a click...",components=button)asyncdefcheck(button_ctx):ifint(button_ctx.author.user.id)==int(ctx.author.user.id):returnTrueawaitctx.send("I wasn't asking you!",ephemeral=True)returnFalsetry:# Like before, this wait_for listens for a certain event, but is made specifically for components.# Although, this returns a new Context, independent of the original context.button_ctx:ComponentContext=awaitbot.wait_for_component(components=button,check=check,timeout=15)# With this new Context, you're able to send a new response.awaitbutton_ctx.send("You clicked it!")exceptasyncio.TimeoutError:# When it times out, edit the original message and remove the button(s)returnawaitctx.edit(components=[])bot.start()asyncwait_forWaits for an event once, and returns the result.Unlike event decorators, this is not persistent, and can be used to only proceed in a command once an event happens.Arguments*name: str: The event to wait for?check: Callable[..., bool]: A function or coroutine to call, which should return a truthy value if the data should be returned?timeout: float: How long to wait for the event before raising an errorReturnsThe value of the dispatched eventRaisesasyncio.TimeoutErrorasyncwait_for_componentWaits for a component to be interacted with, and returns the resulting context.Argumentscomponents: str | Button | SelectMenu | list[str | Button | SelectMenu]: The component(s) to wait formessages: int | Message | list[int | Message]: The message(s) to check for?check: Callable[..., bool]: A function or coroutine to call, which should return a truthy value if the data should be returned?timeout: float: How long to wait for the event before raising an errorReturnsTheComponentContextof the dispatched eventRaisesasyncio.TimeoutError