Aar*_*nJK 2 python python-3.x discord discord.py
我想设置一条带有表情符号的嵌入式消息,当用户单击某个表情符号时,它就会赋予他们一个角色。我一直在四处寻找帮助......
我已经获得了嵌入的消息部分,但只是不知道如何将表情符号反应添加到嵌入中,或者将其设置为在单击表情符号时授予用户角色。
@client.event
async def on_message(message) :
#ignore this portion
if message.author == client.user:
return
elif message.content.startswith("~ping"):
await client.send_message(message.channe,"Pong!")
#read below this now
elif message.content.startswith("~embed"):
emb = (discord.Embed(title="role Update", description="Use the emotes to role", colour=0x3DF270))
await client.send_message(message.channel, embed=emb)
elif message.content.startswith("~embedroles"):
channel = bot.get_channel('532629344774914069')
role = discord.utils.get(user.server.roles, name="test")
while True:
reaction = await client.wait_for_reaction(emoji='\N{THUMBS UP SIGN}', message=message)
await bot.add_roles(reaction.message.author, role)
Run Code Online (Sandbox Code Playgroud)
我希望这是有道理的。这是我所指的示例... https://imgur.com/2QYCSAi
首先请记住,每条消息最多只能有 20 条回复。
要添加反应,您必须传递其 Unicode 版本或discord.Emoji对象。正如常见问题解答中所述,您需要使用Client.add_reaction,它将消息和表情符号作为参数进行反应。
要获取该discord.Message对象,只需将发送的消息分配给变量:reacted_message = await client.send_message(channel, embed=embed)。
您可以迭代包含要添加的反应的元组(使用简单的 for 循环)以使机器人对消息做出反应。
我建议您使用事件,而不是使用Client.wait_for_reaction方法,当您只能在有限的时间内做出反应时,该on_reaction_add方法很有用,它将反馈 adiscord.Reaction和discord.User谁做出了反应。
在这种情况下,您需要将收到反应的消息与discord.Reaction.message您正在观看的消息进行比较。这就是为什么我们之前保存在变量中的消息应该保存在可访问的地方,例如您的机器人的属性(比方说self.watched_messages),因此您可以首先检查该消息是否是“已监视的消息”。我建议您不要直接使用消息对象,而是使用它的 ID 作为字典 ( self.watched_messages) 中的键,该值是另一个字典:“角色赋予”反应(它们的 Unicode 值,或者如果是自定义的 ID)和角色 ID给。
如果反应的消息 ID 在字典中,并且表情符号在键中由 messageID 索引的列表中,则可以使用Client.add_roles将角色添加到成员。
由于您只能获得discord.User做出反应的内容,因此您需要获得discord.Member添加角色的权限。你可能会路过discord.Message.server。
类似地(保持相同的嵌套字典),使用on_reaction_remove事件删除角色,使用Client.remove_roles.
这是我的想法的伪代码
# template of the nested dict:
watched_messages = {
messageID: {
regularEmojiName: roleID,
customEmojiID: roleID
}
}
async def manage_reaction(self, reaction, user, added: bool):
if not reaction.message.id in self.watched_messages:
return
messageID = reaction.message.id
mapping = self.watched_messages[messageID]
if not reaction.emoji in mapping:
# reaction.emoji is str if normal emoji or ID if custom, but we use both as keys in mapping
return
member = discord.utils.get(reaction.message.server.members, id=user.id)
role = discord.utils.get(reaction.message.server.roles, id=mapping[reaction.emoji])
if added:
await client.add_roles(member, role)
else:
await client.remove_roles(member, role)
@client.event
async def on_reaction_add(self, reaction, user):
await self.manage_reactions(reaction, user, True)
@client.event
async def on_reaction_remove(self, reaction, user):
await self.manage_reactions(reaction, user, False)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7526 次 |
| 最近记录: |