gpt4 book ai didi

python - Discord.py discord.NotFound 异常

转载 作者:太空宇宙 更新时间:2023-11-04 00:20:00 25 4
gpt4 key购买 nike

大家。我一直在为我的 discord 服务器开发几个机器人,我在其中一个机器人中遇到了一个我似乎无法解决的问题。我有一个名为“信息”的命令,本质上,该命令显示有关所需用户的一些信息。当我调用命令 ?info [user] 时,它工作得很好,直到我有意请求一个不存在的用户的信息来测试我已经存在的异常。这里'

@laugh.command(pass_context=True)
async def info(ctx, user: discord.Member = None):
if ctx.message.channel.name != "admin":
await laugh.send_message(ctx.message.author, "Sorry, you need to be in a text channel called 'admin' for that.")
return
if not ctx.message.author.server_permissions.administrator:
return
if not user:
user = ctx.message.author
try:
minfo = ("""
{0}'s ID is: {1}
{0}'s status is: {2}
{0}'s highest role is: {3}
{0} joined at: {4}""".format(user.name, user.id, user.status, user.top_role, user.joined_at))
embed = discord.Embed(title = user.name, description = minfo, color = 0x00D2FF)
await laugh.say(embed = embed)
except discord.NotFound:
await laugh.say("User not found.")

当我运行此代码并将一个不存在的用户作为参数时,我的异常不起作用,相反,我将此输出发送到我的控制台:

Ignoring exception in command info
Traceback (most recent call last):
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 367, in invoke
yield from self.prepare(ctx)
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 345, in prepare
yield from self._parse_arguments(ctx)
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 304, in _parse_arguments
transformed = yield from self.transform(ctx, param)
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 223, in transform
raise e
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 221, in transform
return (yield from self.do_conversion(ctx, converter, argument))
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 184, in do_conversion
return instance.convert()
File "C:\Users\Matthew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\converter.py", line 100, in convert
raise BadArgument('Member "{}" not found'.format(self.argument))
discord.ext.commands.errors.BadArgument: Member "test" not found

我查看了无数示例和论坛帖子,我获得的唯一知识是错误的来源在 async def info(ctx, user: discord.Member = None):,关于我可以做什么的任何想法?

最佳答案

命令的错误处理有点奇怪。当您为参数 user 指定转换器时,该转换发生在命令协程的主体之外。要处理它,您必须编写一个错误处理程序协程,并将其与相关命令相关联。

@laugh.command(pass_context=True)
async def info(ctx, user: discord.Member = None):
if ctx.message.channel.name != "admin":
await laugh.send_message(ctx.message.author, "Sorry, you need to be in a text channel called 'admin' for that.")
return
if not ctx.message.author.server_permissions.administrator:
return
if not user:
user = ctx.message.author
minfo = ("""
{0}'s ID is: {1}
{0}'s status is: {2}
{0}'s highest role is: {3}
{0} joined at: {4}""".format(user.name, user.id, user.status, user.top_role, user.joined_at))
embed = discord.Embed(title = user.name, description = minfo, color = 0x00D2FF)
await laugh.say(embed = embed)

@info.error
async def info_error(ctx, error): # This might need to be (error, ctx), I'm not sure
if isinstance(error, commands.BadArgument):
await laugh.say('I could not find that user')

您可以阅读错误处理程序的文档,以及 discord.ext.commands 模块的其余部分,在 documentation for the rewrite branch.

如果您要使用命令扩展进行大量工作,那么现在转换您的代码可能很有意义,这样您就可以使用准确的文档。

关于python - Discord.py discord.NotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49478189/

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