繁体   English   中英

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

[英]My bot doesn't give role, but it doesn't send error

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

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")

您必须使用名为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)
  • 当有人用定义的表情符号对消息做出反应时添加一个角色

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM