机器人不会在特定时间发送通知

问题描述 投票:0回答:1

机器人不会创建提醒,尽管它应该创建提醒。他只是看不到时间。我希望它能以 24 小时格式正常工作。

# Reminder
notes = {}

@bot.command()
async def note(ctx, time_str: str, *, message):
    try:
        time = datetime.strptime(time_str, "%y.%m.%d %H:%M")
        now = datetime.now()
        if time <= now:
            await ctx.send("You have selected a past time. Please choose a future time.")
            return
        notes[time] = {"author": ctx.author, "message": message}
        await ctx.send(f"Note saved for {time.strftime('%y.%m.%d %H:%M')}")
    except ValueError:
        await ctx.send("Invalid time format. Use the format 'yy.mm.dd HH:MM'.")

async def check_notes(): 
    await bot.wait_until_ready()
    while not bot.is_closed():
        now = datetime.now()
        for time, note in list(notes.items()):
            if time <= now:
                author = note["author"]
                message = note["message"]
                embed = discord.Embed(
                    title="Reminder!",
                    description=f"{author.mention}, here's your reminder:\n{message}",
                    color=discord.Color.blue()
                )
                embed.set_thumbnail(url=author.avatar_url)
                await ctx.send(embed=embed)
                del notes[time]  # Remove the note after sending the reminder
        await asyncio.sleep(60)

@bot.command()
async def my_notes(ctx):
    embed = discord.Embed()
    embed.set_author(name="🌌 | Your Notes", icon_url=ctx.author.avatar.url)
    if not notes:
        embed.description = ":calendar: You don't have any notes yet."
    else:
        note_list = "\n".join([f"• **{time.strftime('%y.%m.%d %H:%M')}**: {note['message']}" for time, note in notes.items() if note['author'] == ctx.author])
        embed.description = f":calendar: | Your Notes:\n{note_list}"
    await ctx.send(embed=embed)

我尝试了我力所能及的一切,但没有任何帮助。我坐了一整天,什么也不懂。

discord discord.py
1个回答
0
投票

从您发送的代码来看,您实际上并没有在任何地方调用

check_notes

不建议使用带有

while not Bot.is_closed()
的函数。相反,请使用
tasks
,它在命名空间
discord.ext.tasks
中可用。这是创建提醒的一种非常糟糕的方式,因为如果机器人崩溃或您重新启动它,所有提醒都将丢失。我建议使用数据库来存储提醒。这是提醒命令的示例。

© www.soinside.com 2019 - 2024. All rights reserved.