无法弄清楚如何让React角色与discord.py中的示例一起工作

问题描述 投票:0回答: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)

有人知道为什么反应角色不起作用吗?骰子滚筒运转良好

我已经尝试了here找到的原始代码的各种变体,包括仅创建一个新应用程序并为其提供所有正确的权限,然后尝试这样做,但它仍然无法工作

python discord discord.py
1个回答
0
投票

您似乎希望在将反应添加到消息时发生一些事情,并且这个“某事”应该在 on_raw_reaction_add 函数的范围内定义。 目前只返回 None ,不执行任何其他操作。

© www.soinside.com 2019 - 2024. All rights reserved.