【问题标题】:Discord.py set custom guild prefixes using sqlite3Discord.py 使用 sqlite3 设置自定义公会前缀
【发布时间】:2021-07-19 02:00:53
【问题描述】:

我正在用 Python 和 sqlite3 制作一个 Discord 机器人。它基本上是一个多用途的机器人,可以用于许多事情。它的功能之一是公会的自定义前缀 - 但是,该机器人似乎没有响应我发送的新前缀。这是我的代码:

@bot.group(invoke_without_command=True)
async def prefix(ctx):
    await ctx.send("**Prefix command configuration:**\n\nSet new prefix: `j!prefix new <prefix>`")

实际的new命令:

@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()

我的机器人 = commands.Bot:

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="????"))

(prefix是我之前在代码中设置的变量,叫prefix = "j!")

机器人似乎根本不响应新前缀;但是,前缀看起来已经正确存储在我的 SQLite3 数据库(称为“main”)中。我在这里做错了什么,也许是调用 bot = commands.Bot 的部分?我该如何解决这个问题?

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您可以通过函数获取每个公会的前缀。

    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
    

    而不是commands.Bot

    bot = commands.Bot(command_prefix=get_prefix, ...)
    

    【讨论】:

    • 如果数据库不存在,您也可以将默认前缀添加到数据库中
    • 谢谢,该代码似乎有效,但每当我尝试在另一台服务器(前缀未更改的服务器)中向我的机器人发送消息时,我都会收到此错误:TypeError: command_prefix must be plain string, iterable of strings, or callable returning either of these, not NoneType(我的另一台服务器也在数据库中)我该如何修改这个,如果服务器在数据库中但没有前缀,它只是默认为默认前缀?
    • 如果前缀不存在(prefix = "!" 顶部行),您可以添加一个 sql 命令,将公会前缀添加到数据库中
    猜你喜欢
    • 2021-03-31
    • 2021-04-04
    • 2021-06-15
    • 2021-03-30
    • 2021-11-01
    • 2020-12-31
    • 1970-01-01
    • 2021-02-15
    • 2019-06-26
    相关资源
    最近更新 更多