接收用户输入并将输入用于 discord.py 中的不同任务

时间:2021-01-12 20:20:41

标签: python discord.py discord.py-rewrite

我想在 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!')

1 个答案:

答案 0 :(得分:0)

获取用户输入的最佳方式是将其包含在函数调用中。例如 h!start 5 启动函数并将计时器设置为 5 分钟。 为此,您应该将函数标头更改为以下内容:

async def start(ctx, input: int):

然后您可以使用 asyncio.sleep(input) 来睡眠所需的时间,而不是使用装饰器。