MongoDB
 sql >> Base de Dados >  >> NoSQL >> MongoDB

Comando de lembrete usando o MongoDB discord.py


anexo aos comentários abaixo da pergunta:

para verificar se é hora de lembrar um usuário, você pode usar o datetime módulo
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))