首页 > 解决方案 > 通过用户提及向用户添加角色

问题描述

我试图制作一个能够通过用户提及向用户添加角色的机器人。我也想在命令中传递角色。所以语法应该是:
!addrole ROLENAME @user
我试过这个:

import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get

def read_token():
    with open("token.txt", "r") as f:
        lines = f.readlines()
        return lines[0].strip()

token = read_token()

bot = commands.Bot(command_prefix='!')


@bot.command(pass_context=True)
async def addrole(ctx, arg1, member: discord.Member):
    role = get(member.guild.roles, name={arg1})
    await member.add_roles(role)

bot.run(token)

但它不起作用。我收到错误:
AttributeError: 'NoneType' object has no attribute 'id'

标签: pythondiscorddiscord.py-rewrite

解决方案


您已经在使用转换器member也使用一个转换器role

from discord import Member, Role

@bot.command()
async def addrole(ctx, member: Member, *, role: Role):
    await member.add_roles(role)

, *,允许您提供具有多个单词的角色名称。


推荐阅读