【问题标题】:Reminder Command Issue in discord.pydiscord.py 中的提醒命令问题
【发布时间】:2020-12-18 21:20:30
【问题描述】:

我正在尝试发出不和谐的机器人提醒命令,但是,我遇到了一个问题。

当我执行命令时,它完美地执行了睡眠时间,但是,它没有显示我设置的提醒。

示例:当我发送/reminder 5m Fix the server 时,它应该发送Alright, I will remind you about Fix the server in 5 minutes.,但是,它发送的是:Alright, I will remind you about {} in 5 minutes. 而且当我不包含提醒参数时,它应该发送一条错误消息,但它什么也不做。

这是我的代码:

@bot.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):
    user = ctx.message.author 
    embed = discord.Embed(title="Please specify a proper duration, send `reminder_help` for more information.", description="", 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"{bot.user.avatar_url}")
    embed2 = discord.Embed(title="Please specify what do you want me to remind you about.", description="", color=0x55a7f7, timestamp=datetime.utcnow())
    embed2.set_footer(text="If you have any questions, suggestions or bug reports, please join our support Discord Server: link hidden", icon_url=f"{bot.user.avatar_url}")    
    embed3 = discord.Embed(title="You have specified a too long duration!\nMaximum duration is 90 days.", color=0x55a7f7, timestamp=datetime.utcnow())
    embed3.set_footer(text="If you have any questions, suggestions or bug reports, please join our support Discord Server: link hidden", icon_url=f"{bot.user.avatar_url}")        
    embed4 = discord.Embed(title="You have specified a too short duration!\nMinimum duration is 5 minutes.", color=0x55a7f7, timestamp=datetime.utcnow())
    embed4.set_footer(text="If you have any questions, suggestions or bug reports, please join our support Discord Server: link hidden", icon_url=f"{bot.user.avatar_url}")      
    seconds = 0
    if reminder is None:
        await ctx.send(embed=embed2) # Error message
        return
    for i in time:
        if i.lower().endswith("d"):
            seconds += int(i[:-1]) * 60 * 60 * 24
            counter = f"{seconds//60//60//24} days"
        if i.lower().endswith("h"):
            seconds += int(i[:-1]) * 60 * 60  
            counter = f"{seconds//60//60} hours"
        elif i.lower().endswith("m"):
            seconds += int(i[:-1]) * 60
            counter = f"{seconds//60} minutes"
        elif i.lower().endswith("s"):
            seconds += int(i[:-1])  
            counter = f"{seconds} seconds"
        if seconds == 0:
            await ctx.send(embed=embed) # Error message
            return
        elif seconds < 300:
            await ctx.send(embed=embed4) # Error message
            return
        elif seconds > 7776000:
            await ctx.send(embed=embed3) # Error message
            return
        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.")

任何帮助将不胜感激,在此先感谢。

【问题讨论】:

  • 据我所知,代码没有问题。你说只是{reminder} 部分不起作用,对吧?
  • 是的,提醒部分不起作用,它应该显示提醒是什么,但它显示的是像 {} 这样的空括号。而且当我根本没有提醒时它也不会发送错误。
  • 您是否尝试将async def reminder(ctx, *time, **reminder): 更改为async def reminder(ctx, time, *, reminder):
  • 我尝试用你上面所说的替换它,但由于某种原因,无论我输入什么,秒计数器总是为 0,我不确定是什么原因造成的,尽管我假设我还必须编辑代码的某些部分以适应参数更改。
  • embed1,2,3,4 在哪里?

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


【解决方案1】:

我通过更改代码中的一些内容解决了您的问题。所以这是新代码:

@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("d"):
        seconds += int(time[:-1]) * 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)

首先,我不知道是什么导致了问题,但是你无缘无故地使用了 for 循环,这是不必要的。然后您不必创建 4 次嵌入,您可以使用 add_field。所以它现在有效。如果仍有问题,请发表评论。

【讨论】:

  • 嘿,感谢您的帮助,但我对那个帖子还有另一个问题。是否可以使用if time.lower().endswith("day") 而不是("d")?我已尝试更改它,但出现值错误:ValueError: invalid literal for int() with base 10: 'da' 你能帮我解决这个问题吗?
  • 如果你用day改变它,你应该改变seconds += int(time[:-1])的部分。您可以使用seconds += int(time[:-3]) 更改它。
  • 这确实适用于一天,谢谢,但是当我尝试执行 days 并输入 (time[:-4]) 时它不起作用并给出 ValueError:示例:/reminder 5days HelloCommand raised an exception: ValueError: invalid literal for int() with base 10: '5day'
  • 你能显示代码吗?您必须更改触发错误的内容。
  • 和你上面发的一模一样,只是我把if time.lower().endswith("d"): seconds += int(time[:-1]) * 60 * 60 * 24改成了:if time.lower().endswith("days"): seconds += int(time[:-4]) * 60 * 60 * 24
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 2021-09-06
  • 2021-07-11
  • 1970-01-01
  • 2021-03-06
  • 2022-01-05
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多