0

I'm new to discord.py and I tried to send a private message with my bot when I react with the 'bro_emoji' to a specific message :

@bot.event
async def on_raw_reaction_add(payload):

    emoji = payload.emoji.name
    canal = payload.channel_id
    message = payload.message_id
    roles = bot.get_guild(payload.guild_id).roles
    bro_role = get(roles, name="Bro")
    member = bot.get_guild(payload.guild_id).get_member(payload.user_id)

    if emoji == "bro_emoji" and canal == 920004461575954472 and message == 920004958923939901:
        print('Role acquired')
        await member.add_roles(bro_role) # Issue here
        await member.send("Bro role acquired") # Issue here

There are the errors :

AttributeError: 'NoneType' object has no attribute 'add_roles'

AttributeError: 'NoneType' object has no attribute 'send'

Thanks :D <3

2 Answers2

0

Try creating the variables that start by bot.get_ in the on_ready() function and just making it global:

global member

member = bot.get_guild(payload.guild_id).get_member(payload.user_id)

Because if the bot isn't officially logged in the bot.get_ functions return None. And the on_ready() function gets activated only when the bot is officially logged in.

Nickname
  • 1
  • 3
0

To piggyback off of Nickname's answer as it took me a moment to figure out what they meant, you need to put the "global member" in your on_ready method. Make sure your on_ready is inside the same file where you initialize the "bot" instance

bot = commands.Bot("<insert prefix here>", intents=<intents>)


@bot.event
async def on_ready()
    global member


@bot.event
async def on_raw_reaction_add(payload):
    emoji = payload.emoji.name
    canal = payload.channel_id
    message = payload.message_id
    roles = bot.get_guild(payload.guild_id).roles
    bro_role = get(roles, name="Bro")
    member = bot.get_guild(payload.guild_id).get_member(payload.user_id)

    if emoji == "bro_emoji" and canal == 920004461575954472 and message == 920004958923939901:
        print('Role acquired')
        await member.add_roles(bro_role) # Issue here
        await member.send("Bro role acquired") # Issue here