From 563bcd4e2aa71c1e7ff87a3bf7a3beb4114bb0cb Mon Sep 17 00:00:00 2001 From: jackra1n <45038833+jackra1n@users.noreply.github.com> Date: Tue, 19 Mar 2024 15:25:06 +0100 Subject: [PATCH] Improve error messages in error channel, improve error message for karma donate --- core/bot.py | 6 +++++- extensions/karma.py | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/bot.py b/core/bot.py index 454083e..45ac674 100644 --- a/core/bot.py +++ b/core/bot.py @@ -86,7 +86,11 @@ async def on_command_error(self, ctx: commands.Context, error) -> None: await ctx.send("A required argument is missing.") ERRORS_CHANNEL_ID = 1219407043186659479 - embed = discord.Embed(title="Error", description=f"```{error}```", color=discord.Color.red()) + if ctx.guild: + error_msg = f"Error in {ctx.guild.name} ({ctx.guild.id}) by {ctx.author} -> {ctx.command.qualified_name}" + else: + error_msg = f"Error in DMs by {ctx.author} -> {ctx.command.qualified_name}" + embed = discord.Embed(title=error_msg, description=f"```{error}```", color=discord.Color.red()) await self.get_channel(ERRORS_CHANNEL_ID).send(embed=embed) try: diff --git a/extensions/karma.py b/extensions/karma.py index 6c6af4f..49e7eca 100644 --- a/extensions/karma.py +++ b/extensions/karma.py @@ -1,5 +1,6 @@ import logging import os +import io import re from datetime import datetime @@ -165,7 +166,7 @@ async def karma_donate(self, ctx: commands.Context, *args): """ if len(args) != 2: msg = f"Got {len(args)} arguments, expected 2." - raise commands.MissingRequiredArgument(msg) + raise NotEnoughArguments(msg) user = None amount = None @@ -238,8 +239,9 @@ def pred(m: discord.Member) -> bool: @karma_donate.error async def karma_donate_error(self, ctx: commands.Context, error): embed = discord.Embed(color=0xF66045) - if isinstance(error, commands.MissingRequiredArgument): - embed.description = "You didn't specify a user to donate to!" + print(type(error)) + if isinstance(error, NotEnoughArguments): + embed.description = "You didn't specify a `amount` or `user` to donate to!" elif isinstance(error, commands.BadArgument): embed.description = f"Wrong command usage! Command usage is `{ctx.prefix}karma donate `" else: @@ -460,10 +462,10 @@ async def karma_stats_graph(self, ctx: commands.Context): fig = go.Figure(data=go.Bar(x=x, y=y)) fig.update_layout(title="Karma Graph", xaxis_title="Percentile of users", yaxis_title="Total karma") fig.update_layout(template="plotly_dark") - fig.write_image(filename) + image_bytes = fig.to_image(format="png") + image_file = io.BytesIO(image_bytes) - await ctx.send(file=discord.File(filename)) - os.remove(filename) + await ctx.send(file=discord.File(image_file, filename=f"{filename}.png")) @commands.hybrid_group(name="post", aliases=["po"], invoke_without_command=True) async def post(self, ctx: commands.Context): @@ -1098,6 +1100,10 @@ async def on_submit(self, interaction: discord.Interaction) -> None: await _update_kasino_msg(bot, kasino_id) +class NotEnoughArguments(commands.UserInputError): + pass + + async def setup(bot: core.Substiify): query = await bot.db.fetch("SELECT * FROM discord_channel WHERE upvote = True") upvote_channels = [channel["discord_channel_id"] for channel in query] or []