Я программирую бот-дискорд, который дает пользователям роль, когда они реагируют с эмодзи под сообщением.(Роль реакции)
Если я создаю роль реакции с помощью команды !actadd, бот сохраняет ее, и я снова могу удалить запись в базе данных с помощью команды !actremove.
Ноесли пользователь реагирует с эмодзи под сообщением, то роль не получает пользователь.
Вместо этого я получаю следующее сообщение об ошибке:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\Kai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "E:\dev\discord_bot\cogs\reactrole.py", line 24, in on_raw_reaction_add
role = discord.utils.get(guild.roles, id=int(result[1]['role']))
IndexError: list index out of range
Та же проблема существует длястроки 34, 52 и 62. Бот не находит запись в базе данных.И я не знаю почему.
полный исходный код:
import re
import discord
from discord.ext import commands
class Reactrole(commands.Cog, name='Reactrole'):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_raw_reaction_add(self, reaction):
guild = self.bot.get_guild(reaction.guild_id)
user = guild.get_member(reaction.user_id)
if user == self.bot.user:
return
elif not user == self.bot.user:
if '<:' in str(reaction.emoji):
guild_id = str(reaction.guild_id)
message_id = str(reaction.message_id)
emoji_id = str(reaction.emoji.id)
result = await self.bot.pg_con.fetch("SELECT emoji, role, message_id, channel_id FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emoji_id)
if result:
role = discord.utils.get(guild.roles, id=int(result[1]['role']))
await user.add_roles(role)
else:
return
elif '<:' not in str(reaction.emoji):
guild_id = str(reaction.guild_id)
message_id = str(reaction.message_id)
emoji_id = str(reaction.emoji)
result = await self.bot.pg_con.fetch("SELECT emoji, role, message_id, channel_id FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emoji_id)
if result:
role = discord.utils.get(guild.roles, id=int(result[1]['role']))
await user.add_roles(role)
else:
return
@commands.Cog.listener()
async def on_raw_reaction_remove(self, reaction):
guild = self.bot.get_guild(reaction.guild_id)
user = guild.get_member(reaction.user_id)
if user == self.bot.user:
return
elif not user == self.bot.user:
if '<:' in str(reaction.emoji):
guild_id = str(reaction.guild_id)
message_id = str(reaction.message_id)
emoji_id = str(reaction.emoji.id)
result = await self.bot.pg_con.fetch("SELECT emoji, role, message_id, channel_id FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emoji_id)
if result:
role = discord.utils.get(guild.roles, id=int(result[1]['role']))
await user.remove_roles(role)
else:
return
elif '<:' not in str(reaction.emoji):
guild_id = str(reaction.guild_id)
message_id = str(reaction.message_id)
emoji_id = str(reaction.emoji)
result = await self.bot.pg_con.fetch("SELECT emoji, role, message_id, channel_id FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emoji_id)
if result:
role = discord.utils.get(guild.roles, id=int(result[1]['role']))
await user.remove_roles(role)
else:
return
@commands.command()
@commands.has_permissions(administrator=True)
async def reactadd(self, ctx, channel: discord.TextChannel, messageid, emoji, role: discord.Role):
guild_id = str(ctx.guild.id)
message_id = str(messageid)
role_id = str(role.id)
channel_id = str(channel.id)
emoji_id = str(emoji)
result = await self.bot.pg_con.fetch("SELECT emoji, role, message_id, channel_id FROM reactrole WHERE guild_id = $1 and message_id = $2", guild_id, message_id)
if '<:' in emoji:
emm = re.sub(':.*?:', '', emoji).strip('<>')
if not result:
await self.bot.pg_con.execute('INSERT INTO reactrole(emoji, role, message_id, channel_id, guild_id) VALUES($1, $2, $3, $4, $5)', emm, role_id, message_id, channel_id, guild_id)
msg = await channel.fetch_message(message_id)
em = self.bot.get_emoji(int(emm))
await msg.add_reaction(em)
await ctx.send(embed=discord.Embed(color=discord.Color.green(), description=f"Reactrole wurde erfolgreich für die Rolle {role.mention} eingerichtet!"))
elif result:
if not emm in str(result[0]['emoji']) and role_id in str(result[1]['role']):
await self.bot.pg_con.execute('INSERT INTO reactrole(emoji, role, message_id, channel_id, guild_id) VALUES($1, $2, $3, $4, $5)', emm, role_id, message_id, channel_id, guild_id)
msg = await channel.fetch_message(message_id)
em = self.bot.get_emoji(int(emm))
await msg.add_reaction(em)
await ctx.send(embed=discord.Embed(color=discord.Color.green(), description=f"Reactrole wurde erfolgreich für die Rolle {role.mention} eingerichtet!"))
else:
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f'Der React {emoji} oder die Rolle {role.mention} existieren bereits als Reactrole an dieser Nachricht!'))
elif '<:' not in emoji:
if not result:
await self.bot.pg_con.execute('INSERT INTO reactrole(emoji, role, message_id, channel_id, guild_id) VALUES($1, $2, $3, $4, $5)', emoji_id, role_id, message_id, channel_id, guild_id)
msg = await channel.fetch_message(message_id)
await msg.add_reaction(emoji)
await ctx.send(embed=discord.Embed(color=discord.Color.green(), description=f"Reactrole wurde erfolgreich für die Rolle {role.mention} eingerichtet!"))
elif result:
if not emoji_id in str(result[0]['emoji']) and role_id in str(result[1]['role']):
await self.bot.pg_con.execute('INSERT INTO reactrole(emoji, role, message_id, channel_id, guild_id) VALUES($1, $2, $3, $4, $5)', emoji_id, role_id, message_id, channel_id, guild_id)
msg = await channel.fetch_message(message_id)
await msg.add_reaction(emoji)
await ctx.send(embed=discord.Embed(color=discord.Color.green(), description=f"Reactrole wurde erfolgreich für die Rolle {role.mention} eingerichtet!"))
else:
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f'Der React {emoji} oder die Rolle {role.mention} existieren bereits als Reactrole an dieser Nachricht!'))
@commands.command()
@commands.has_permissions(administrator=True)
async def reactremove(self, ctx, messageid, emoji):
guild_id = str(ctx.guild.id)
message_id = str(messageid)
emoji_id = str(emoji)
if '<:' in emoji:
emm = re.sub(':.*?:', '', emoji).strip('<>')
result = await self.bot.pg_con.fetch("SELECT emoji, role, message_id, channel_id FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emm)
if not result:
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f'Diese Reaction wurde nicht an der Nachricht gefunden!'))
elif emm in str(result[0]['emoji']):
await self.bot.pg_con.execute("DELETE FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emm)
await ctx.send(embed=discord.Embed(color=discord.Color.green(), description=f'Reactrole wurde für den React {emoji} gelöscht!'))
else:
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f'Diese Reaction wurde nicht an der Nachricht gefunden!'))
elif '<:' not in emoji:
result = await self.bot.pg_con.fetch("SELECT emoji, role, message_id, channel_id FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emoji_id)
if not result:
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f'Diese Reaction wurde nicht an der Nachricht gefunden!'))
elif emoji_id in str(result[0]['emoji']):
await self.bot.pg_con.execute("DELETE FROM reactrole WHERE guild_id = $1 and message_id = $2 and emoji = $3", guild_id, message_id, emoji_id)
await ctx.send(embed=discord.Embed(color=discord.Color.green(), description=f'Reactrole wurde für den React {emoji} gelöscht!'))
else:
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f'Diese Reaction wurde nicht an der Nachricht gefunden!'))
def setup(bot):
bot.add_cog(Reactrole(bot))