我的机器人没有提供角色,但它没有发送错误

时间:2021-03-27 08:03:17

标签: python discord discord.py

(对不起我的英语)我的机器人必须通过对表情符号的反应来发挥作用,但它没有这样做。但它不会发送错误消息。请帮忙。 我的代码:

import discord
from discord.ext import commands
from discord.utils import get

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 Moji.add_reaction('?')
@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 user.add_roles(Role)
    
client.run("My token")

1 个答案:

答案 0 :(得分:0)

您必须使用名为 on_raw_reaction_add 的事件。您可能需要阅读Docs

现在进入进一步的程序:

  1. 定义事件:
    @client.event
    async def on_raw_reaction_add(payload): # We use payload
  1. 定义角色:
guild = client.get_guild(payload.guild_id) # Define the guild
member = get(guild.members, id=payload.user_id) # Get the member from the guild
# channel and message IDs should be integer:
if payload.channel_id == Channel_ID and payload.message_id == Message_ID: # Inser your ID's
    if str(payload.emoji) == "YourEmoji":
        role = get(payload.member.guild.roles, name='RoleName') # ID also possible.
  1. 添加角色:
if role is not None:
    await payload.member.add_roles(role)

所以你的整个代码将是:

@client.event
async def on_raw_reaction_add(self, payload):
    guild = client.get_guild(payload.guild_id)
    member = get(guild.members, id=payload.user_id)
    if payload.channel_id == Channel_ID and payload.message_id == Message_ID:
        if str(payload.emoji) == "YourEmoji":
            role = get(payload.member.guild.roles, name='RoleName') # Or id='RoleID'
        else:
            role = get(guild.roles, name=payload.emoji)
        if role is not None:
            await payload.member.add_roles(role) # Add the role

要添加更多角色,只需使用 elif 语句。

我们在做什么?

  • 向定义的频道发送消息(Channel_ID)
  • 当有人用定义的表情符号对消息做出反应时添加角色