【问题标题】:Discord bot applying role on bot react check issueDiscord 机器人在机器人反应检查问题上应用角色
【发布时间】:2019-08-10 05:27:31
【问题描述】:

您好,这是一个 cog thats 功能,机器人发送消息 on_member_join 并允许用户对机器人为用户添加特定角色的帖子做出反应,在这种情况下它是 Members。我在添加支票时遇到问题。

  1. 我想让机器人在用户对帖子做出反应之前设置一个竖起大拇指的表情符号,而不应用用户角色。

  2. 这种方法似乎存在一个缺陷,它允许包括机器人在内的任何人对竖起大拇指表情符号做出反应,并赋予加入用户成员角色,而不是用户自己。似乎需要检查以仅注册加入用户已使用竖起大拇指的反应。

这可能吗?

这是我正在使用的:

我已经在代码块中引用了带有 # 的待办事项来给你一个想法。

thumbs_up = "\N{THUMBS UP SIGN}"

def react_check(user, msg, emoji):
    def check(reaction, user):
       return user==user and reaction.message.id==msg.id 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 = "Hello {}, welcome to {}!".format(user, guildName)
    embed.description = "Before you continue we just want to make sure you have read up on our all important group guidelines. if you haven't you can find them in <#546694486391128066>. Once you're satified with them go ahead and give my message a :thumbsup:."
    embed.set_image(url='')
    msg = await channel.send(embed=embed)
    await msg.add_reaction(thumbs_up) # todo: make bot set a thumbs_up emoji but only set members role when the user mentioned reacts. 
    await self.bot.wait_for('reaction_add', check=react_check(user, msg, thumbs_up))
    role = get(user.guild.roles, name="Members")
    if self.bot: #supposed check to see if the bot 
        pass
    else: 
        await user.add_roles(role)
        await channel.send(':thumbsup: Awesome! you\'re now set to go. If you would like to add some roles head over to <#555913791615926302> for more details. ')# Check 

def setup(bot):
    bot.add_cog(Welcome(bot))

【问题讨论】:

  • 您应该在此问题中添加react_check 代码。 react_check 是否为user 以外的对msg 做出反应的用户返回True?只有在给出满足react_check 标准的反应时,它才应该继续协程。
  • @PatrickHaugh 我们昨天在另一个问题中谈到了确切的代码。我已经继续并在我的问题中添加了react_check 代码。理想情况下,我希望机器人在不向用户添加成员角色的情况下添加对帖子的反应,除非用户添加到该反应中。 IE。机器人首先做出反应,但用户第二个做出反应,将角色应用于该用户。

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


【解决方案1】:

在您的react_check 中,您正在执行user==user,这将永远是正确的。您需要将其中一个用户命名为其他名称(在原始代码中,您会注意到我使用了 userusr,这可能会让您感到困惑):

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

这段代码使用了一种稍微复杂但非常有用的模式,称为闭包。闭包基本上是一种创建同一函数的多个版本的技术,这些版本对某些变量使用不同的值。在这里,调用react_check 将返回一个check 函数,该函数将根据您传递给react_check 的值检查反应

另外,self.bot 也应该始终为真,所以我认为检查不会完成任何事情。

【讨论】:

    猜你喜欢
    • 2022-10-30
    • 2020-12-30
    • 1970-01-01
    • 2021-04-23
    • 2021-10-04
    • 2019-01-15
    • 2021-05-22
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多