Discord.py通过私人消息在服务器上赋予角色

时间:2020-01-05 16:17:51

标签: python discord discord.py

我想给一个私人消息机器人的用户在特定服务器中扮演特定角色。我该怎么办,我在互联网上找不到任何东西。我现在拥有的代码(无效且已过时):

        target_server_id = "623597980175433749"
        target_role_id   = "656158110179655701"


        #if not ctx.message.channel.is_private:
            #await client.say("Private command only")
        server = await client.get_guild(target_server_id)
        role = discord.utils.get(guild.roles, id=target_role_id)
        member = guild.get_member(ctx.message.author.id)
        if member:
            await client.add_roles(member, role)
        else:
            await client.say("You are not a member")

1 个答案:

答案 0 :(得分:1)

我认为您需要进行的唯一更改是将整数用作ID,将client.add_roles更改为member.add_roles,然后使用ctx.send

target_server_id = 623597980175433749
target_role_id   = 656158110179655701

if not ctx.guild is not None:
    await ctx.send("Private command only")
server = await client.get_guild(target_server_id)
role = discord.utils.get(guild.roles, id=target_role_id)
member = guild.get_member(ctx.message.author.id)
if member:
    await member.add_roles(role)
else:
    await ctx.send("You are not a member")