简体   繁体   English

Python/Discord:Bot 没有属性添加角色

[英]Python/Discord: Bot doesn't have attribute add roles

this is the code with the error:这是有错误的代码:

if message.content == "Test":
    member = message.author
    role = discord.utils.get(member.guild.roles, id="Character Created")
    await bot.add_roles(member, role)

This is my imports ("bot" used to be defined as Discord.Client() but I swapped it for the bot import):这是我的导入(“bot”曾经被定义为 Discord.Client() 但我将它换成了 bot 导入):

import discord
import asyncio
from discord.ext import commands
import os
from discord.utils import get
from discord.ext.commands import bot

This is the error message:这是错误消息:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\User\Desktop\idlerpgtest.py", line 41, in on_message
    await bot.add_roles(member, role)
AttributeError: module 'discord.ext.commands.bot' has no attribute 'add_roles'

It won't work because bot has no attribute add_roles , discord.Member has.它不会工作,因为bot有没有属性add_rolesdiscord.Member了。 So you can do this:所以你可以这样做:

if message.content == "Test":
    member = message.author
    role = discord.utils.get(member.guild.roles, id="Character Created")
    await member.add_roles(role)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM