如何通过直接消息discord.py在服务器中为用户分配角色 [英] How to assign a user a role in a server from a direct message discord.py

查看:122
本文介绍了如何通过直接消息discord.py在服务器中为用户分配角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试制作一个机器人,如果用户直接向该机器人发送消息,它将为他们提供一个用户和机器人都在服务器上的角色。我试图根据角色添加角色ID,但没有用。

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.

谢谢

推荐答案

在这里,我们记录ID,然后在收到命令时访问相应的对象。

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

这篇关于如何通过直接消息discord.py在服务器中为用户分配角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆