【问题标题】:How to setup on_message with sqlite3 in discord.py?如何在 discord.py 中使用 sqlite3 设置 on_message?
【发布时间】:2020-12-18 10:41:18
【问题描述】:

您好,我创建了以下活动。它需要输入一次,然后将 guild id 、 channel id 和 role id 存储在 sqlite db 中。之后,当该特定公会中的某个人在特定频道中提到至少 3 个用户时,机器人会赋予他们新的角色。

class ScrimsCog(commands.Cog, name='Scrims-Commands') :

        def __init__(self,bot):
            self.bot = bot
        @commands.Cog.listener()
        async def on_message(self, message):
            if message.guild:
                db = sqlite3.connect('main.sqlite')
                cursor = db.cursor()
                cursor.execute(
                    f"SELECT * FROM main WHERE guild_id = ?", (message.guild.id, ))
                result = cursor.fetchone()
                if result:
                    channel = self.bot.get_channel(result[2])
                    role = message.guild.get_role(result[1])
                    if role:
                        if message.channel == channel:
                            if len(message.mentions) >= 3:
                                await message.add_reaction(emoji="<a:tick:748476262640779276>")
                            user = message.author
                            await user.add_roles(role)
            await self.bot.process_commands(message)
            

        
        
        @commands.group(invoke_without_command=True)
        async def scrimsmod(self,ctx):
            await ctx.send('Available Setup Commands: \nscrimsmod channel <#channel>\nscrimsmod role  <message>')
        @scrimsmod.command()
        async def channel(self, ctx, channel:discord.TextChannel):
            if ctx.message.author.guild_permissions.manage_messages:
                db = sqlite3.connect('main.sqlite')
                cursor = db.cursor()
                cursor.execute(f"SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}")
                result =  cursor.fetchone()
                if result is None:
                    sql = ("INSERT INTO main(guild_id, channel_id) VALUES(?,?)")
                    val = (ctx.guild.id, channel.id)
                    await ctx.send(f" Default Registration Channel has been set to {channel.mention}")
                elif result is not None:
                    sql = ("UPDATE main SET channel_id = ? WHERE guild_id = ?")
                    val = (channel.id, ctx.guild.id)
                    await ctx.send(f"Default Registration Channel has been updated to {channel.mention}")
                cursor.execute(sql, val)
                db.commit()
                cursor.close()
                db.close()

        @scrimsmod.command()
        async def role(self, ctx,role: discord.Role):
            if ctx.message.author.guild_permissions.manage_messages:
                db = sqlite3.connect('main.sqlite')
                cursor = db.cursor()
                cursor.execute(f"SELECT role FROM main WHERE guild_id = {ctx.guild.id}")
                result =  cursor.fetchone()
                if result is None:
                    sql = ("INSERT INTO main(guild_id, role) VALUES(?,?)")
                    val = (ctx.guild.id, role.id)
                    await ctx.send(f"Default role to give on correct registration have been set to `{role}`")
                elif result is not None:
                    sql = ("UPDATE main SET role = ? WHERE guild_id = ?")
                    val = (role.id, ctx.guild.id)
                    await ctx.send(f"Default role to give on correct registration have been updated to  `{role}`")
                cursor.execute(sql, val)
                db.commit()
                cursor.close()
                db.close()
    

好吧,我相信代码很好,它不会抛出任何错误。问题是它完美地接受输入而没有任何错误,但不会将其存储到数据库中。

我相信问题出在我的数据库上, 代码 -

db = sqlite3.connect('main.sqlite',timeout=10)
    cursor = db.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS main(
            guild_id INTEGER,
            role INTEGER,
            channel_id INTEGER
        )
    ''')

我相信问题就在这里,我应该将 TEXT 放在 INTEGER 的位置,但我不确定。回答我你做错了什么以及我应该如何解决这个问题。

【问题讨论】:

    标签: python sqlite discord discord.py


    【解决方案1】:

    我回答了你的最后一个问题,假设角色和频道是TEXT 表格中的类型,并且该答案应该已经解决了你的问题。如果您更改它们,它将正常工作,并且如果您在on_message 事件顶部定义数据库变量和光标一次,它会更有效。所以你不必每次都连接到数据库。

    【讨论】:

    • 您好,我已将角色、频道和公会 ID 更改为文本类型,但它仍然没有在 db 中记录任何数据。我应该切换到其他数据库吗? @Nurqm
    • 尝试另一个数据库并尝试我在您上一个问题中编写的代码。
    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 2021-01-30
    • 2021-07-19
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多