mongodb - 使用 MongoDB discord.py 的提醒命令
问题描述
我是数据库和日期时间之类的新手。
我实际上想创建一个使用 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}”。
我希望这是所有需要的信息。
解决方案
问题下方评论的附件:
检查是否该提醒用户您可以使用该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))
推荐阅读
- python - 有没有办法在 Jupyter Notebook 上创建一个脚本,我可以一个接一个地运行一个笔记本?
- tinymce - 工具栏设置的自定义所见即所得问题
- android - 如何在本机反应中关闭/禁用 Swype 上的选项卡更改(从 react-native-elements 导入)
- java - 队列实现,peek() 方法错误
- python - 命令“python setup.py egg_info”失败,错误代码 1 在 /tmp/pip-build-hn4hol_z/spacy/
- .net - POST 请求在 Chrome 中为一小部分设备返回 411 错误
- python - 如何使用 pyarmor 混淆整个包?
- python - 仅在 python click 中使用 --help 时显示消息
- r - 如何让 r 显示特定的观察结果?
- java - java测试中的“cucumber.runtime.CucumberException:Arity mismatch:Step Definition”是什么?