代码之家  ›  专栏  ›  技术社区  ›  AutumnSkies

无法弄清楚如何让react角色使用confirm.py中的示例

  •  0
  • AutumnSkies  · 技术社区  · 1 年前

    因此,我正在为我的dnd和lancer团队编写一个机器人,完全没有经验。我主要是将示例代码串在一起,然后使其工作。我目前正忙于获取反应角色的示例代码,以完整地看到它 here

    我当前的代码是这样的

    # This example requires the 'members' and 'message_content' privileged intents to function.
    
    import discord
    from discord.ext import commands
    import random
    import logging
    
    description = '''Godawful bot programmed by my discord username
    
    Primary use case is rolling dice and helping with scheduling for sessions. Also supposed to do reaction roles'''
    
    intents = discord.Intents.default()
    intents.members = True
    intents.message_content = True
    
    bot = commands.Bot(command_prefix='p;', description=description, intents=intents)
    handler = logging.FileHandler(filename='platform.log', encoding='utf-8', mode='w')
    
    
    @bot.event
    async def on_ready():
        print(f'Logged in as {bot.user} (ID: {bot.user.id})')
        print('------')
    
    @bot.command()
    async def roll(ctx, dice: str):
        """Rolls a dice in NdN format."""
        try:
            rolls, limit = map(int, dice.split('d'))
        except Exception:
            await ctx.send('ERROR:Syntax - Format has to be in NdN!')
            return
    
        result = ', '.join(str(random.randint(1, limit)) for r in range(rolls))
        await ctx.send(result)
    
    class MyClient(discord.Client):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
            self.role_message_id = '1249373090727858259'  # ID of the message that can be reacted to to add/remove a role.
            self.emoji_to_role = {
                discord.PartialEmoji(name='🔴'): '1249516324603035689',  # ID of the role associated with unicode emoji '🔴'.
                discord.PartialEmoji(name='🟡'): '1249516335696973884',  # ID of the role associated with unicode emoji '🟡'.
                discord.PartialEmoji(name='green', id=0): 0,  # ID of the role associated with a partial emoji's ID.
            }
    
        async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
            """Gives a role based on a reaction emoji."""
            # Make sure that the message the user is reacting to is the one we care about.
            if payload.message_id != self.role_message_id:
                return
    
    
    bot.run('bot key thing', log_handler=handler)
    

    有人知道为什么反应角色不起作用吗?骰子滚轮工作正常

    我尝试了发现的原始代码的各种变体 在这里 包括仅仅创建一个新应用程序并给予它所有正确的权限,然后尝试一下,它仍然无法工作

    1 回复  |  直到 1 年前
        1
  •  1
  •   ManOnSaturn    1 年前

    似乎你希望在消息中添加反应时发生一些事情,而这个“事情”应该在 on_raw_reaction_add 功能。 It's currently only returning None ,什么也不做。

        2
  •  0
  •   Matt    1 年前

    从您链接的示例中,您需要添加整个“on_raw_reaction_add”函数,其中最重要的部分是

    try:
        role_id = self.emoji_to_role[payload.emoji]
    except KeyError:
        # If the emoji isn't the one we care about then exit as well.
        return
    
    role = guild.get_role(role_id)
    if role is None:
        # Make sure the role still exists and is valid.
        return
    
    try:
        # Finally, add the role.
        await payload.member.add_roles(role)
    except discord.HTTPException:
        # If we want to do something in case of errors we'd do it here.
        pass