【问题标题】:Another issue in Discord Bot Reminder command in discord.pydiscord.py 中 Discord Bot Reminder 命令的另一个问题
【发布时间】:2020-12-20 12:10:57
【问题描述】:

最近我做了一个不和谐的机器人提醒命令并遇到了问题,但感谢 Nurqm 的帮助,我能够修复它。 但是,我在时间设置中遇到了另一个问题,当我做/reminder 5days Fix the server,提醒我在5天后修复服务器时,我得到了一个ValueError。

这是我的代码:

@client.command(case_insensitive = True, aliases = ["remind", "remindme", "remind_me"])
@commands.bot_has_permissions(attach_files = True, embed_links = True)
async def reminder(ctx, time, *, reminder):
    print(time)
    print(reminder)
    user = ctx.message.author
    embed = discord.Embed(color=0x55a7f7, timestamp=datetime.utcnow())
    embed.set_footer(text="If you have any questions, suggestions or bug reports, please join our support Discord Server: link hidden", icon_url=f"{client.user.avatar_url}")
    seconds = 0
    if reminder is None:
        embed.add_field(name='Warning', value='Please specify what do you want me to remind you about.') # Error message
    if time.lower().endswith("days"):
        seconds += int(time[:-4]) * 60 * 60 * 24
        counter = f"{seconds // 60 // 60 // 24} days"
    if time.lower().endswith("h"):
        seconds += int(time[:-1]) * 60 * 60
        counter = f"{seconds // 60 // 60} hours"
    elif time.lower().endswith("m"):
        seconds += int(time[:-1]) * 60
        counter = f"{seconds // 60} minutes"
    elif time.lower().endswith("s"):
        seconds += int(time[:-1])
        counter = f"{seconds} seconds"
    if seconds == 0:
        embed.add_field(name='Warning',
                        value='Please specify a proper duration, send reminder_help for more information.')
    elif seconds < 300:
        embed.add_field(name='Warning',
                        value='You have specified a too short duration!\nMinimum duration is 5 minutes.')
    elif seconds > 7776000:
        embed.add_field(name='Warning', value='You have specified a too long duration!\nMaximum duration is 90 days.')
    else:
        await ctx.send(f"Alright, I will remind you about {reminder} in {counter}.")
        await asyncio.sleep(seconds)
        await ctx.send(f"Hi, you asked me to remind you about {reminder} {counter} ago.")
        return
    await ctx.send(embed=embed)

错误是:Command raised an exception: ValueError: invalid literal for int() with base 10: '5day'

如果有人知道有关如何处理此错误的任何信息,请通知我。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    我只是想出了为什么这不起作用,因为您每次都创建一个if statement,所以当您输入/reminder 10days test 时,它会触发几天但也会触发elif time.lower().endswith("s"):。如果你把这个if time.lower().endswith("h"):if statement改成elif statement,你的问题就解决了。

    【讨论】:

    • 这确实是正确的解决方案,我还添加了所有可能的时间别名,如 min、hr、sec 等。所以我不得不重新排列 if 语句的顺序,把 "s " 和 "d" 语句最后。感谢您一直以来的帮助。
    猜你喜欢
    • 2021-04-16
    • 2020-08-16
    • 2020-09-24
    • 2021-01-25
    • 1970-01-01
    • 2021-01-02
    • 2020-11-03
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多