接收用户输入并将输入用于 discord.py 中的不同任务
Receiving user input and uses the input for a different task in discord.py
我想在 discord.py 中创建一个命令,当命令被激活时(例如本例中的 h!start),它会要求用户输入它想要接收的分钟数提醒,程序会在 x 分钟后发送提醒。
我有提醒的代码,只是不知道如何让用户输入特定的时间段。
@client.command()
async def start(ctx):
global start_channel
start_channel = ctx.channel.id
channel = client.get_channel(int(start_channel))
reminder.start()
await channel.send('Reminder Started')
@tasks.loop(minutes=5) #the amount of minutes should be able to be set by the user
async def reminder():
channel = client.get_channel(int(start_channel))
await channel.send('It has been 5 minutes.')
@client.command()
async def stop(ctx):
channel = client.get_channel(int(start_channel))
reminder.cancel()
await channel.send('Reminder stopped. Thanks for using!')
获取用户输入的最佳方式是将其包含在函数调用中。例如 h!start 5
启动功能并将定时器设置为 5 分钟。
为此,您应该将函数头更改为以下内容:
async def start(ctx, input: int):
然后你可以使用 asyncio.sleep(input)
休眠你想要的时间,而不是使用装饰器。
我想在 discord.py 中创建一个命令,当命令被激活时(例如本例中的 h!start),它会要求用户输入它想要接收的分钟数提醒,程序会在 x 分钟后发送提醒。
我有提醒的代码,只是不知道如何让用户输入特定的时间段。
@client.command()
async def start(ctx):
global start_channel
start_channel = ctx.channel.id
channel = client.get_channel(int(start_channel))
reminder.start()
await channel.send('Reminder Started')
@tasks.loop(minutes=5) #the amount of minutes should be able to be set by the user
async def reminder():
channel = client.get_channel(int(start_channel))
await channel.send('It has been 5 minutes.')
@client.command()
async def stop(ctx):
channel = client.get_channel(int(start_channel))
reminder.cancel()
await channel.send('Reminder stopped. Thanks for using!')
获取用户输入的最佳方式是将其包含在函数调用中。例如 h!start 5
启动功能并将定时器设置为 5 分钟。
为此,您应该将函数头更改为以下内容:
async def start(ctx, input: int):
然后你可以使用 asyncio.sleep(input)
休眠你想要的时间,而不是使用装饰器。