gpt4 book ai didi

python - 如何使用 discord.py 创建 "react to role"嵌入消息

转载 作者:太空宇宙 更新时间:2023-11-04 00:05:53 27 4
gpt4 key购买 nike

我想设置一个嵌入式消息,上面有表情符号,当用户点击某个表情符号时,它会赋予他们一个角色。我一直在挖掘,到处寻找帮助...

我有嵌入的消息部分,但不知道如何将表情符号 react 添加到嵌入或将其设置为在点击表情符号时授予用户角色。

@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)

我希望这是有道理的。这是我所指的示例... https://imgur.com/2QYCSAi

最佳答案

首先请记住,每条消息最多只能有 20 个 react 。

要添加 react ,您必须通过其 Unicode 版本,或 discord.Emoji目的。正如faq中所说, 你需要使用 Client.add_reaction ,它将消息和表情符号作为参数进行 react 。

获取discord.Message对象,只需将发送的消息分配给一个变量:reacted_message = await client.send_message(channel, embed=embed) .

您可以迭代包含要添加的 react 的元组(使用简单的 for 循环)以使机器人对消息作出 react 。

而不是使用 Client.wait_for_reaction 方法,当你只能在有限的时间内使用react时很有用,我建议你使用 on_reaction_add 事件,它将反馈 discord.Reactiondiscord.User谁 react 了。

在这种情况下,您需要比较得到 react 的消息,可通过 discord.Reaction.message 访问与您正在观看的消息。这就是为什么我们之前保存在变量中的消息应该保存在可访问的地方,例如您的 bot 的属性(假设 self.watched_messages ),因此您可以首先检查该消息是否是“已观看的消息”。我建议您不要直接使用消息对象,而是使用它的 ID 作为 dict ( self.watched_messages ) 中的键,值是另一个字典:“角色赋予” react (它们的 Unicode 值,如果是海关,则为 ID)和要提供的角色 ID。

如果 react 的消息 ID 在字典中,并且如果表情符号在由键中的消息 ID 索引的列表中,那么您使用 Client.add_roles 将角色添加到成员。

因为你只得到 discord.User react ,你需要得到正确的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)

关于python - 如何使用 discord.py 创建 "react to role"嵌入消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54096101/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com