python - 我如何使用数据库进行更改前缀命令?
问题描述
我想获取公会 ID 尝试如果公会 ID == 在数据库中保存公会 ID,获取前缀并将前缀更改为数据库中的前缀。代码:
intents = discord.Intents().all()
Bot = commands.Bot(command_prefix=prefix here, intents=intents)
@Bot.event
async def on_guild_join(guild):
server = get_guild_or_false(guild.id)
if server:
pass
else:
aso = Aso(guild.id,".").save()
db.commit()
@Bot.command()
async def change_prefix(ctx,prefix):
objects = Aso.manager(db)
guild_id = ctx.message.guild.id
for guildcode in objects.all():
guildcode_id = guildcode.id
idcode = objects.get(guildcode_id)
if guild_id == idcode.guild_id:
idcode.prefix = prefix
idcode.update()
db.commit()
ID | 公会ID | 字首 |
---|---|---|
1 | 身份证在这里 | . |
2 | 身份证在这里 | ? |
解决方案
从你想为不同的公会有不同的前缀的问题来看。由于这只是一个 discord.py 标记的问题,这就是您想要的:
from discord.ext import commands
async def get_prefix(bot, message):
prefix = "?" # Default prefix
if not message.guild:
return commands.when_mentioned_or(prefix)(bot, message)
# DB fetch code here and replace the prefix variable
return commands.when_mentioned_or(prefix)(bot, message)
intents = discord.Intents().all()
Bot = commands.Bot(command_prefix=get_prefix, intents=intents)
推荐阅读
- android - Firebase 事件参数未使用方法 #1 记录,但适用于方法 #2。为什么?
- pdf - PDF 字符串中使用的未知编码
- list - 补丁声明未修补到共享点列表,Power Apps
- c++ - Rubik's Cube Scramble Generator 算法
- image - 图像在 Rshiny 中不显示
- android - 使用 Afreechart 和 itextg 将 lineChart 添加到 Pdf 文件
- python - 如何在将数据添加到 sqlite3 中的表的函数中使用参数?
- angular - 角度 6 中的“窗口”类型上不存在属性“XMLSerializer”
- python - 遍历python列表中的字典条目
- java - Spring 好像随机掉了一颗豆子?