首页 > 解决方案 > Discord.py 我正在寻找使用命令为每个人设置角色的 make 命令

问题描述

import discord
from discord.utils import get

client = commands.Bot(command_prefix="!")
@client.command(pass_context=True)
async def addrole(ctx):
    user = ctx.message.author
    role = discord.utils.get(user.server.roles, name="Open")
    await ctx.add_roles(user, role)

但是当我使用这个命令时。我收到此错误(discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'Member'对象没有属性'server')

标签: pythondiscord.py

解决方案


server更改为guild及其Member.add_roles()

以下是修改后的代码:

import discord
from discord.utils import get

client = commands.Bot(command_prefix="!")
@client.command()
async def addrole(ctx):
    role = get(ctx.guild.roles, name="Open")
    await ctx.author.add_roles(role)

推荐阅读