Discord.py discord.NotFound 异常 [英] Discord.py discord.NotFound exception

查看:29
本文介绍了Discord.py discord.NotFound 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

everyone. I've been working on a few bots for my discord server and I've come across an issue in one of them which I cant seem to solve. I have a command called 'info', essentially, this command displays some information on a desired user. When I call the command ?info [user], it works just fine until I intentionally request the info on a user that doesn't exist to test an exception I have in place. Here'

@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.")

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

When I run this code and give a non-existent user as parameters, my exception doesn't work, instead, I get this output to my console:

Ignoring exception in command info
Traceback (most recent call last):
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandsot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandscore.py", line 367, in invoke
    yield from self.prepare(ctx)
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandscore.py", line 345, in prepare
    yield from self._parse_arguments(ctx)
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandscore.py", line 304, in _parse_arguments
    transformed = yield from self.transform(ctx, param)
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandscore.py", line 223, in transform
    raise e
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandscore.py", line 221, in transform
    return (yield from self.do_conversion(ctx, converter, argument))
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandscore.py", line 184, in do_conversion
    return instance.convert()
  File "C:UsersMatthewAppDataLocalProgramsPythonPython36-32libsite-packagesdiscordextcommandsconverter.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):,关于我能做什么的任何想法?

I've looked at countless examples and forums posts and the only knowledge I've gained was that the source of the error is in async def info(ctx, user: discord.Member = None):, any ideas on what I can do?

推荐答案

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

Error handling for commands is a little weird. When you specify a converter for the argument user, that conversion takes place outside of the body of the command coroutine. To handle it, you must write an error handler coroutine, and associate it with the command in question.

@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')

您可以在 rewrite 分支的文档.

You can read the documentation for error handlers, and the rest of the discord.ext.commands module, in the documentation for the rewrite branch.

如果您要使用命令扩展做大量工作,现在转换您的代码以便您可以使用准确的文档可能是有意义的.

If you're going to be doing a lot of work with the commands extension, it may make sense to convert your code now so that you can work with accurate documentation.

这篇关于Discord.py discord.NotFound 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆