P
P
Punishment2021-03-29 09:57:48
Python
Punishment, 2021-03-29 09:57:48

UnboundLocalError: local variable 'role' referenced before assignment how to fix discord py error?

I'm trying to create a bot that will give out a role by reacting to an emoji. But when I click on react I get an error

line 28, in on_raw_reaction_add
    if role is not None:
UnboundLocalError: local variable 'role' referenced before assignment

Please help
me here is my code:
import logging

import discord
from discord.ext import commands
from discord.utils import get

client = commands.Bot(command_prefix = ".",intents = discord.Intents.all())

@client.event
async def on_ready():
  print(discord.__version__)

@client.event
async def on_raw_reaction_add(payload):
  
  guild = client.get_guild(payload.guild_id)
  member = get(guild.members, id=payload.user_id)
  
  #role = 0
  Channel_ID = 815951876708892672
  Message_ID = 825491143315030048
  
  if payload.channel_id == Channel_ID and payload.message_id == Message_ID:
    
    if str(payload.emoji) == ":pubg:":
      
      role = get(payload.member.guild.roles, id = 817371069547020308) 
    if role is not None:
      
      await payload.member.add_roles(role)

client.run("My token")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2021-03-29
@Punishment

Set up indents correctly.

M
mkone112, 2021-03-29
@mkone112

Lord, learn python already. What's the point of writing a bot if you don't know Japanese? Same type, elementary questions, one after another...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question