对表情符号 discord.py 做出反应时添加角色

问题描述 投票:0回答:4

我试图在用户对这个表情符号做出反应时添加一个角色,但我无法得到

设置角色


@bot.command()
async def roles(ctx):
    global IDMessage
    reaction   = await ctx.reply("Select your Game" + '\n' + '\n' + "- Valorant 🦸‍♀️" '\n' + "- World of Warcraft ⚔" + '\n' + "- League of Legends 🧙‍♀️" + '\n' + "- Cs:Go 🔫")
    

    await reaction.add_reaction('🦸‍♀️')
    await reaction.add_reaction('⚔')
    await reaction.add_reaction('🧙‍♀️')
    await reaction.add_reaction('🔫')
    
    IDMessage = reaction.message_id

这是对我不起作用的代码部分

添加反应角色

@bot.event
async def on_reaction_add(reaction, user):
  ChID = '920320584032870424'
  if reaction.message.channel.id != ChID:
    return
  if reaction.emoji == "🦸‍♀️":
    Valorant = discord.utils.get(user.server.roles, name="Valorant")
    await bot.add_roles(user, Valorant)
python discord.py roles emoji
4个回答
2
投票

ChID =

'920320584032870424'
(字符串)正在与整数
reaction.message.channel.id
.

进行比较
  ChID = '920320584032870424'

相反,使用

  ChID = 920320584032870424

这是一个整数。


1
投票

这里有一些问题。而不是

on_reaction_add
,使用
on_raw_reaction_add
。大多数机器人使用后者而不是前者。 为什么你会问?因为,
on_reaction_add
仅适用于机器人缓存中的消息。

因此,在您的机器人可以读取消息后发送的每条消息都存储在字典或列表中(这就是缓存)。默认情况下缓存的限制是

1000
消息。

所以如果你重新启动了你的机器人,消息将永远不会在缓存中,因此事件不会触发。

另一个问题是您正在比较字符串 (

ChID
) 变量和整数 (
reaction.message.channel.id
),它们永远不会相等,因此
if reaction.message.channel.id != ChID
将始终返回 True,因此不会执行任何操作(当您返回该条件为真)。

第三个问题是

bot.add_roles
不存在。
add_roles
discord.Member
对象的一个方法。

第四期是,

Member.server
不是东西,
Member.guild

所以你更新后的代码会是这样的:

@bot.event
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
  ChID = 920320584032870424
  if payload.channel.id != ChID:
    return
  if str(payload.emoji) == "🦸‍♀️":
    valorant = discord.utils.get(payload.member.guild.roles, name="Valorant")
    await payload.member.add_roles(valorant)

还有我不使用的个人建议

global
在极少数情况下,您实际上需要这个关键字

但是你不太可能在用

discord.py
lib

制作的机器人中使用它

更多关于这个:


0
投票

我遇到了同样的问题,我就是这样解决的。

你已经添加了反应,现在你需要等待用户反应。

为此,您可以使用 discord.py 的 wait_for 函数。

检查功能:

def check(reaction):
    if str(reaction.emoji) == “EMOJIHERE”:
        return “emoji name”
    elif str(reaction.emoji) == “SECONDEMOJI”:
        return “emoji name”
    elif str(reaction.emoji) == “THIRDEMOJI”:
        return “emoji name”
    elif str(reaction.emoji) == “FOURTHEMOJI”:
        return “emoji name”
    else:
        return “error”


代码:


async def roles(ctx):
    global IDMessage
    reaction   = await ctx.reply("Select your Game" + '\n' + '\n' + "- Valorant 🦸‍♀️" '\n' + "- World of Warcraft ⚔" + '\n' + "- League of Legends 🧙‍♀️" + '\n' + "- Cs:Go 🔫")
    

    await reaction.add_reaction('🦸‍♀️')
    await reaction.add_reaction('⚔')
    await reaction.add_reaction('🧙‍♀️')
    await reaction.add_reaction('🔫')
    
    confirm = await bot.wait_for(“reaction_add”, check=check)
    if confirm == "emoji name 1":
        doSomething()
    elif confirm == "emoji name 2":
        doSomething()
    elif confirm == "emoji name 3":
        doSomething()
    elif confirm == "emoji name 4":
        doSomething()
    else:
        print("Error")
        await ctx.send("An error occurred.")


用适当的值替换 EMOJIHERE 和表情符号名称。


-1
投票
##Embedd##

@client.command()
@commands.has_permissions(administrator=True)
async def roles(ctx):
        embed = discord.Embed(title="Server Roles", description="roles you can pick", color = discord.Colour.green())
        embed.add_field(name="**NOTIFICATIONS**", value="🟡\n", inline=True)
        embed.add_field(name="**STREAMER**", value="🟣\n", inline=True)
        embed.set_footer(text="Bot created by @FreakSheep#3267")
        embed.set_author(name='Flockersbot', icon_url='https://cdn.discordapp.com/attachments/898921206420484108/1066931617643364433/Flockers_Bot.png')
        embed.set_thumbnail(url='https://cdn.discordapp.com/attachments/898921206420484108/1066931617643364433/Flockers_Bot.png')
        msg = await ctx.send(embed=embed)
        msg = await msg.fetch() 
        await msg.add_reaction('🟡')
        await msg.add_reaction('🟣')

##Role React##

 if payload.emoji.name == "🟣" and payload.message_id == 1066863494475034644:
        role = discord.utils.get(guild.roles, name="Streamer")
        if role is not None:
            member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members)
            await member.add_roles(role)
© www.soinside.com 2019 - 2024. All rights reserved.