title: discord bot -- IndexError: list index out of range
body: <p>I program a discord bot that gives users a role when they react with an emoji under a message. (Reaction Role)</p>
  
  <p>If I create a reaction role with the command !reactadd, then the bot saves it and I can delete the entry in the database again with the command !reactremove.</p>
  
  <p>But if a user reacts with the emoji under the message, then the user doesn't get the role.</p>
  
  <p>Instead, I get the following error message:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The same problem exists for lines 34, 52 and 62. The bot doesn't find the database entry.
  And I don't know why.</p>
  
  <p>the complete source code:</p>
  
  <pre><code>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 '&lt;:' 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 '&lt;:' 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 '&lt;:' 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 '&lt;:' 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 '&lt;:' in emoji:
              emm = re.sub(':.*?:', '', emoji).strip('&lt;&gt;')
              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 '&lt;:' 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 '&lt;:' in emoji:
              emm = re.sub(':.*?:', '', emoji).strip('&lt;&gt;')
              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 '&lt;:' 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))
  </code></pre>