Discord bot在bot反应检查问题上应用角色

时间:2019-03-19 15:57:54

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

嗨,这是一个齿轮功能,机器人在其中发送一条消息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))

1 个答案:

答案 0 :(得分:0)

在您的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也应该始终为真,因此我认为检查不会完成任何事情。

相关问题