# Discord Bot#4 Reaction Role
---
## 前置作業
----
### 幫機器人創一個身分組
- 記得把管理身分組權限打開
![](https://i.imgur.com/OrrHpUn.png)
----
### 創一個你要自動新增的身分組
- bot的身分組一定要在這個身分組上面
![](https://i.imgur.com/drAaam8.png)
----
### 上傳一個表情符號(Optional)
----
## 在Cog資料夾底下新增role. py
```python=
import discord
from discord.ext import commands
class Role(commands.Cog):
def __init__(self, bot):
self.bot = bot
def setup(bot):
bot.add_cog(Role(bot))
```
---
## Reaction
- on_reaction_add vs on_raw_reaction_add
----
### Sum up
- on_reaction_add: 重開機器人之後的發出的訊息有效
- on_raw_reaction_add: 只要有人點reaction就會觸發
----
### emoji格式
- 自定義
- 內建(Unicode)
----
### 自定義emoji
在輸入欄打一個表情符號,之後在前面加上一個反斜線,按下enter,就會顯示自定義emoji的格式
- 格式類似這樣 <:emoji_name:emoji_id>
<:energy_emmo:953329973702316072>
----
### 內建emoji
要去查你要的表情的Unicode
https://unicode.org/emoji/charts/full-emoji-list.html#1f5fb
```
Example:
Thinking emoji的Unicode是U+1F914,在Python裡是用\U0001F914表示
```
----
### Example
```python=
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
# if str(payload.emoji) == '<:energy_emmo:953329973702316072>':
if str(payload.emoji) == '\U0001F914':
print('TEST')
```
---
## Role
----
### add_roles
1. 從payload中抓到發出訊息的伺服器(guild)
2. 從這個伺服器物件用role_id抓到我們想要加的身分組
3. 把這個按表情的成員新增一個身分組
----
### 取得role_id
伺服器設定->身分組->右鍵身分組->複製id
![](https://i.imgur.com/6W2dnIR.png)
----
### Example
```python=
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if str(payload.emoji) == '\U0001F914':
guild = self.bot.get_guild(payload.guild_id)
role = guild.get_role(roleID)
member = await guild.fetch_member(payload.user_id)
await member.add_roles(role)
```
----
### 移除身分組也是一樣的作法
```python=
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
if str(payload.emoji) == '\U0001F914':
guild = self.bot.get_guild(payload.guild_id)
role = guild.get_role(roleID)
member = await guild.fetch_member(payload.user_id)
await member.remove_roles(role)
```
---
## 支援多個身分組
----
### Dictionary
- 把表情和對應的身分組id做成一個字典
```python=
roleDict = {'\U0001F914': 953334015652417596,
'<:energy_emmo:953329973702316072>': 953462476723347476}
```
----
### dict.items()
- 幫助我們把字典的key,val分開處理
https://docs.python.org/3/tutorial/datastructures.html#dictionaries
----
### Example
```python=
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
for emoji, roleID in roleDict.items():
if str(payload.emoji) == emoji:
guild = self.bot.get_guild(payload.guild_id)
role = guild.get_role(roleID)
member = await guild.fetch_member(payload.user_id)
await member.add_roles(role)
```
---
### 新增提示
```python=
@commands.command()
async def role(self, ctx):
msg = ''
for emoji, roleID in roleDict.items():
role = ctx.guild.get_role(roleID)
msg += f'{emoji} : {role}\n'
await ctx.send(msg)
msg_obj = await ctx.send(msg)
for emoji in roleDict:
await msg_obj.add_reaction(emoji)
```
{"metaMigratedAt":"2023-06-16T21:24:51.406Z","metaMigratedFrom":"YAML","title":"Discord Bot#4 Reaction Role","breaks":true,"contributors":"[{\"id\":\"e66eff6e-ecb8-470a-a384-ea2a02e04747\",\"add\":3911,\"del\":768}]"}