问题
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