P
P
Punishment2021-03-17 00:09:19
Python
Punishment, 2021-03-17 00:09:19

My code doesn't work but doesn't throw a discord py error?

My bot should issue roles by reacting to emoji, but it doesn't work. However, no error is displayed in the console. Here is my code:

import discord
from discord.ext import commands
import json

client = commands.Bot(command_prefix = ".")

@client.event
async def on_raw_reaction_add(payload):
  if payload.member.bot:
    pass
  else:
    with open('reactrole.json') as react_file:
      data = json.load(react_file)
      for x in data:
        if x['pubg_emoji'] == payload.emoji.name:
          
          role = discord.utils.get(client.get_guild(payload.guild_id).roles, id=x['pubg_id'])

          await payload.member.add_roles(role)

@client.command()
async def reactrole(ctx,emoji,role : discord.Role,*,message):
  emb = discord.Embed(description = message)
  msg = await ctx.channel.send(embed = emb)
  await msg.add_reaction (emoji)
  
client.run("My token")

File code:
["pubg_emoji":":pubg:","pubg_id": 817371069547020308]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-03-17
@SoreMix

File ID:
["pubg_emoji":":pubg:","pubg_id": 817371069547020308]

It can't be, it's an invalid format.
Even if we assume that this is a dictionary, it's still
for x in data:
        if x['pubg_emoji'] == payload.emoji.name:

will throw an error

P
Punishment, 2021-03-20
@Punishment

I tried to fix it but now the error is different:

line 13, in on_ready
    await client.add_reaction(Moji, emoji='')
AttributeError: 'Bot' object has no attribute 'add_reaction'

This horror has been going on for a month, please help
me. Here is my code:
import discord
from discord.ext import commands

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

@client.event
async def on_ready():
  print(discord.__version__)
  
  Channel = client.get_channel(815949348948934716)
  Text= "Выбери свою роль"
  Moji = await Channel.send(Text)
  await client.add_reaction(Moji, emoji='')
@client.event
async def on_reaction_add(reaction, user):
  
  Channel = client.get_channel(815949348948934716)
  if reaction.message.channel.id != Channel:
    return
  if reaction.emoji == "":
    
    Role = discord.utils.get(user.server.roles, name="PUBG")
    await client.add_roles(user, Role)
    
client.run("My token")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question