-1

我是数据库和日期时间之类的新手。

我实际上想创建一个使用 MongoDB 作为数据库的提醒命令。我正在使用 Motor,因为我想与它一起使用 asyncio。请告诉我我是否在正确的道路上,如果我不是,那我该怎么办?

我已经使用电机设置了与 MongoDB 的基本连接。

这是我的代码。

import discord
from discord.ext import commands
import pymongo
from pymongo import MongoClient
import os
import asyncio
import motor
import motor.motor_asyncio

class Reminder(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print('Reminder is Ready.')


    ### MongoDB Variables ###

    @commands.command()
    async def remind(self, ctx, time, *, msg):


        ### MongoDB Variables ###
        mongo_url = os.environ['Mongodb_url']
        cluster = motor.motor_asyncio.AsyncIOMotorClient(str(mongo_url))
        db = cluster['Database']
        collection = db['reminder']

        
        ### Discord Variables ###
        author_id = ctx.author.id
        guild_id = ctx.guild.id


        ### Time Variables ###
        time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
        remindertime = int(time[0]) * time_conversion[time[-1]]

        if ctx.author.bot:
            return

        if (await collection.count_documents({}) == 0):
            rem_info = {"_id": author_id, "GuildID": guild_id, "time": remindertime, "msg": msg}

            await collection.insert_one(rem_info)
            await ctx.send('Logged In')

        


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

什么是提醒命令和我想做什么?

基本上,该命令将花费被提醒的时间量和被提醒的主题作为参数。在命令中指定的一定时间后,它会 DM 用户说“你让我提醒你关于 {topic}”。

我希望这是所有需要的信息。

4

1 回答 1

0

问题下方评论的附件:

检查是否该提醒用户您可以使用该datetime模块

import discord
from discord.ext import commands, tasks
import pymongo
from pymongo import MongoClient
import os
import asyncio
import motor
import motor.motor_asyncio

import datetime
from datetime import datetime, timedelta

### MongoDB Variables ###
mongo_url = os.environ['Mongodb_url']
cluster = motor.motor_asyncio.AsyncIOMotorClient(str(mongo_url))
db = cluster['Database']
collection = db['reminder']

class Reminder(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):
        reminders = collection.find({})
        for reminder in reminders:
            # reminder should look like this: {"_id": 1234, "GuildID": 1234, "time": datetime_objekt, "msg": "some text"}
            now = datetime.now()
            if now >= reminder:
                # remind the user 
                guild = self.client.get_guild(reminder['GuildID'])
                member = guild.get_member(reminder['_id'])
                await member.send(f"reminder for {reminder['msg']}")
            



    @commands.Cog.listener()
    async def on_ready(self):
        print('Reminder is Ready.')


    @commands.command()
    async def remind(self, ctx, time, *, msg):
        """Reminder Command"""
        # like above
        
        # just change this
        ### Time Variables ###
        time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
        remindseconds = int(time[0]) * time_conversion[time[-1]]
        remindertime = datetime.now() + timedelta(seconds=remindseconds)


        


def setup(bot):
    bot.add_cog(Reminder(bot))
于 2021-06-22T14:18:15.087 回答