【问题标题】:Discord Bot Python add_rolesDiscord Bot Python add_roles
【发布时间】:2019-11-06 02:32:15
【问题描述】:

我正在尝试制作一个 Discord 机器人,当你键入 !join 时它会给你一个“会员”角色。 我在使用 add_roles 时遇到问题。

from discord.ext import commands
import os
from webserver import keep_alive

bot = commands.Bot(command_prefix = "!")
bot.remove_command('help')

@bot.event
async def on_member_join(member):
    role = discord.utils.get(member.guild.roles, name="OG")
    await member.add_roles(role)

@bot.event
async def on_ready():
  print("yeay eyea")


@bot.command(pass_context=True)
async def join(ctx):
  member = ctx.message.author
  role = discord.utils.get(member.guild.roles, name="Member")
  await bot.add_roles(member, role)



keep_alive()
TOKEN = os.environ.get("NjQwMTQaaaaaaaaaaaaaaaaaaaA4qHkx3Tl-l3CbpX8kTICfA")
bot.run("NaaaaaaaaaaaaaTICfA")

这是我尝试使用命令“!join”时遇到的错误:

Ignoring exception in command join:Traceback (most recent call last):
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext
/commands/core.py", line 79, in wrapped    ret = await coro(*args, **kwargs)
  File "main.py", line 23, in join
    await bot.add_roles(member, role)AttributeError: 'Bot' object has no attribute 'add_roles'

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext/commands/core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext/commands/core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError:'Bot' object has no attribute 'add_roles'

add_roles 不存在?

【问题讨论】:

  • 正如回溯所说:“Bot”对象没有“add_roles”属性。

标签: python discord.py


【解决方案1】:

抱歉,我正在回答而不是发表评论,但由于我的代表率低,我无法发布 cmets。您得到的错误是因为协程来自成员类。所以你需要做member.add_roles(member, role) 而不是bot.add_roles() 编辑:另外,请确保删除/更改您的令牌,因为它允许人们更改您的机器人代码并实质上劫持您的机器人。

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 2021-07-18
    • 2021-08-30
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2020-07-15
    • 2018-10-21
    • 2021-03-01
    相关资源
    最近更新 更多