Add a wait_for function to discord-py-interactions
Project description
interactions-wait-for
Extension for interactions.py 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, setup
import asyncio
bot = Client(token="...")
# apply hooks to the class
setup(bot)
@bot.command(
name="test", description="this is just a test command."
)
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.wait_for import setup
import asyncio
bot = Client(token="...")
# apply hooks to the class
setup(bot)
@bot.command(
name="test", description="this is just a test command."
)
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
Waits 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 error
Returns
The value of the dispatched event
Raises
asyncio.TimeoutError
async wait_for_component
Waits for a component to be interacted with, and returns the resulting context.
Arguments
components: 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 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.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3e7ce7fbcd6a2504922e8aeff3b330a9fa0200054af054253ea0f022ec31b44 |
|
MD5 | 62c4b2fc15662d11c25712e6ef983fea |
|
BLAKE2b-256 | 50d1f3a678f36333cb3f95e1b1d999278dac63989a13ddeacdcf1793032d88cf |
Hashes for interactions_wait_for-1.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57a5be43eae819ec9bc76edfbe470730dcb350919f5b1886b55bfa8e9454343b |
|
MD5 | dcaf2dc6760356145c7d415ee01de9ab |
|
BLAKE2b-256 | e50ff0a78bfa776e9a1813dbb3c812e7c75f0f5e7c92ec8546b6169eecf33423 |