【问题标题】:Trying to make a remind command with discord.py尝试使用 discord.py 发出提醒命令
【发布时间】:2022-01-05 17:26:37
【问题描述】:

我一直在尝试在 Discord.py 中创建一个提醒命令,我已经快完成了,我只是遇到了搜索提醒数据库 (MongoDB) 并检索到期提醒的任务的问题DM作者。这是我的这个命令和任务的完整代码:

class remind(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.reminder_task.start()

    def cog_unload(self):
        self.reminder_task.cancel()

    @tasks.loop(minutes=1.0)
    async def reminder_task(self):
        data = collection.find({}).to_list(length=None)
        # async for i in data:
        for reminder in data:
            now = datetime.now()
            remindme = str({reminder['time']})
            remind = datetime.strptime(remindme, "%Y-%m-%d %H:%M:%S.%f")
            if now >= remind:
                guild = self.client.get_guild(remind['GuildID'])
                member = guild.get_member(remind['author_id'])
                await member.send(f"Reminder: `{remind['msg']}`")



    @slash_commands.command(name="remind", description="create a reminder!", options=[Option("time", "time until I remind you", Type.STRING, required=True), Option("reminder", "what should I remind you about?", Type.STRING, required=True)], guild_ids=bonbot_support)
    async def remind(self, ctx, time, reminder):
        time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}
        remindseconds = int(time[0]) * time_conversion[time[-1]]
        remindertime = datetime.now() + timedelta(seconds=remindseconds)
        author_id = ctx.author.id
        guild_id = ctx.guild.id
        if ctx.author.bot:
                return

        rem_info = {"author_id": author_id, "GuildID": guild_id, "time": remindertime, "msg": reminder}

        await collection.insert_one(rem_info)
        await ctx.send('Reminder was set!')


def setup(bot):
    bot.add_cog(remind(bot))

我无法摆脱的错误如下:

    remindme = str({reminder['time']})
TypeError: '_asyncio.Future' object is not subscriptable

有人有什么想法吗?

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    data = collection.find({}).to_list(length=None) 替换为data = await collection.find({}).to_list(length=None),因为您尝试使用未来对象。

    【讨论】:

    • 出于某种原因,这给了我:“discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.remind' 引发错误:SyntaxError: cannot assign to await expression (remind.py, line 40 )"
    • 哦,那是我的错误。我已经编辑了我的答案。请查看并重试。
    • 做到了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2020-12-18
    • 2021-07-11
    • 2021-03-06
    • 1970-01-01
    • 2022-01-21
    • 2021-08-09
    相关资源
    最近更新 更多