我的机器人如何通过命令为用户提供角色

时间:2021-01-16 11:30:38

标签: python python-3.x discord.py

例如,如果我输入 !Cuff @user,我希望该用户获得 cuff 的角色。有人知道怎么做吗?

# If user send an audio and it's a private chat
@bot.message_handler(content_types=["audio"])
def react_to_audio(message):
    if message.chat.type == "private":
        bot.reply_to(message, """What a nice sound! I'm not here to listen to some audio, tho. My work is to wish a good night to all members of a group chat""")

1 个答案:

答案 0 :(得分:0)

如果你想发出命令,你应该使用 discord.ext.commands 而不是 on_message 事件。然后,您可以通过传递参数轻松获取 discord.Member 对象。

此外,client 对象没有属性 add_roles。您应该使用 discord.Member.add_roles(role) 并且 message 对象没有属性 server,而是使用 guild

from discord.ext import commands

client = commands.Bot(command_prefix="!")

@client.command()
async def cuff(ctx, member: discord.Member=None):
    if not member:
        return # or you can send message to the channel that says "you have to pass member argument"
    role = get(ctx.guild.roles, name='Cuff')
    await member.add_roles(role)