sql >> Database >  >> NoSQL >> MongoDB

Herinneringsopdracht met MongoDB discord.py

bijlage bij de opmerkingen onder de vraag:

om te controleren of het tijd is om een ​​gebruiker eraan te herinneren, kunt u de datetime . gebruiken module

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))



  1. $addFields gebruiken in MongoDB Driver voor C#

  2. mongodb-query:hoe unieke vermeldingen te krijgen

  3. Top 10 kenmerken van Big Data Hadoop

  4. Wachtwoord opnieuw instellen in NodeJS