对用户角色的反应不起作用-discord.py

发布于 2025-01-10 19:14:07 字数 1200 浏览 1 评论 0原文

我只是用discord.py 为我的discord 服务器构建一个discord 机器人。 我写了一个代码,但它不起作用。它创建消息,您可以做出反应。甚至出现了第一条调试消息:用户对消息做出了反应。

但它不会给我这个角色。 这是我的代码:

async def reactMsg(ctx):

    send_sub = await ctx.message.channel.send("React to this message to subscribe NPT.")
    reactions = ['✅']
    for i in reactions:
        await send_sub.add_reaction(i)

@client.event
async def on_raw_reaction_add(payload):
    #You forgot to await the bot.get_channel
    channel = client.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    guild = client.get_guild(payload.guild_id)
    #Put the following Line
    member = guild.get_member(payload.user_id)
    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)

    print("Debug: User reacted to message")

    # only work if it is the client
    if payload.user_id == client.user.id:
        return

    if payload.message_id == 943634886642765874 and reaction.emoji == '✅':
        roles = discord.utils.get(guild.roles, name='TrackerSub')
        await member.add_roles(roles)
        await reaction.remove(payload.member)
        print("Debug: Give role to user")

有什么想法我做错了吗?

I'm just building a discord bot for my discord server with discord.py.
I made a code, but it won't work. It creates the message and you can react. Even the first debug message comes: User reacted to message.

But it won't give me the role.
This is my code:

async def reactMsg(ctx):

    send_sub = await ctx.message.channel.send("React to this message to subscribe NPT.")
    reactions = ['✅']
    for i in reactions:
        await send_sub.add_reaction(i)

@client.event
async def on_raw_reaction_add(payload):
    #You forgot to await the bot.get_channel
    channel = client.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    guild = client.get_guild(payload.guild_id)
    #Put the following Line
    member = guild.get_member(payload.user_id)
    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)

    print("Debug: User reacted to message")

    # only work if it is the client
    if payload.user_id == client.user.id:
        return

    if payload.message_id == 943634886642765874 and reaction.emoji == '✅':
        roles = discord.utils.get(guild.roles, name='TrackerSub')
        await member.add_roles(roles)
        await reaction.remove(payload.member)
        print("Debug: Give role to user")

Any ideas what I made wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠魁 2025-01-17 19:14:07

更新的代码

我发现有一些问题,让我们看一下您可以进行的一些更改,以使其变得更好:

async def react_msg(ctx):
    # Send a message, no need for `ctx.message.channel.send`, it's redundant.
    message = await ctx.send("React to this message to subscribe NPT.")
    reactions = ['✅']
    for i in reactions:
        await message.add_reaction(i)

@client.event
async def on_raw_reaction_add(payload):
    # We don't want to do operations if the bot is the one who reacted
    if payload.user_id == client.user.id: 
        return
    
    channel = client.get_channel(payload.channel_id)
    if channel is None: # This channel wasn't found
        return None
    
    guild = channel.guild
    if guild is None: # This reaction was not in a guild
        return
    
    member = payload.member
    if member is None:
        try:
            member = guild.get_member(payload.user_id) or (await guild.fetch_member(payload.user_id))
        except discord.NotFound: # This member wasn't found
            return None
    
    message = await channel.fetch_message(payload.message_id)
    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
    if reaction is None: # This reaction was not found
        return
    
    # The message ID is not right or the emoji was wrong
    if payload.message_id != 943634886642765874 or str(reaction.emoji) != '✅':
        return
    
    roles = discord.utils.get(guild.roles, name='TrackerSub')
    if roles is None: 
        # This role was not found, return.
        return
    
    await member.add_roles(roles)
    await reaction.remove(member)

请记住,

拥有良好的 on_command_error 对于 Discord 机器人开发非常重要。当调试这样的事情时,使用回溯总是一个好朋友。

Updated Code

I see a couple things wrong, let's walk through some changes you can make to make this a bit better:

async def react_msg(ctx):
    # Send a message, no need for `ctx.message.channel.send`, it's redundant.
    message = await ctx.send("React to this message to subscribe NPT.")
    reactions = ['✅']
    for i in reactions:
        await message.add_reaction(i)

@client.event
async def on_raw_reaction_add(payload):
    # We don't want to do operations if the bot is the one who reacted
    if payload.user_id == client.user.id: 
        return
    
    channel = client.get_channel(payload.channel_id)
    if channel is None: # This channel wasn't found
        return None
    
    guild = channel.guild
    if guild is None: # This reaction was not in a guild
        return
    
    member = payload.member
    if member is None:
        try:
            member = guild.get_member(payload.user_id) or (await guild.fetch_member(payload.user_id))
        except discord.NotFound: # This member wasn't found
            return None
    
    message = await channel.fetch_message(payload.message_id)
    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
    if reaction is None: # This reaction was not found
        return
    
    # The message ID is not right or the emoji was wrong
    if payload.message_id != 943634886642765874 or str(reaction.emoji) != '✅':
        return
    
    roles = discord.utils.get(guild.roles, name='TrackerSub')
    if roles is None: 
        # This role was not found, return.
        return
    
    await member.add_roles(roles)
    await reaction.remove(member)

Remember

Having a good on_command_error is important to Discord bot development. Using the traceback is always going to be a good friend when debugging things like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文