【问题标题】:Issue adding a role when reacting to a post对帖子做出反应时添加角色问题
【发布时间】:2019-08-09 00:53:15
【问题描述】:

当用户对帖子做出反应时,我在添加角色时遇到问题。

我希望它如何发挥作用是,当用户加入 Discord 服务器时,机器人将使用 on_join 事件发送消息(现在我使用命令 test 进行测试)。

下一步是on_reaction_add 事件,当用户对此消息做出反应时,机器人会将角色添加到该用户。

这是我正在使用的。但是,我已经对此进行了测试,但没有得到理想的结果。

(在 discord.py 重写时)

 @commands.command(pass_context=True)
async def test(self, user: discord.Member): #lets change this to the command 'test' for now and change on on_join event later. 
    guildName = user.guild.name
    embed = discord.Embed(colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
    embed.title = "Welcome to {} {}!".format(guildName, user)
    embed.description = "Head over to <#443133907102203912> to have a read up on our rules. Once you have read them please reaction to this post with a :thumbsup:"
    embed.set_image(url='')
    await user.send(embed=embed)

async def on_reaction_add(reaction, user): 
    channelid = '555844758778544160' #The text-channel where the react should happen
    role = discord.utils.get(user.guild.roles, name="Members") #The role applied when the post is reacted to

    if reaction.message.channel.id != channelid:
        return #So it only happens in the specified channel
    if str(reaction.emoji) == "<:thumbsup:555844758778544160>":#The reaction (not sure if this should be raw in discord.py rewrite)
        await user.add_roles(user, role)#Adds role when post is reacted to

【问题讨论】:

  • 这段代码是在 cog 还是在扩展中?您使用的是什么版本的 discord.py?最近的一项更改破坏了扩展和 cogs 工作方式的许多向后兼容性,因此您可能正在查看过时的示例。您需要在user 之前有一个ctx 参数,因为正在传入上下文,并且您可能还需要on_reaction_add 中的self(假设此代码在一个齿轮中)。
  • 这是一个齿轮,我正在使用 discord.py 重写
  • 什么版本号?你上次更新是什么时候?几周前进行了重大更改,因此如果您使用新代码但遵循可能解释您的问题的旧教程/示例。
  • @PatrickHaugh discord.py 重写 1.0.0a
  • @PatrickHaugh 谢谢你的回复我已经发布了一个答案,所以我可以告诉你我已经尝试过这个(添加了一些更改)不幸的是对用户消息的反应仍然没有申请这个角色我在这里错过了什么吗?

标签: python python-3.x discord discord.py discord.py-rewrite


【解决方案1】:

如果需要,您可以等待对特定消息的反应。这在这种情况下很方便,您可能想等待反应然后继续执行您的协程。假设您使用的是最新的重写,我将编写以下内容。您可以找到所有这些here 的文档:

from discord.utils import get
from discord.ext.commands import Cog, command
listener = Cog.listener

thumbs_up = "\N{THUMBS UP SIGN}"

def react_check(user, msg, emoji):
    def check(reaction, usr):
        return usr==user and reaction.message.id==msg.id and reaction.emoji==emoji
    return check

class MyCog(Cog):
    def __init__(self, bot):
        self.bot = bot
    @listener()
    async def on_member_join(self, member):
        guildName = user.guild.name
        embed = discord.Embed(colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
        embed.title = "Welcome to {} {}!".format(guildName, user)
        embed.description = "Head over to <#443133907102203912> to have a read up on our rules. Once you have read them please reaction to this post with a :thumbsup:"
        embed.set_image(url='')
        msg = await user.send(embed=embed)
        await self.bot.wait_for('reaction_add', check=react_check(user, msg, thumbs_up))
        role = get(user.guild.roles, name="Members")
        await user.add_roles(role)

然后在你的主机器人中,你需要一行写着

from mycog import MyCog

bot.add_cog(MyCog(bot))

【讨论】:

    【解决方案2】:
    from discord.ext import commands
    import discord
    import random
    from .utils import checks
    from random import randint
    from discord.utils import get
    
    
    thumbs_up = "\N{THUMBS UP SIGN}"
    
    def react_check(user, msg, emoji):
        def check(reaction, usr):
            return usr==user and reaction.message==msg and reaction.emoji==emoji
        return check
    
    class Welcome(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
        
        @commands.Cog.listener()
        async def on_member_join(self, user: discord.Member):
            guildName = user.guild.name
            channel = self.bot.get_channel(555844758778544160) 
            embed = discord.Embed(colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
            embed.title = "Welcome to {} {}!".format(guildName, user)
            embed.description = "Head over to <#443133907102203912> to have a read up on our rules. Once you have read them please reaction to this post with a :thumbsup:"
            embed.set_image(url='')
            msg = await channel.send(embed=embed)
            await self.bot.wait_for('reaction_add', check=react_check(user, msg, thumbs_up))
            role = get(user.guild.roles, name="Members")
            await user.add_roles(role)
            await channel.send('test')# Check 
        
    def setup(bot):
        bot.add_cog(Welcome(bot))
    

    【讨论】:

    • 您的活动不在 cog 中。应该再缩进一级
    • 那是def react_check(user, msg, emoji):... ?
    • 不,on_member_join
    • @PatrickHaugh 啊,是的,我看到了缩进错误,我刚刚修复了它并再次测试代码,但我仍然没有得到任何结果这里是屏幕截图pasteboard.co/I62lvRn.png 它似乎正在发送用户加入时的消息,但当赞许反应添加到帖子时,我去检查用户角色并且尚未添加成员角色。
    • @PatrickHaugh 我刚刚在我的控制台中注意到`文件“C:\Users\Sean\Desktop\developer-bot\cogs\welcome.py”,第 28 行,on_member_join 等待 msg.add_reaction (check=react_check(msg, thumbs_up, reaction.emoji)) NameError: name 'reaction' is not defined` 虽然不是已经定义了吗?
    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2023-01-13
    • 1970-01-01
    • 2020-03-22
    • 2020-08-03
    • 2019-08-31
    相关资源
    最近更新 更多