我一直试图在Discord.py中创建一个提醒命令,而且我已经接近完成了,我只是遇到了一个问题,即搜索提醒数据库(MongoDB),并检索即将到期的提醒通知。下面是这个命令和任务的完整代码:
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有人有什么想法吗?
发布于 2021-11-28 15:47:34
将data = collection.find({}).to_list(length=None)替换为data = await collection.find({}).to_list(length=None),因为您尝试使用未来的对象。
https://stackoverflow.com/questions/70144939
复制相似问题