How to assign a user a role in a server from a direct message discord.py

落爺英雄遲暮 提交于 2020-03-28 07:05:02

问题


So I am trying to make a bot where if a user direct messages the bot, it will give them a role in a server that both the user and the bot are in. I tried to just add the role based on the role ID but that did not work.

Here is an example of what I was thinking which could maybe help explain it a bit better.

role = (role ID)

member = message.author
await client.add_roles(member, role)

Now keep in mind this will be happening in a direct message, and not in a server where this would be much easier.

If anyone knows how to do this or has any ideas, please let me know.

Thanks


回答1:


Here we record the ids and then access the appropriate objects when we recieve the command.

target_server_id = "123..."
target_role_id   = "456..."

@bot.command(pass_context=True)
async def gimmieRole(ctx):
    if not ctx.message.channel.is_private:
        await bot.say("Private command only")
    server = await bot.get_Server(target_server_id)
    role = discord.utils.get(server.roles, id=target_role_id)
    member = server.get_member(ctx.message.author.id)
    if member:
        await bot.add_roles(member, role)
    else:
        await bot.say("You are not a member")


来源:https://stackoverflow.com/questions/51737572/how-to-assign-a-user-a-role-in-a-server-from-a-direct-message-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!