Add a wait_for function to discord-py-interactions
Project description
interactions-wait-for
Extension for discord-py-interactions which implements wait_for
Installation:
pip install -U interactions-wait-for
wait_for
Benefits:
- An actual
wait_for
- Asynchronous checks
- Timeouts
- Doesn't overwrite any library code
So what is this so-called wait_for
?
wait_for
is an awaitable future that waits for a specific event, and returns the result.
Use cases:
- Waiting for an interaction or message
- Continue commands after response
- Unlike events:
- You keep data from your slash command
- You can listen for a response with a timer and a check
- You can do stuff when timed out
Okay, but how do I use it?
You import the wait_for
library like this:
from interactions.ext import wait_for
Here is an example code which shows you how to wait for a message, with an asynchronous check and a timeout:
from interactions import Client, Message
from interactions.ext.wait_for import wait_for
import asyncio
bot = Client(token="...")
# apply hooks to the class
wait_for.setup(bot)
@bot.command(
name="test", description="this is just a test command.", scope=817958268097789972
)
async def test(ctx):
await ctx.send("grabbing a message...")
async def check(msg):
if int(msg.author.id) == int(ctx.author.user.id):
return True
await ctx.send("I wasn't asking you")
return False
try:
msg: Message = await wait_for(
bot, "on_message_create", check=check, timeout=15
)
except asyncio.TimeoutError:
return await ctx.send("You said nothing :(")
bot.start()
wait_for_component
What's the difference between wait_for
and wait_for_component
?
While you could wait for a component click with wait_for
, wait_for_component
is 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 the wait_for_component
so 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:
from interactions import Client, ComponentContext, Button
from interactions.ext import wait_for
import asyncio
bot = Client(token="...")
# apply hooks to the class
# add_method adds the wait_for and wait_for_component methods to your bot
wait_for.setup(bot, add_method=True)
@bot.command(
name="test", description="this is just a test command.", scope=817958268097789972
)
async def test(ctx):
button = Button(style=1, label="testing", custom_id="testing")
await ctx.send("grabbing a click...", components=button)
async def check(button_ctx):
if int(button_ctx.author.user.id) == int(ctx.author.user.id):
return True
await ctx.send("I wasn't asking you!", ephemeral=True)
return False
try:
button_ctx: ComponentContext = await bot.wait_for_component(
components=button, check=check, timeout=15
)
except asyncio.TimeoutError:
return await ctx.send("You didn't click :(")
bot.start()
async wait_for
Arguments:
name
-str
: The event to wait forcheck
-Optional[Callable[..., Union[bool, Awaitable[bool]]]]
: A function or coroutine to call, which should return a truthy value if the data should be returned, defaultNone
timeout
-Optional[float]
: How long to wait for the event before raising an error
Returns:
The value of the dispatched event
Raises:
asyncio.TimeoutError
async wait_for_component
Arguments:
components
-Union[Union[Button, SelectMenu], List[Union[Button, SelectMenu]]]
: The component(s) to wait for, defaultNone
messages
-Union[interactions.Message, int, list]
: The message to check for, or a list of messages to check forcheck
-Optional[Callable[..., Union[bool, Awaitable[bool]]]]
: A function or coroutine to call, which should return a truthy value if the data should be returned, defaultNone
timeout
-Optional[float]
: How long to wait for the event before raising an error
Returns:
The ComponentContext
of the dispatched event
Raises:
asyncio.TimeoutError
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for interactions-wait-for-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 972c1469d3a9b8255d5c404a0cb6d64eb3944056bcd1a53f7d4beeb7a28842ba |
|
MD5 | 6dcfef67068582fd4302342e7d79ea17 |
|
BLAKE2b-256 | 3631bfdfde53a63e24a95dba970f241c3fe47cf0fcfd362d54cdf91c30e6efbc |
Hashes for interactions_wait_for-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad2cc2919f6cb22d86fb969879654bf7eddb7d56a45f893f853c914cc614f722 |
|
MD5 | 282a1b5767d0122eb31e823a2a3ce954 |
|
BLAKE2b-256 | 6041f20f2f468fdd818bb3b798efd42c4f07d0ccac1893ddac178869f11354ad |