From 99cb747dead7ea6ba96a4719ff88f05d013cf363 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Fri, 1 Jul 2022 17:57:21 +0300 Subject: [PATCH] Handle a few more errors & format --- bot.py | 10 +++++----- cogs/error_handler.py | 18 +++++++++++------- cogs/mc_server.py | 16 ++++++++++++---- cogs/misc.py | 31 ++++++++++++++++++++----------- cogs/suggestions.py | 3 ++- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/bot.py b/bot.py index 0c2627d..68ab8bd 100644 --- a/bot.py +++ b/bot.py @@ -43,7 +43,10 @@ class MinearchyBot(commands.Bot): strip_after_prefix=True, help_attrs=dict( brief="Sends help.", - help="Sends all the commands of the bot, or help of a specific command and module.", + help=( + "Sends all the commands of the bot, or help of a specific command" + " and module." + ), ), ) @@ -89,9 +92,6 @@ with open("./config.json") as f: for key in ["BOT_TOKEN", "WEBHOOK_URL"]: config.setdefault(key, os.getenv(key)) -bot = MinearchyBot( - token=config["BOT_TOKEN"], - webhook_url=config["WEBHOOK_URL"] -) +bot = MinearchyBot(token=config["BOT_TOKEN"], webhook_url=config["WEBHOOK_URL"]) bot.run() diff --git a/cogs/error_handler.py b/cogs/error_handler.py index 4a14b87..59c17f5 100644 --- a/cogs/error_handler.py +++ b/cogs/error_handler.py @@ -1,5 +1,6 @@ from __future__ import annotations +import contextlib import traceback from typing import TYPE_CHECKING @@ -32,24 +33,27 @@ class ErrorHandler(commands.Cog): return elif isinstance(error, commands.NoPrivateMessage): - try: + with contextlib.suppress(discord.HTTPException): await ctx.author.send( - f"The commmand `{ctx.command}` cannot be used in DMs." + f"The command `{ctx.command.qualified_name}` cannot be used in DMs." ) - except discord.HTTPException: - pass - elif isinstance(error, commands.MissingPermissions): + elif isinstance(error, (commands.MissingPermissions, commands.NotOwner)): await ctx.reply("You can't use this command!") elif isinstance(error, commands.MissingRequiredArgument): - await ctx.reply(f"Missing a required argument: `{error.param.name}`") + await ctx.reply(f"Missing a required argument: `{error.param.name}`.") + + elif isinstance(error, commands.TooManyArguments): + await ctx.reply("Too many arguments.") elif isinstance(error, commands.ChannelNotFound): await ctx.reply("Invalid channel.") else: - trace = "".join(traceback.format_exception(type(error), error, error.__traceback__)) + trace = "".join( + traceback.format_exception(type(error), error, error.__traceback__) + ) print(f"Ignoring exception in command {ctx.command}:\n{trace}") await self.bot.log_webhook.send(f"<@512640455834337290>```{trace}```") diff --git a/cogs/mc_server.py b/cogs/mc_server.py index 7471aab..76ce132 100644 --- a/cogs/mc_server.py +++ b/cogs/mc_server.py @@ -24,13 +24,16 @@ class MinecraftServer( ) async def ip(self, ctx: commands.Context) -> None: await ctx.reply( - f"Java edition IP: `{self.bot.mc_server.ip}`\nBedrock edition IP: `{self.bot.mc_server.bedrock_ip}` (Port: 19132)\nNote: Minecraft 1.18+ is required to join." + f"Java edition IP: `{self.bot.mc_server.ip}`\nBedrock edition IP:" + f" `{self.bot.mc_server.bedrock_ip}` (Port: 19132)\nNote: Minecraft 1.18+" + " is required to join." ) @ip.command(brief="Sends the Java edition IP.", help="Sends the Java edition IP.") async def java(self, ctx: commands.Context) -> None: await ctx.reply( - f"The IP to connect on Minecraft Java edition is `{self.bot.mc_server.ip}`\nNote: Minecraft 1.18+ is required to join." + "The IP to connect on Minecraft Java edition is" + f" `{self.bot.mc_server.ip}`\nNote: Minecraft 1.18+ is required to join." ) @ip.command( @@ -39,12 +42,17 @@ class MinecraftServer( ) async def bedrock(self, ctx: commands.Context) -> None: await ctx.reply( - f"The IP to connect on Minecraft Bedrock edition is `{self.bot.mc_server.bedrock_ip}` (Port: 19132)\nNote: Minecraft 1.18+ is required to join." + "The IP to connect on Minecraft Bedrock edition is" + f" `{self.bot.mc_server.bedrock_ip}` (Port: 19132)\nNote: Minecraft 1.18+" + " is required to join." ) @commands.command( brief="Shows information about the Minecraft server.", - help="Shows the total player count, the Minecraft server IP and the server latency.", + help=( + "Shows the total player count, the Minecraft server IP and the server" + " latency." + ), ) async def status(self, ctx: commands.Context) -> None: server = self.bot.mc_server diff --git a/cogs/misc.py b/cogs/misc.py index bc16419..4be4fed 100644 --- a/cogs/misc.py +++ b/cogs/misc.py @@ -46,7 +46,9 @@ class Miscellanious( ) embed.add_field( name="Uptime", - value=f"```{datetime.timedelta(seconds=int(time.time() - self.bot.up_ts))}```", + value=( + f"```{datetime.timedelta(seconds=int(time.time() - self.bot.up_ts))}```" + ), ) await ctx.reply(embed=embed) @@ -76,7 +78,10 @@ class Miscellanious( @commands.command( brief="Sends the latest deleted messages.", - help="Sends the last 5 deleted messages in a specified channel.\nIf the channel isn't specified, it uses the current channel.", + help=( + "Sends the last 5 deleted messages in a specified channel.\nIf the channel" + " isn't specified, it uses the current channel." + ), ) @commands.has_permissions( manage_messages=True @@ -91,12 +96,16 @@ class Miscellanious( if not logs: await ctx.reply( - f"There are no messages to be sniped in {'this channel.' if channel.id == ctx.channel.id else channel.mention}" + "There are no messages to be sniped in" + f" {'this channel.' if channel.id == ctx.channel.id else channel.mention}" ) return embed = discord.Embed( - title=f"Showing last 5 deleted messages for {'the current channel' if ctx.channel.id == channel.id else channel}", + title=( + "Showing last 5 deleted messages for" + f" {'the current channel' if ctx.channel.id == channel.id else channel}" + ), description="The lower the number is, the more recent it got deleted.", color=self.bot.embed_color, ) @@ -104,15 +113,15 @@ class Miscellanious( for i, log in reversed(list(enumerate(logs))): message, ts = log embed.add_field( - name=str(i) + (" (latest)" if not i else ""), + name=str(i) + ("" if i else " (latest)"), value=inspect.cleandoc( f"""Author: {message.author.mention} (ID: {message.author.id}, Plain: {discord.utils.escape_markdown(str(message.author))}) - Deleted at: (Relative: ) - Content: - ``` - {message.content.replace('`', f'{zwsp}`{zwsp}')} - ```""" - ), # zero-width spaces, or it will break. + Deleted at: (Relative: ) + Content: + ``` + {message.content.replace('`', f'{zwsp}`{zwsp}')} + ```""" + ), inline=False, ) diff --git a/cogs/suggestions.py b/cogs/suggestions.py index 35ea52c..2c1bac8 100644 --- a/cogs/suggestions.py +++ b/cogs/suggestions.py @@ -31,7 +31,8 @@ class Suggestions(commands.Cog): await message.add_reaction("❌") await ctx.reply( - f"Suggestion submitted!\nYou can view it at {self.bot.suggestions_channel.mention}" + "Suggestion submitted!\nYou can view it at" + f" {self.bot.suggestions_channel.mention}" )