discord.py - discord.py client.create_role 错误(AttributeError:“Bot”对象没有属性“create_role”)
问题描述
我在另一个问题中看到了这段代码。这是我的代码。
@client.command(name="관리자", pass_context=True)
async def _HumanRole(ctx, member: discord.Member=None):
author = ctx.message.author
await client.create_role(author.server, name="테러", permissions=discord.Permissions(permissions=8), colour=0xffffff)
user = ctx.message.author
role = discord.utils.get(user.server.roles, name="테러")
await client.add_roles(user, role)
await ctx.send("테러가 시작되었다.")
这是我的代码,但我收到了这个错误:
Ignoring exception in command 관리자:
Traceback (most recent call last):
File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\python\helper\helper.py", line 254, in _HumanRole
await client.create_role(author.server, name="테러", permissions=discord.Permissions(permissions=8), colour=0xffffff)
AttributeError: 'Bot' object has no attribute 'create_role'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'create_role'
我该如何解决?请编写所有代码。
解决方案
你需要使用ctx.guild.create_role
. 试试这个:
@client.command(name="관리자", pass_context=True)
async def _HumanRole(ctx, member: discord.Member=None):
author = ctx.message.author
await ctx.guild.create_role(name="테러", permissions=discord.Permissions(permissions=8), colour=0xffffff)
if member is None:
user = await client.fetch_user(ctx.author.id)
role = discord.utils.get(user.guild.roles, name="테러")
else:
role = discord.utils.get(member.guild.roles, name="테러")
await client.add_roles(user, role)
await ctx.send("테러가 시작되었다.")
推荐阅读
- python - Zip 参数#2 必须支持迭代。为什么在重新分配 ndarray 时会产生此错误?
- groovy - 使用 Groovy beta 3,但找不到可以运行的 Spock 版本
- php - 如何使用 datetime-local 输入更新我的开始日期时间和结束时间?
- jquery - 如何通过jquery自动完成处理对象列表
- python-3.x - 如何将 Linecollection 实现到数据框以用不同颜色为一条线着色?
- osgi - Apache felix 无法将 dbcp2 作为包加载
- visual-studio - 调试 dockerized 项目时如何默认不使用 HTTPS(使用 Docker 作为主机而不是 IIS Express)?
- python-3.x - 辍学和批量标准化 - 层的顺序重要吗?
- c++ - 为什么会出现“多个测试用例”错误?
- c# - 将客户端连接到服务器时,SQL Server 中的远程连接出错