如何在discord.py中使用sqlite3设置on_message?

时间:2020-08-29 20:54:00

标签: python sqlite discord discord.py

您好,我创建了以下活动。在将公会ID,频道ID和角色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中。

enter image description here

我认为问题出在我的数据库上, 代码-

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位置,但是不确定。回答我,我做错了什么以及应该如何解决。

1 个答案:

答案 0 :(得分:0)

我回答了您的最后一个问题,假设您在表中键入的角色和渠道为TEXT,并且该答案应该可以解决您的问题。如果更改它们,则可以很好地工作,并且如果仅一次在on_message事件顶部定义数据库变量和光标,它将更加高效。因此,您不必每次都连接到数据库。