1
Fork 0
mirror of https://github.com/RGBCube/minearchy-bot synced 2025-07-27 00:47:44 +00:00

Handle a few more errors & format

This commit is contained in:
RGBCube 2022-07-01 17:57:21 +03:00
parent c619bf5e63
commit 99cb747dea
5 changed files with 50 additions and 28 deletions

10
bot.py
View file

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

View file

@ -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}```")

View file

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

View file

@ -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: <t:{ts}:F> (Relative: <t:{ts}:R>)
Content:
```
{message.content.replace('`', f'{zwsp}`{zwsp}')}
```"""
), # zero-width spaces, or it will break.
Deleted at: <t:{ts}:F> (Relative: <t:{ts}:R>)
Content:
```
{message.content.replace('`', f'{zwsp}`{zwsp}')}
```"""
),
inline=False,
)

View file

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