需要有关 discord.py 机器人的建议

Need advice about discord.py bot

写我的第一个机器人。我正在尝试制作一个可以通过命令更改成员的用户名和角色的机器人。

示例:!setnick [成员] [newnick] ; !giverole [成员] [角色]

我的角色命令运行良好,但是我对更改昵称文档感到困惑 here

    @bot.command(pass_context=True)
async def setnick(ctx, nickname):
    await change_nickname(server.get_member, nickname)
    await ctx.send(f"User's nickname has been changed.")

机器人运行,但是当我在 discord 中输入命令时 returns 出现此错误:

Ignoring exception in command setnick:
Traceback (most recent call last):
  File "FILEPATH", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "FILEPATH", line 23, in setnick
    await change_nickname(server.get_member, nickname)
NameError: name 'change_nickname' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "FILEPATH", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "FILEPATH", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "FILEPATH", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'change_nickname' is not defined

提前致谢!

change_nickname 是您从 class 调用的函数,您可以参考您的文档,您会注意到该函数在 class 下调用 [=19] =].
这意味着您不能凭空调用 change_nickname

For example, it is like your send(string) method. You can't just invoke it out of nowhere, you need to use a text channel object to invoke it.
Hence, the reason why you did ctx.send() instead of doing send().

您可能在登录到您的机器人时创建了一个客户端对象,yourObjectName = new Client()。找到它并使用它来调用 change_nickname(),如下所示:

await yourObjectName.change_nickname(server.get_member, nickname)