[Discord.py] How to add or remove job titles after a reaction [python]

I'm a little ridiculous, so I'll make a note of it.

If you give a specific reaction to a specific message, add a role

@client.event
async def on_raw_reaction_add(payload):
    if payload.message_id ==Message ID you want to have a reaction:
        checked_emoji = payload.emoji.id

        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
        if checked_emoji ==Reaction ID:
            role = guild.get_role(Job title ID)
            await payload.member.add_roles(role)

If you erase the reaction, the role will disappear

@client.event
async def on_raw_reaction_remove(payload):
    if payload.message_id ==Message ID you want to have a reaction:
        checked_emoji = payload.emoji.id

        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
        if checked_emoji ==Reaction ID:
            role = guild.get_role(Job title ID)
            member = guild.get_member(payload.user_id)
            await member.remove_roles(role)

Where I was addicted

For those who have a job title

            await payload.member.add_roles(role)

I can go, but the one to erase is

            member = guild.get_member(payload.user_id)
            await member.remove_roles(role)

You need to get member once.

If a reaction is added, member will be included in payload, but when the reaction is deleted, member does not exist.

https://discordpy.readthedocs.io/en/latest/api.html?highlight=reaction%20remove#discord.RawReactionActionEvent.member

            await payload.member.remove_roles(role)

And write

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

I hope that people who are angry and angry will find a solution after seeing this article.

Recommended Posts

[Discord.py] How to add or remove job titles after a reaction [python]
How to add a Python module search path
How to write a Python class
How to make a string into an array or an array into a string in Python
How to remove duplicates from a Python list while preserving order.
How to add a package with PyCharm
[Python] How to make a class iterable
[Python] How to invert a character string
Add a Python virtual environment to VSCode
How to run a Maya Python script
How to format a list of dictionaries (or instances) well in Python
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to open a web browser from python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
To add a module to python put in Julialang
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
[Go] How to write or call a function
How to remove duplicate elements in Python3 list
[Python] How to remove duplicate values from the list
How to convert / restore a string with [] in python
[Python] [Django] How to use ChoiceField and how to add options
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib
How to set up a Python environment using pyenv
How to measure processing time in Python or Java
[Python] How to expand variables in a character string
How to write a list / dictionary type of Python3
How to build a Django (python) environment on docker
How to make a Python package using VS Code
[Python] How to write a docstring that conforms to PEP8
How to add page numbers to PDF files (in Python)
How to save a table scraped by python to csv
[Morphological analysis] How to add a new dictionary to Mecab
How to build a Python environment on amazon linux 2
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
[Python] How to draw a scatter plot with Matplotlib
How to install Python
How to install python
[Django] A pattern to add related records after creating a record
How to install NPI + send a message to line with python
[Python] How to make a list of character strings character by character
How to build a new python virtual environment on Ubuntu
How to convert an array to a dictionary with Python [Application]
How to run a Python file at a Windows 10 command prompt
How to run a Python program from within a shell script
How to shuffle a part of a Python list (at random.shuffle)
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to launch AWS Batch from a python client app
How to import a file anywhere you like in Python
How to specify a public directory Python simple HTTP server
How to transpose a 2D array using only python [Note]
How to make a Python package (written for an intern)
[Python Kivy] How to create a simple pop up window
I tried "How to get a method decorated in Python"
How to get a list of built-in exceptions in python