首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图使用discord.py生成一个提醒命令

试图使用discord.py生成一个提醒命令
EN

Stack Overflow用户
提问于 2021-11-28 15:38:37
回答 1查看 172关注 0票数 0

我一直试图在Discord.py中创建一个提醒命令,而且我已经接近完成了,我只是遇到了一个问题,即搜索提醒数据库(MongoDB),并检索即将到期的提醒通知。下面是这个命令和任务的完整代码:

代码语言:javascript
复制
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))

我无法消除的错误如下:

代码语言:javascript
复制
    remindme = str({reminder['time']})
TypeError: '_asyncio.Future' object is not subscriptable

有人有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-28 15:47:34

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70144939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档