【问题标题】:Discord.py - Get context from error objectDiscord.py - 从错误对象中获取上下文
【发布时间】:2021-08-05 23:43:00
【问题描述】:

我有一个齿轮test

我有一个函数正在检查命令是否有错误。

在这个例子中,目前,如果用户在 Discord 中调用命令时没有提供参数,则会将错误打印到控制台。

我想用所述错误消息回复调用命令的原始消息 (!test)。

通常我会使用commands.Context 来执行此操作,但是由于它不是我的实现中的参数,那么最好的方法是什么?

import discord
from discord.ext import commands


class Test(commands.Cog):
    def __init__(self, client):
        self.client = client


    @commands.command()
    async def test(self, ctx, arg1):
        await ctx.reply(arg1)

    @test.error
    async def test_error(self, error):
        if isinstance(error, commands.errors.MissingRequiredArgument):
            # Here I want to reply
            print(error)


def setup(client):
    client.add_cog(Test(client))

提前感谢您的帮助

【问题讨论】:

    标签: python error-handling discord discord.py


    【解决方案1】:

    错误处理程序也将ctx 作为参数

    @test.error
    async def test_error(self, ctx, error):
        if isinstance(error, commands.errors.MissingRequiredArgument):
            # Here I want to reply
            print(error)
    

    看看error handling introduction

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2021-05-10
      • 2019-06-15
      • 1970-01-01
      • 2023-03-09
      • 2021-03-27
      • 2021-04-28
      相关资源
      最近更新 更多