Discord 机器人在机器人反应检查问题上应用角色
Discord bot applying role on bot react check issue
嗨,这是一个 cog thats 功能,机器人发送消息 on_member_join
并允许用户对 post 作出反应,在这种情况下,机器人会为用户添加特定角色Members
。我在添加支票时遇到问题。
我想让机器人在用户对post做出反应之前设置一个预先竖起大拇指的表情符号,而不应用用户角色。
这种方法似乎有一个缺陷,它允许包括机器人在内的任何人用竖起大拇指的表情符号做出反应,并为加入的用户授予成员角色,而不是用户自己。似乎需要进行检查以仅注册加入用户已经使用了竖起大拇指的反应。
这可能吗?
这是我正在处理的内容:
我在代码块中用 # 引用了待办事项来给你一个想法。
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
中,您正在做 user==user
,这将永远是正确的。您需要为其中一个用户命名其他名称(在原始代码中,您会注意到我使用了 user
和 usr
,这可能让您感到困惑):
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
将 return 一个 check
函数,该函数将根据您传递给 react_check
的值检查反应
此外,self.bot
也应该始终为真,所以我认为检查不会完成任何事情。
嗨,这是一个 cog thats 功能,机器人发送消息 on_member_join
并允许用户对 post 作出反应,在这种情况下,机器人会为用户添加特定角色Members
。我在添加支票时遇到问题。
我想让机器人在用户对post做出反应之前设置一个预先竖起大拇指的表情符号,而不应用用户角色。
这种方法似乎有一个缺陷,它允许包括机器人在内的任何人用竖起大拇指的表情符号做出反应,并为加入的用户授予成员角色,而不是用户自己。似乎需要进行检查以仅注册加入用户已经使用了竖起大拇指的反应。
这可能吗?
这是我正在处理的内容:
我在代码块中用 # 引用了待办事项来给你一个想法。
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
中,您正在做 user==user
,这将永远是正确的。您需要为其中一个用户命名其他名称(在原始代码中,您会注意到我使用了 user
和 usr
,这可能让您感到困惑):
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
将 return 一个 check
函数,该函数将根据您传递给 react_check
此外,self.bot
也应该始终为真,所以我认为检查不会完成任何事情。