Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new to databases and datetime and stuff.

I actually want to create a reminder command which uses MongoDB as a Database. I am using Motor as I want to use asyncio along with it. Please tell me if I am on the right path or not and if I am not, then what should I do?

I have setup the basic connection with MongoDB using motor.

Here is my code.

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

What is Reminder Command and whati want to do?

Basically, the command will take the amount of time to be reminded and the topic to be reminded about as arguments. After the certain amount of time specified in the command, it will DM the user that "You asked me to remind you about {topic}".

I hope that is all the needed information.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
157 views
Welcome To Ask or Share your Answers For Others

1 Answer

attachment to the comments below the question:

to check if it is time to remind a user you can use the datetime 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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...