【发布时间】:2020-12-18 01:31:00
【问题描述】:
我创建了一个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!")
从现在开始,我认为问题出在on_message 部分,channel.id、channel-id、role 未定义,但即使我定义了它们,它仍然不起作用。
【问题讨论】:
-
有什么问题?
-
我不知道如何定义公会ID,频道ID,然后如何将它们与数据库中的ID匹配,on_message部分抛出所有错误,on_message的简短语法就是所有搞砸了@nurqm
-
我不确定你想用这段代码做什么。你想给提到 3 个人的用户赋予一个特定的角色,这将适用于每个公会吗?为什么需要数据库?
-
是的,你是对的,我想为每个提到三个人的用户赋予角色,我需要 db,因为只有当用户在特定频道中提及时才会赋予角色,并且还会赋予特定角色。因此,我必须记录机器人将为该特定公会提供的消息所扮演的角色。 @nurqm
标签: python sqlite discord discord.py