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.149]) by gnuweeb.org (Postfix) with ESMTPSA id 65BCE80DFA; Sun, 25 Sep 2022 21:15:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1664140506; bh=dY7Bgn1/bx9myYPUQNq8sSlnAax9QdUukgOdPB72Qp4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RV6Sm6NJLc07VqQbyGZFvUlM4WFB1OSdp9cPOyXOUEDB9VxreSi5S2z9kc4syRj9y qn9PvqhfNTGwO+YJrA8mJcB3MG17oPBEvP5mFBemiecEQ1u65kkMc0TwpqmQzOrbia xOSDkTUSEkDlQG8lr87LOYofIVqa99oaSs9qJCJpyCMY48mGtRAtnuCuj7A6m6wMfj 8LKcLm/wXR5efORkH7wAq0Q54jtG5D79d6KW4sqmTXKPrJIANDchz5XZI+ZghbfiX5 8O/VQKSAdB1/Sh6BrlSZOEC21+JYCmyq7bFqpUFCRxtiliETD8igqLxcIiNfnrfqN9 d25P9+Zw6YTiQ== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [RFC PATCH v5 10/18] discord: Add error handler on events Date: Mon, 26 Sep 2022 04:14:19 +0700 Message-Id: <20220925211427.412-11-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220925211427.412-1-kiizuha@gnuweeb.org> References: <20220925211427.412-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add an error handler to ignore the `CommandNotFound` error while someone sends a message with the bot prefix (e.g `$hai`) that is not exist in the bot basic commands. Actually, this error wouldn't appear if set log_handler to None in the main file on the client.run() parameter. But I will keep this if using a log handler in the future use. The 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