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