-1
@client.event
async def on_raw_reaction_add(payload):
    print("Reaction detected")
    print(f"payload.user_id: {str(payload.user_id)}")
    print(f"payload.message_id: {str(payload.message_id)}")
    print(f"payload.emoji.name: {str(payload.emoji.name)}")
    guild = discord.utils.find(lambda g: g.id == payload.guild_id, client.guilds)
    
    if payload.emoji.name == "" and payload.message_id == 1095071488282992831:
        role = discord.utils.get(guild.roles, id=1094731340383002694)
        member = guild.get_member(payload.user_id)
        await member.add_roles(role) #line 72

I'm getting the error on Line 72 saying 'NoneType' object has no attribute 'add_roles'. I know this means the 'member' variable somehow failed, but I don't have a clue how it failed. The output also printed this:

Reaction detected
payload.user_id: 692156420895801495
payload.message_id: 1095071488282992831
payload.emoji.name: 

Can someone help?

Yozy Opto
  • 23
  • 5

1 Answers1

0

In order for you to get members from a server using guild.get_member they need to be stored in the bot's cache. To make this happen, enable the members intent:

from discord.ext import commands
import discord

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_raw_reaction_add(payload):
    print("Reaction detected")
    print(f"payload.user_id: {payload.user_id}")
    print(f"payload.message_id: {payload.message_id}")
    print(f"payload.emoji.name: {payload.emoji}")
    if payload.message_id == 1095071488282992831 and str(payload.emoji) == "":
        guild = bot.get_guild(payload.guild_id)
        role = guild.get_role(1094731340383002694)
        member = guild.get_member(payload.user_id)
        await member.add_roles(role)
Hazzu
  • 1,051
  • 1
  • 4
  • 8
  • Traceback (most recent call last): File "main.py", line 10, in intents = discord.Intents.default() AttributeError: module 'discord' has no attribute 'Intents' – Yozy Opto Apr 12 '23 at 01:01
  • ``` import os import discord from keep_alive import keep_alive from discord.ext import commands, tasks import logging import time from datetime import datetime import pytz intents = discord.Intents.default() intents.members = True # When IP abuse occurs, type "kill 1" in the Shell tab. logging.basicConfig( format="%(asctime)s - %(name)s - %(message)s", level=logging.INFO, ) client = commands.Bot(command_prefix="!!", intents=intents) ``` This is the top of my code btw – Yozy Opto Apr 12 '23 at 01:01
  • You will need to update your discord.py version. Run the command `pip install discord.py --upgrade` – Hazzu Apr 12 '23 at 01:27
  • If you don't know how to upgrade a python library, check out some docs on the internet, like this one: https://blog.finxter.com/easy-way-to-update-a-python-package-with-pip-upgrade/ – Hazzu Apr 12 '23 at 01:29
  • File "", line 1 pip install discord.py --upgrade SyntaxError: invalid syntax – Yozy Opto Apr 12 '23 at 01:43
  • I think the mistake is that you are running that code in the python shell instead of the Command Prompt. Try running that same code in Command Prompt/Terminal and the package should install. – Hazzu Apr 12 '23 at 03:33
  • How did you install discord.py? The process to update is similar – Hazzu Apr 12 '23 at 03:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253113/discussion-between-yozy-opto-and-hazzu). – Yozy Opto Apr 12 '23 at 14:50