前缀和提醒命令的数据库 discord.py
Databases for Prefix and Reminder Command discord.py
我不太了解数据库,我想为我的提醒命令和我的可变前缀做一个。对于前缀,我目前使用的是 json 文件,但有人说最好使用数据库。此外,我的提醒命令不会存储提醒,因此当机器人关闭时,提醒就会消失。如果有人能帮助我,我会很高兴。
这是我的提醒命令代码:
@commands.command(aliases=['remind','remindme'])
async def reminder(self, ctx, time, *, reminder):
user = ctx.message.author
embed = discord.Embed(colour=discord.Colour.gold())
seconds = 0
if reminder is None:
embed.add_field(name='❌ Wrong Usage', value='`reminder <duration> <reminder>` is the correct way to use this command.')
if time.lower().endswith("d"):
seconds += int(time[:-1]) * 60 * 60 * 24
counter = f"{seconds // 60 // 60 // 24} days"
if time.lower().endswith("h"):
seconds += int(time[:-1]) * 60 * 60
counter = f"{seconds // 60 // 60} hours"
elif time.lower().endswith("m"):
seconds += int(time[:-1]) * 60
counter = f"{seconds // 60} minutes"
elif time.lower().endswith("s"):
seconds += int(time[:-1])
counter = f"{seconds} seconds"
if seconds == 0:
embed.add_field(name='❌ Wrong Usage',
value='Please specify a proper duration')
elif seconds > 31536000:
embed.add_field(name='❌ Wrong Usage', value='You have specified a too long duration!\nMaximum duration is 365 days.')
else:
await ctx.send(f"Alright, I will remind you about `{reminder}` in `{counter}`.")
await asyncio.sleep(seconds)
await ctx.send(f"**Reminder** {user.mention}: {reminder}")
return
await ctx.send(embed=embed)```
你有 2 个数据库选项 SQL 或否 SQL 所以 MySQL/SQLite/Postgres 或 MongoDB。您可以从他们的网站上获得一个免费的 500MB MongoDB 数据库,但如果您将其托管在 VPS/KVM/dedicated machine/computer 上,则可以在那里进行设置。 JSON 对于涉及频繁数据更改的任何事情都是不利的。
无论何时创建新提醒,都必须将其插入数据库。然后你需要使用 SELECT/find 从中获取它。我在下面附上了一个使用多个前缀的示例,默认前缀为 ?
,在 DMS 中,如果它在 server/guild 中,它将检查数据库。
import pymongo
password = os.environ.get("password")
client = pymongo.MongoClient(f"mongodb://127.0.0.1")
def get_prefix(bot, message):
global client
if not message.guild:
return commands.when_mentioned_or("?")(bot, message)
db = client.bot
posts = db.serversettings
prefix = "?"
for x in posts.find({"guildid":message.guild.id}):
prefix=x["prefix"]
return commands.when_mentioned_or(prefix)(bot, message)
bot = commands.Bot(command_prefix=get_prefix,case_insensitive=True)
我不太了解数据库,我想为我的提醒命令和我的可变前缀做一个。对于前缀,我目前使用的是 json 文件,但有人说最好使用数据库。此外,我的提醒命令不会存储提醒,因此当机器人关闭时,提醒就会消失。如果有人能帮助我,我会很高兴。
这是我的提醒命令代码:
@commands.command(aliases=['remind','remindme'])
async def reminder(self, ctx, time, *, reminder):
user = ctx.message.author
embed = discord.Embed(colour=discord.Colour.gold())
seconds = 0
if reminder is None:
embed.add_field(name='❌ Wrong Usage', value='`reminder <duration> <reminder>` is the correct way to use this command.')
if time.lower().endswith("d"):
seconds += int(time[:-1]) * 60 * 60 * 24
counter = f"{seconds // 60 // 60 // 24} days"
if time.lower().endswith("h"):
seconds += int(time[:-1]) * 60 * 60
counter = f"{seconds // 60 // 60} hours"
elif time.lower().endswith("m"):
seconds += int(time[:-1]) * 60
counter = f"{seconds // 60} minutes"
elif time.lower().endswith("s"):
seconds += int(time[:-1])
counter = f"{seconds} seconds"
if seconds == 0:
embed.add_field(name='❌ Wrong Usage',
value='Please specify a proper duration')
elif seconds > 31536000:
embed.add_field(name='❌ Wrong Usage', value='You have specified a too long duration!\nMaximum duration is 365 days.')
else:
await ctx.send(f"Alright, I will remind you about `{reminder}` in `{counter}`.")
await asyncio.sleep(seconds)
await ctx.send(f"**Reminder** {user.mention}: {reminder}")
return
await ctx.send(embed=embed)```
你有 2 个数据库选项 SQL 或否 SQL 所以 MySQL/SQLite/Postgres 或 MongoDB。您可以从他们的网站上获得一个免费的 500MB MongoDB 数据库,但如果您将其托管在 VPS/KVM/dedicated machine/computer 上,则可以在那里进行设置。 JSON 对于涉及频繁数据更改的任何事情都是不利的。
无论何时创建新提醒,都必须将其插入数据库。然后你需要使用 SELECT/find 从中获取它。我在下面附上了一个使用多个前缀的示例,默认前缀为 ?
,在 DMS 中,如果它在 server/guild 中,它将检查数据库。
import pymongo
password = os.environ.get("password")
client = pymongo.MongoClient(f"mongodb://127.0.0.1")
def get_prefix(bot, message):
global client
if not message.guild:
return commands.when_mentioned_or("?")(bot, message)
db = client.bot
posts = db.serversettings
prefix = "?"
for x in posts.find({"guildid":message.guild.id}):
prefix=x["prefix"]
return commands.when_mentioned_or(prefix)(bot, message)
bot = commands.Bot(command_prefix=get_prefix,case_insensitive=True)