Python Discord.py使用sqlite3设置自定义公会前缀

Python Discord.py使用sqlite3设置自定义公会前缀,python,discord.py,Python,Discord.py,我正在用Python和sqlite3制作一个不和谐的机器人。它基本上是一个多用途的机器人,可以用于很多事情。它的一个特性是为公会定制前缀——但是,bot似乎没有响应我发送的新前缀。这是我的代码: @bot.group(invoke_without_command=True) async def prefix(ctx): await ctx.send("**Prefix command configuration:**\n\nSet new prefix: `j!prefix n

我正在用Python和sqlite3制作一个不和谐的机器人。它基本上是一个多用途的机器人,可以用于很多事情。它的一个特性是为公会定制前缀——但是,bot似乎没有响应我发送的新前缀。这是我的代码:

@bot.group(invoke_without_command=True)
async def prefix(ctx):
    await ctx.send("**Prefix command configuration:**\n\nSet new prefix: `j!prefix new <prefix>`")
@bot.group(调用\u而不调用\u命令=True)
异步def前缀(ctx):
等待ctx.send(“**前缀命令配置:*\n\n设置新前缀:`j!前缀新`”)
实际的命令:

@prefix.command()
async def new(ctx, new_pr):
    if ctx.message.author.guild_permissions.manage_messages:
        db = sqlite3.connect('main.sqlite')
        cursor = db.cursor()
        cursor.execute(f"SELECT prefix FROM main WHERE guild_id = {ctx.guild.id}")
        prefix = cursor.fetchone()
        if prefix is None:
            sql = ("INSERT INTO main(guild_id, prefix) VALUES(?,?)")
            val = (ctx.guild.id, new_pr)
            prefixsetem = discord.Embed(title=f"<:Success:835190431758549043> **{ctx.guild.name}**'s prefix set to `{new_pr}`", description=f"Set by **{ctx.author}**", color=0x03fc45)
            await ctx.send(embed=prefixsetem)
        elif prefix is not None:
            sql = ("UPDATE main SET prefix = ? WHERE guild_id = ?")
            val = (new_pr, ctx.guild.id)
            prefixsetem = discord.Embed(title=f"<:Success:835190431758549043> **{ctx.guild.name}**'s prefix updated to `{new_pr}`", description=f"Updated by **{ctx.author}**", color=0x03fc45)
            await ctx.send(embed=prefixsetem)
        cursor.execute(sql, val)
        db.commit()
        cursor.close()
        db.close()
@prefix.command()
异步def新建(ctx,新建\U pr):
如果ctx.message.author.guild\u permissions.manage\u消息:
db=sqlite3.connect('main.sqlite')
cursor=db.cursor()
cursor.execute(f“从主目录中选择前缀,其中guild_id={ctx.guild.id}”)
prefix=cursor.fetchone()
如果前缀为“无”:
sql=(“插入到主(公会id,前缀)值(?,)中)
val=(ctx.guild.id,新的\u pr)
prefixsetem=discord.Embed(title=f“**{ctx.guild.name}**”的前缀设置为“{new_pr}`”,description=f“set by**{ctx.author}**”,color=0x03fc45)
等待ctx.send(嵌入=前缀)
elif前缀不是无:
sql=(“更新主集合前缀=?其中id=?”)
val=(新的,ctx.guild.id)
prefixsetem=discord.Embed(title=f“**{ctx.guild.name}**”的前缀更新为“{new_pr}`”,description=f“更新人**{ctx.author}**”,颜色=0x03fc45)
等待ctx.send(嵌入=前缀)
cursor.execute(sql,val)
db.commit()
cursor.close()
db.close()
我的机器人=命令。机器人:

bot = commands.Bot(command_prefix=prefix, intents=intents, help_command=PrettyHelp(active_time=120, color=0xf5b642, ending_note="j! help <command> for information about a command - select reactions to move to next pages or to delete this message", show_index=False, page_left="⏪", page_right="⏩", remove="You can get prefixes per guild by a function.

def get_prefix(bot, message):
    db = sqlite3.connect("main.sqlite")
    cur = db.cursor()
    cur.execute("SELECT prefix FROM main WHERE guild_id = ?", (message.guild.id))
    prefix = cur.fetchone()
    if len(prefix):
        prefix = prefix[0]
    else:
        prefix = "!" #return default prefix if guild not saved in database.
    db.close()
    return prefix

bot=commands.bot(command\u prefix=prefix,intents=intents,help\u command=PrettyHelp(active\u time=120,color=0xf5b642,end\u note=“j!help获取有关命令的信息-选择移动到下一页或删除此消息的反应”),show\u index=False,page\u left=”⏪", 第_页右=”⏩“,remove=“您可以通过函数获得每个公会的前缀

bot = commands.Bot(command_prefix=get_prefix, ...)
而不是
命令。Bot


如果数据库中还没有默认前缀,您也可以将其添加到数据库中。谢谢,该代码似乎可以工作,但每当我尝试在另一台服务器(前缀未更改的服务器)中向我的bot发送消息时,我得到了这个错误:
TypeError:command\u前缀必须是纯字符串、可调用的字符串或可调用的返回这些字符串中的任何一个,而不是NoneType
(我的另一个服务器也在数据库中)如果服务器在数据库中,但没有前缀,我如何修改它,使其默认为默认前缀?如果前缀不存在,您可以添加一个sql命令,在数据库中添加公会前缀(
prefix=“!”
行在顶部)好的,谢谢!效果非常好!