您好,我的机器人在多个行业中,当用户在特定频道中提及任何一位用户时,我希望它发挥特定的作用,我选择了sqlite3作为工作,并写下了这一点,
结构:
@bot.event
async def on_ready():
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS main(
guild_id TEXT,
channel_id TEXT,
role_id TEXT
)
''')
齿轮:
import discord
from discord.ext import commands
import discord.utils
from discord.utils import get
import asyncio
import datetime
import sqlite3
class TestCog(commands.Cog, name='test') :
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) >= 1:
await message.add_reaction(emoji="<a:tick:748476262640779276>")
user = message.author
await user.add_roles(role)
@commands.group(invoke_without_command=True)
async def test(self,ctx):
await ctx.send('Test command running...')
@test.command()
async def channel(self, ctx, channel:discord.TextChannel):
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'Test 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'Test channel has been updated to {channel.mention}')
cursor.execute(sql, val)
db.commit()
cursor.close()
db.close()
@test.command()
async def role(self, ctx,role: discord.Role):
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT role_id FROM main WHERE guild_id = {ctx.guild.id}")
result = cursor.fetchone()
if result is None:
sql = ("INSERT INTO main (guild_id, role_id) VALUES(?,?)")
val = (ctx.guild.id, role.id)
await ctx.send(f" Role set to `{role}`")
elif result is not None:
sql = ("UPDATE main SET role_id = ? WHERE guild_id = ?")
val = (role.id, ctx.guild.id)
await ctx.send(f"Default role have been updated to `{role}`")
cursor.execute(sql, val)
db.commit()
cursor.close()
db.close()
async def cog_command_error(self, ctx, error):
raise error
def setup(bot):
bot.add_cog(TestCog(bot))
print("Test cog is loaded!")
表ss显示结构:
我已经检查了角色命令channel命令是否正常工作并将数据正确存储在db中,问题出在on_message
上。它不会引发错误,但是如果用户提到某人则不起作用。我真的无法弄清楚。您可以在on_message部分或代码的任何部分看到任何错误吗?