Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将on_消息与discord.py中的sqlite3集成?_Python_Sqlite_Discord_Discord.py - Fatal编程技术网

Python 如何将on_消息与discord.py中的sqlite3集成?

Python 如何将on_消息与discord.py中的sqlite3集成?,python,sqlite,discord,discord.py,Python,Sqlite,Discord,Discord.py,我做了一个on_message活动,每当你提到某个特定频道中的3个人时,我都会给你一个角色,现在我正在尝试将其与数据库集成,以便可以在多个行会上使用 我写的是: class ScrimsCog(commands.Cog, name='Scrims-Commands') : def __init__(self,bot): self.bot = bot @commands.Cog.listener() async d

我做了一个
on_message
活动,每当你提到某个特定频道中的3个人时,我都会给你一个角色,现在我正在尝试将其与数据库集成,以便可以在多个行会上使用

我写的是:

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

        def __init__(self,bot):
            self.bot = bot
    
        @commands.Cog.listener()
        async def on_message(self,message):
            db = sqlite3.connect('main.sqlite')
            cursor = db.cursor()
            cursor.execute(f"SELECT channel_id FROM main WHERE guild_id = {message.guild.id}")
            result =  cursor.fetchone()
            if result is None:
                return
            else:
                cursor.execute(f"SELECT role FROM main WHERE guild_id = {message.guild.id}")
                if not channel.id == channel_id:
                    return
                if len(message.mentions) >= 3:
                    await message.add_reaction(emoji="<a:tick:748476262640779276>")
                role = discord.utils.get(message.guild.roles, name=role)
                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)
                    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, 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()
    

def setup(bot):
    bot.add_cog(ScrimsCog(bot))
    print("Scrims cog is loaded!")
类ScrimLog(commands.Cog,name='Scrims-commands'):
def uuu init uuuu(自我,机器人):
self.bot=bot
@commands.Cog.listener()
_消息上的异步定义(self,message):
db=sqlite3.connect('main.sqlite')
cursor=db.cursor()
cursor.execute(f“从主频道选择频道,其中guild\u id={message.guild.id}”)
结果=cursor.fetchone()
如果结果为无:
返回
其他:
cursor.execute(f“从主界面选择角色,其中guild_id={message.guild.id}”)
如果不是channel.id==channel\u id:
返回
如果len(消息提及)>=3:
等待消息。添加_反应(emoji=“”)
role=discord.utils.get(message.guild.roles,name=role)
user=message.author
等待用户。添加_角色(角色)
等待self.bot.process_命令(消息)
@commands.group(调用\u而不调用\u command=True)
异步def SCRIMMOD(自身,ctx):
等待ctx.send('可用的安装命令:\nscrimsmod channel\nscrimsmod role')
@scrimmod.command()命令
异步def通道(自、ctx、通道:discord.TextChannel):
如果ctx.message.author.guild\u permissions.manage\u消息:
db=sqlite3.connect('main.sqlite')
cursor=db.cursor()
cursor.execute(f“从主频道选择频道,其中guild\u id={ctx.guild.id}”)
结果=cursor.fetchone()
如果结果为无:
sql=(“插入主(公会id、频道id)值(?,)”)
val=(ctx.guild.id,channel.id)
wait ctx.send(f“默认注册通道已设置为{Channel.ention}”)
elif结果不是无:
sql=(“更新主设置通道\u id=?其中协会\u id=?”)
val=(channel.id,ctx.guild.id)
wait ctx.send(f“默认注册频道已更新为{Channel.notice}”)
cursor.execute(sql,val)
db.commit()
cursor.close()
db.close()
@scrimmod.command()命令
异步定义角色(self、ctx、角色:discord.role):
如果ctx.message.author.guild\u permissions.manage\u消息:
db=sqlite3.connect('main.sqlite')
cursor=db.cursor()
cursor.execute(f“从主界面选择角色,其中guild_id={ctx.guild.id}”)
结果=cursor.fetchone()
如果结果为无:
sql=(“插入到主(公会id,角色)值(?,)中”)
val=(ctx.guild.id,角色)
wait ctx.send(f“正确注册时提供的默认角色已设置为`{role}`”)
elif结果不是无:
sql=(“更新主集合角色=?其中公会id=?”)
val=(角色,ctx.guild.id)
wait ctx.send(f“正确注册时提供的默认角色已更新为`{role}`”)
cursor.execute(sql,val)
db.commit()
cursor.close()
db.close()
def设置(机器人):
bot.add_cog(ScrimLog(bot))
打印(“已加载Scrims cog!”)

从现在起,我认为问题在于消息部分的
频道id
频道id
角色
未定义,但即使我定义了它们,它仍然不起作用。

f-string可能会导致问题,试试这个

cursor.execute("SELECT channel_id FROM main WHERE guild_id = ?", [message.guild.id])

首先,如果在
on_message
事件下连接到数据库,这会降低bot的效率。对于其他问题,您必须像下面这样更改命令参数

异步def通道(self,ctx,channel):
异步def角色(self,ctx,role):

而不是这些
角色:discord.role
频道:discord.TextChannel

然后,当您想调用频道或角色时,必须使用
discord.utils.get

对于频道:

wait ctx.send(f“默认注册频道已更新为{discord.utils.get(ctx.guild.text_channels,name=Channel).title}”)

对于角色:

wait ctx.send(f“正确注册时提供的默认角色已更新为{discord.utils.get(ctx.guild.roles,name=role)}”)

因此,当用户使用
角色
频道
命令时,他们必须编写准确的频道

当您将频道id与数据库进行比较时,还可以使用
discord.utils.get
如下所示:

channel\u in\u database=discord.utils.get(ctx.guild.text\u channels,name=“在这里,您需要连接到数据库并获取此公会的频道名称。”)

然后,
如果不是chanel\u in_database.id==ctx.channel.id:
,或者您可以(我不是100%确定这会起作用)从该公会的数据库中获取频道名称并执行以下操作:

if“来自数据库的频道名称”==ctx.channel.name


如果其中一个出现了任何问题,或者您仍然有未回答的问题,只需评论一下

问题是什么?我不知道如何定义公会id、频道id,然后如何将它们与数据库中的id匹配,消息部分会抛出所有错误,on_消息的语法很短@nurqmI不确定您想用这段代码做什么。你想给提到3个人的.用户指定一个特定的角色吗?这将适用于每个公会?你为什么这么想