【问题标题】:Add discord role to someone in direct message在直接消息中为某人添加不和谐角色
【发布时间】:2020-06-03 09:03:55
【问题描述】:

我有一个不和谐的机器人,您可以在其中使用!activate 激活您的密钥。 在活动中,我希望机器人在我的不和谐中赋予message.author PREMIUM MEMBER 角色。 我怎样才能做到这一点

我使用的是 @client.event 而不是 ctx。

感谢您的回答!

【问题讨论】:

标签: python discord.py


【解决方案1】:

试试这个

def check_activation_key(activation_key):
    # do your check here, return True or False

@client.event
async def on_message(message):

    if message.content.startswith('!activate ') and message.channel == message.author.dm_channel: # !activate, dms only
        activation_key = message.content[10:]
        if check_activation_key(activation_key):
            guild = client.get_guild(ID_OF_SERVER_TO_ASSIGN_ROLE_IN) # right click and "copy id"
            role = guild.get_role(ID_OF_ROLE_TO_ASSIGN) # right click and "copy id"
            await guild.get_member(message.author.id).add_roles(role)
    if message.content.startswith('!unactivate') and message.channel == message.author.dm_channel:
        guild = client.get_guild(ID_OF_SERVER_TO_TAKE_ROLE_IN)
        if ('PREMIUM MEMBER' in [role.name for role in guild.get_member(message.author.id).roles]):
            await guild.get_member(message.author.id).remove_roles(guild.get_role(ID_OF_ROLE_GOES_HERE))
        else:
            await message.channel.send('You did not verify yet')

一个缺点是这个不支持多台服务器,只有1台,但是你可以用不同的服务器ID发出不同的激活器命令。

通过右键单击图标/横幅并选择“复制 ID”来获取公会 ID。通过右键单击角色(在服务器设置或某人的个人资料中)并选择“复制 id”来获取角色的 ID。

【讨论】:

  • 我没有测试你的答案,但我现在让它工作了。还是谢谢你的回答
【解决方案2】:

您需要机器人所在的公会对象,这样它就知道从哪里获取角色。此外,message.author 对象将返回一个用户,而不是成员,但我们需要成员对象才能添加角色。

@client.event
async def on_message(message):
    if message.content.lower().startswith("!activate") and not message.guild:
        guild = client.get_guild(112233445566778899) # the guild's ID
        role = discord.utils.get(guild.roles, name="PREMIUM MEMBER") # or you can use id=
        member = await guild.fetch_member(message.author.id)
        await member.add_roles(role)

参考资料:

【讨论】:

  • 别担心!祝机器人好运:^)
猜你喜欢
  • 2018-08-11
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2021-01-17
  • 2020-04-04
  • 1970-01-01
相关资源
最近更新 更多