From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.100]) by gnuweeb.org (Postfix) with ESMTPSA id 94E168060C; Tue, 20 Sep 2022 13:48:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1663681732; bh=vurtlSM28vGbz6GCbQj3Fv40XCaz9vKIcc2WEA74ZtA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QlexP7jdUoVkcIJJ/SUNt9YP+yx+Xlxiz4xf9qVWgZth0Bf936ce/kmK1hf/1Ip/B fv6mG5Hkybd4MF5UfRmgH9n5JOskU4+uKwDpZ1oN9rfz3ZxQ2PKfe/yc12orRc+T9q m0xBti/cbzXIw5y3IgMqGoZcOrjSxa7vzQGqCjm21IzwfFUsupznhU+iDmQ3PE2jBH 2r8C9SMWJCk+zd73pePlVeRzHuDnmgZc6Rrpdelk1pQNb6hDrVXUWiStKUrPqY6PGH e/MicJ1WHSMxM8wXwz6eFWrfb14lGiCCl32A49LBmrnsUfFjX4//nF3Jo391545OBg dA04cjCSCicBw== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [RFC PATCH v4 10/17] discord: Add error handler on events Date: Tue, 20 Sep 2022 20:48:05 +0700 Message-Id: <20220920134812.331-11-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220920134812.331-1-kiizuha@gnuweeb.org> References: <20220920134812.331-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add error handler to ignore `CommandNotFound` error while someone send a message with the bot prefix (e.g `$hai`) that is not exists in the bot basic commands. Actually, this error wouldn't appear if set log_handler to None in the main file on client.run() parameter. But I will keep this if using a log handler in the future use. Error log says: discord.ext.commands.bot: Ignoring exception in command None discord.ext.commands.errors.CommandNotFound: Command "w" is not found Signed-off-by: Muhammad Rizki --- .../dscord/gnuweeb/plugins/events/__init__.py | 2 ++ .../dscord/gnuweeb/plugins/events/on_error.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 daemon/dscord/gnuweeb/plugins/events/on_error.py diff --git a/daemon/dscord/gnuweeb/plugins/events/__init__.py b/daemon/dscord/gnuweeb/plugins/events/__init__.py index d9a891f..7074e4d 100644 --- a/daemon/dscord/gnuweeb/plugins/events/__init__.py +++ b/daemon/dscord/gnuweeb/plugins/events/__init__.py @@ -4,8 +4,10 @@ # from .on_ready import OnReady +from .on_error import OnError class Events( OnReady, + OnError ): pass diff --git a/daemon/dscord/gnuweeb/plugins/events/on_error.py b/daemon/dscord/gnuweeb/plugins/events/on_error.py new file mode 100644 index 0000000..e1e4e28 --- /dev/null +++ b/daemon/dscord/gnuweeb/plugins/events/on_error.py @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2022 Muhammad Rizki +# + +from discord.ext import commands + + +class OnError(commands.Cog): + def __init__(self, bot: "commands.Bot") -> None: + self.bot = bot + + + @commands.Cog.listener() + async def on_command_error(self, _, err): + if isinstance(err, commands.CommandNotFound): + pass -- Muhammad Rizki