如何对数据库进行更改前缀命令?

时间:2021-07-16 08:44:27

标签: python discord.py

我想获取公会 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 guild_id 前缀
1 这里是id .
2 这里是id ?

1 个答案:

答案 0 :(得分:0)

从这个问题来看你希望不同的公会有不同的前缀。由于这只是一个带有 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)
相关问题