为提醒命令添加限制 | discord.py

Adding a limit to a remind command | discord.py

所以我有一个简单的提醒脚本 asyncio,我想知道是否可以对提醒时间添加一个限制,我当时考虑的是 60 分钟左右。这是我的代码:

@client.command()
async def remind(ctx, mins : int, reminder):
    embed=discord.Embed(title='Reminder set', description=f"{ctx.author.mention}, I have set a reminder for {mins} minutes with the reminder being {reminder}", colour=discord.Colour.blurple())
    embed.timestamp = ctx.message.created_at
    await ctx.send(embed=embed)

    counter = 0
    while counter <= int(mins):
        counter += 1
        await asyncio.sleep(60)

        if counter == int(mins):
            await ctx.send(f"{ctx.author.mention}, your reminder for {reminder} with a time of {mins} minutes has gone off.")
            break

如果你想将mins的限制设置为60,你可以检查mins > 60 在开始你的 while 循环之前。

@client.command()
async def remind(ctx, mins : int, reminder):

    if mins > 60:
        # Stop here if mins > 60
        await ctx.send("The limit for a reminder is 60 minutes!")
        return

    embed=discord.Embed(title='Reminder set', description=f"{ctx.author.mention}, I have set a reminder for {mins} minutes with the reminder being {reminder}", colour=discord.Colour.blurple())
    embed.timestamp = ctx.message.created_at
    await ctx.send(embed=embed)

    counter = 0
    while counter <= int(mins):
        counter += 1
        await asyncio.sleep(60)

        if counter == int(mins):
            await ctx.send(f"{ctx.author.mention}, your reminder for {reminder} with a time of {mins} minutes has gone off.")
            break