From cc8ea460f2da29bffc25fe763d2bb8d59f4b7eac Mon Sep 17 00:00:00 2001 From: wlinator Date: Sun, 17 Mar 2024 20:24:54 +0100 Subject: [PATCH] Consistency in error handling --- lib/economy_embeds.py | 8 --- lib/embeds.py | 123 ----------------------------------- lib/embeds/error.py | 57 +++++++++++++--- lib/embeds/info.py | 16 +++-- lib/embeds_old.py | 9 --- modules/economy/__init__.py | 31 ++++++++- modules/economy/blackjack.py | 8 +-- modules/economy/give.py | 4 +- modules/economy/inventory.py | 2 +- 9 files changed, 95 insertions(+), 163 deletions(-) delete mode 100644 lib/embeds.py diff --git a/lib/economy_embeds.py b/lib/economy_embeds.py index a470649..8804751 100644 --- a/lib/economy_embeds.py +++ b/lib/economy_embeds.py @@ -34,11 +34,3 @@ def not_enough_cash(): description="Oops! Your current cash balance isn't sufficient to do that." ) return embed - - -def out_of_time(): - embed = discord.Embed( - color=discord.Color.red(), - description="Uh-oh! Time's up. Your bet is forfeited as the game concludes." - ) - return embed diff --git a/lib/embeds.py b/lib/embeds.py deleted file mode 100644 index 19ffe11..0000000 --- a/lib/embeds.py +++ /dev/null @@ -1,123 +0,0 @@ -import discord - -from lib import formatter -from services.Xp import Xp - - -def welcome_message(member, template=None): - - embed = discord.Embed( - color=discord.Color.embed_background(), - description=f"_ _\n**Welcome** to **{member.guild.name}**" - ) - - if template: - embed.description += "↓↓↓\n" + formatter.template(template, member.name) - - embed.set_thumbnail(url=member.display_avatar) - - return embed - - -def command_error_1(error): - embed = discord.Embed( - color=discord.Color.red(), - description=f"Something went wrong.\n```{error}```" - ) - - return embed - - -def simple_question_5(question): - embed = discord.Embed(color=0xadcca6, - title=question) - - embed.set_footer(text="max. 5 characters") - return embed - - -def simple_question_30(question): - embed = discord.Embed(color=0xadcca6, - title=question) - - embed.set_footer(text="max. 30 characters") - return embed - - -def simple_question_100(question): - embed = discord.Embed(color=0xadcca6, - title=question) - - embed.set_footer(text="max. 100 characters") - return embed - - -def simple_question_300(question): - embed = discord.Embed(color=0xadcca6, - title=question) - - embed.set_footer(text="max. 300 characters") - return embed - - -def simple_question_none(question): - embed = discord.Embed(color=0xadcca6, - title=question) - return embed - - -def simple_question_first(question): - embed = discord.Embed(color=0xadcca6, - title=f"You chose to go with the short introduction! " - f"Let's start with your nickname. {question}") - embed.set_footer(text="max. 100 characters") - return embed - - -def simple_question_first_extended(question): - embed = discord.Embed(color=0xadcca6, - title=f"You chose to go with the extended introduction! " - f"Let's start with your nickname. {question}") - embed.set_footer(text="max. 100 characters") - return embed - - -def no_time(): - embed = discord.Embed(description="You ran out of time or clicked the \"Stop\" button. " - "If you wish to start over, do **/intro**.") - return embed - - -def final_embed_short(ctx, nickname, age, location, pronouns, likes, dislikes): - embed = discord.Embed(color=0x2200FF, description= - f"**(Nick)name:** {nickname}\n\n**Age:** {age}\n\n" - f"**Region:** {location}\n\n**Pronouns:** {pronouns}\n\n" - f"**Likes & interests:** {likes}\n\n**Dislikes:** {dislikes}") - - embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url) - embed.set_footer(text="Type: Short Introduction") - - return embed - - -def final_embed_extended(ctx, nickname, age, location, languages, pronouns, - sexuality, relationship_status, likes, dislikes, extra): - embed = discord.Embed(color=0xD91E1E, description= - f"**(Nick)name:** {nickname}\n\n**Age:** {age}\n\n" - f"**Region:** {location}\n\n**Languages:** {languages}\n\n" - f"**Pronouns:** {pronouns}\n\n**Sexuality** {sexuality}\n\n" - f"**Relationship status:** {relationship_status}\n\n**Likes & interests:** {likes}\n\n" - f"**Dislikes:** {dislikes}\n\n**EXTRAS:** {extra}") - - embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url) - embed.set_footer(text="Type: Extended Introduction") - - return embed - - -def final_confirmation(channel_id): - embed = discord.Embed(color=0xadcca6, - title="Your introduction has been posted in the server!", - description=f"<#{channel_id}>") - - return embed diff --git a/lib/embeds/error.py b/lib/embeds/error.py index 652ca60..5eb972e 100644 --- a/lib/embeds/error.py +++ b/lib/embeds/error.py @@ -2,11 +2,13 @@ import discord from lib import formatter question_icon = "https://i.imgur.com/8xccUws.png" +exclam_icon = "https://i.imgur.com/vitwMUu.png" -def clean_error_embed(): +def clean_error_embed(ctx): embed = discord.Embed( - color=discord.Color.red() + color=discord.Color.red(), + description=f"**{ctx.author.name}** " ) return embed @@ -18,8 +20,8 @@ class EconErrors: """ See MissingRequiredArgument """ - embed = clean_error_embed() - embed.description = f"**{ctx.author.name}** please enter a bet." + embed = clean_error_embed(ctx) + embed.description += "please enter a bet." embed.set_footer(text=f"For more info do '{formatter.get_prefix(ctx)}help {formatter.get_invoked_name(ctx)}'", icon_url=question_icon) return embed @@ -29,25 +31,60 @@ class EconErrors: """ See BadArgument """ - embed = clean_error_embed() - embed.description = f"**{ctx.author.name}** the bet you entered is invalid." + embed = clean_error_embed(ctx) + embed.description += "the bet you entered is invalid." + embed.set_footer(text=f"For more info do '{formatter.get_prefix(ctx)}help {formatter.get_invoked_name(ctx)}'", + icon_url=question_icon) + return embed + + @staticmethod + def bad_argument(ctx): + """ + See BadArgument + """ + embed = clean_error_embed(ctx) + embed.description += "the argument you entered is invalid." embed.set_footer(text=f"For more info do '{formatter.get_prefix(ctx)}help {formatter.get_invoked_name(ctx)}'", icon_url=question_icon) return embed @staticmethod def insufficient_balance(ctx): - embed = clean_error_embed() - embed.description = f"**{ctx.author.name}** you don't have enough cash." + embed = clean_error_embed(ctx) + embed.description += "you don't have enough cash." embed.set_footer(text=f"Do '{formatter.get_prefix(ctx)}balance' to see how much you can spend", icon_url=question_icon) return embed @staticmethod def daily_already_claimed(ctx, unix_time): - embed = clean_error_embed() - embed.description = f"**{ctx.author.name}** already claimed. You can claim your reward again ." + embed = clean_error_embed(ctx) + embed.description += f"already claimed. You can claim your reward again ." embed.set_footer(text=f"For more info do '{formatter.get_prefix(ctx)}help {formatter.get_invoked_name(ctx)}'", icon_url=question_icon) return embed + + @staticmethod + def out_of_time(ctx): + embed = clean_error_embed(ctx) + embed.description += "you ran out of time." + embed.set_footer(text="Your bet was forfeited", icon_url=exclam_icon) + + return embed + + @staticmethod + def already_playing(ctx): + embed = clean_error_embed(ctx) + embed.description += f"you already have a game of {ctx.command.name} running." + embed.set_footer(text="Please finish this game first", icon_url=exclam_icon) + + return embed + + @staticmethod + def generic_exception(ctx): + embed = clean_error_embed(ctx) + embed.description += "something went wrong." + embed.set_footer(text="Try the command again", icon_url=exclam_icon) + + return embed diff --git a/lib/embeds/info.py b/lib/embeds/info.py index 9e4c91d..52f47f1 100644 --- a/lib/embeds/info.py +++ b/lib/embeds/info.py @@ -1,15 +1,23 @@ import discord +question_icon = "https://i.imgur.com/8xccUws.png" exclam_icon = "https://i.imgur.com/vitwMUu.png" +def clean_info_embed(ctx): + embed = discord.Embed( + color=discord.Color.brand_green(), + description=f"**{ctx.author.name}** " + ) + + return embed + + class EconInfo: @staticmethod def daily_reward_claimed(ctx, formatted_amount, streak): - embed = discord.Embed( - color=discord.Color.brand_green(), - description=f"**{ctx.author.name}** you claimed your reward of **${formatted_amount}**!" - ) + embed = clean_info_embed(ctx) + embed.description += f"you claimed your reward of **${formatted_amount}**!" if streak > 1: embed.set_footer(text=f"You're on a streak of {streak} days", diff --git a/lib/embeds_old.py b/lib/embeds_old.py index 19ffe11..9b5626f 100644 --- a/lib/embeds_old.py +++ b/lib/embeds_old.py @@ -19,15 +19,6 @@ def welcome_message(member, template=None): return embed -def command_error_1(error): - embed = discord.Embed( - color=discord.Color.red(), - description=f"Something went wrong.\n```{error}```" - ) - - return embed - - def simple_question_5(question): embed = discord.Embed(color=0xadcca6, title=question) diff --git a/modules/economy/__init__.py b/modules/economy/__init__.py index a3eee7d..f9fc607 100644 --- a/modules/economy/__init__.py +++ b/modules/economy/__init__.py @@ -89,11 +89,13 @@ class Economy(commands.Cog): ) @commands.check(checks.channel) async def inventory(self, ctx): - return await inventory.cmd(ctx) + return await inventory.cmd(self, ctx) - @commands.slash_command( + @bridge.bridge_command( name="sell", description="Sell items from your inventory.", + help="Sell something from your inventory. This command has no arguments because when you do the command " + "it will lead you through the process of selling items.", guild_only=True ) @commands.check(checks.channel) @@ -129,6 +131,31 @@ class Economy(commands.Cog): async def stats(self, ctx, *, game: discord.Option(choices=["BlackJack", "Slots"])): return await stats.cmd(self, ctx, game) + @commands.command( + name="stats", + aliases=["stat"], + help="Display your gambling stats, you can choose between \"blackjack\" or \"slots\"." + ) + async def stats_prefixed(self, ctx, *, game: str): + + if game.lower() == "blackjack" or game.lower() == "bj": + game = "BlackJack" + elif game.lower() == "slots" or game.lower() == "slot": + game = "Slots" + else: + raise commands.BadArgument + + return await stats.cmd(self, ctx, game) + + @stats_prefixed.error + async def on_command_error(self, ctx, error): + if isinstance(error, commands.MissingRequiredArgument): + await ctx.respond(embed=EconErrors.missing_bet(ctx)) + elif isinstance(error, commands.BadArgument): + await ctx.respond(embed=EconErrors.bad_argument(ctx)) + else: + raise error + def setup(client): client.add_cog(Economy(client)) diff --git a/modules/economy/blackjack.py b/modules/economy/blackjack.py index 948ee30..8f9ae5e 100644 --- a/modules/economy/blackjack.py +++ b/modules/economy/blackjack.py @@ -6,7 +6,7 @@ import pytz from dotenv import load_dotenv from handlers.ItemHandler import ItemHandler -from lib import economy_embeds, economy_functions, interaction, embeds_old +from lib import economy_functions, interaction from lib.embeds.error import EconErrors from main import economy_config from services.BlackJackStats import BlackJackStats @@ -32,7 +32,7 @@ async def cmd(ctx, bet: int): # check if the player already has an active blackjack going if ctx.author.id in active_blackjack_games: - await ctx.respond(embed=economy_embeds.already_playing("BlackJack")) + await ctx.respond(embed=EconErrors.already_playing(ctx)) return # Currency handler @@ -161,7 +161,7 @@ async def cmd(ctx, bet: int): stats.push() elif status == 6: - await ctx.send(embed=economy_embeds.out_of_time(), content=ctx.author.mention) + await ctx.send(embed=EconErrors.out_of_time(ctx), content=ctx.author.mention) ctx_currency.take_balance(bet) ctx_currency.push() @@ -181,7 +181,7 @@ async def cmd(ctx, bet: int): stats.push() except Exception as e: - await ctx.respond(embed=embeds_old.command_error_1(e)) + await ctx.respond(embed=EconErrors.generic_exception(ctx)) logs.error("[CommandHandler] Something went wrong in the gambling command: ", e) finally: diff --git a/modules/economy/give.py b/modules/economy/give.py index 22a1594..4d15b46 100644 --- a/modules/economy/give.py +++ b/modules/economy/give.py @@ -1,6 +1,6 @@ import discord -from lib import economy_embeds +from lib.embeds.error import EconErrors from services.Currency import Currency @@ -26,7 +26,7 @@ async def cmd(ctx, user, amount): author_balance = ctx_currency.balance if author_balance < amount or author_balance <= 0: - return await ctx.respond(embed=economy_embeds.not_enough_cash()) + return await ctx.respond(embed=EconErrors.insufficient_balance(ctx)) target_currency.add_balance(amount) ctx_currency.take_balance(amount) diff --git a/modules/economy/inventory.py b/modules/economy/inventory.py index 01240ee..c9f5c2d 100644 --- a/modules/economy/inventory.py +++ b/modules/economy/inventory.py @@ -3,7 +3,7 @@ import discord from services.Inventory import Inventory -async def cmd(ctx): +async def cmd(self, ctx): inventory = Inventory(ctx.author.id) inventory_dict = inventory.get_inventory()