gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:24 28 4
gpt4 key购买 nike

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

例子:!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 中输入命令时,它返回了这个错误:

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 是您从类调用的函数,您可以引用您的文档,您会注意到该函数位于名为 Client 的类下.
这意味着您不能凭空调用 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)

关于python - 需要有关 discord.py 机器人的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59005353/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com