From 360454c152e527d0b3145b2695cbaa336f65594f Mon Sep 17 00:00:00 2001 From: wlinator Date: Sun, 22 Sep 2024 16:22:45 +0200 Subject: [PATCH] refactor: Update coinflip command to handle invalid predictions --- locales/strings.en-US.json | 18 ++++++++-------- modules/economy/coinflip.py | 43 ++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/locales/strings.en-US.json b/locales/strings.en-US.json index 3ba7ffd..9cb66bb 100644 --- a/locales/strings.en-US.json +++ b/locales/strings.en-US.json @@ -79,18 +79,18 @@ "case_type_field_value": "`{0}`", "case_type_field_value_with_duration": "`{0} ({1})`", "coinflip_correct_prediction_author": "Correct Prediction!", - "coinflip_correct_prediction_description": "The coin landed on **{0}**. You predicted correctly!", - "coinflip_flipping_animation_1": "\ud83e\ude79 Flipping...", - "coinflip_flipping_animation_2": "\ud83e\ude79 Flipping..", - "coinflip_flipping_animation_3": "\ud83e\ude79 Flipping.", - "coinflip_flipping_author": "Flipping a Coin", - "coinflip_flipping_description": "The coin is in the air...", + "coinflip_correct_prediction_description": "the coin landed on **{0}**. You predicted correctly!", + "coinflip_flipping_animation_1": "\ud83e\ude99 Flipping...", + "coinflip_flipping_animation_2": "\ud83e\ude99 Flipping..", + "coinflip_flipping_animation_3": "\ud83e\ude99 Flipping.", + "coinflip_flipping_author": "flipping a Coin", + "coinflip_flipping_description": "the coin is in the air...", "coinflip_invalid_prediction_author": "Invalid Prediction", - "coinflip_invalid_prediction_description": "Please enter a valid prediction ('heads'/'h' or 'tails'/'t').", + "coinflip_invalid_prediction_description": "please enter a valid prediction ('heads'/'h' or 'tails'/'t').", "coinflip_result_author": "Coin Flip Result", - "coinflip_result_description": "The coin landed on **{0}**.", + "coinflip_result_description": "the coin landed on **{0}**.", "coinflip_wrong_prediction_author": "Wrong Prediction", - "coinflip_wrong_prediction_description": "The coin landed on **{0}**. Your prediction was incorrect.", + "coinflip_wrong_prediction_description": "the coin landed on **{0}**. Your prediction was incorrect.", "config_author": "Server Configuration", "config_birthday_channel_set": "birthday announcements will be sent in {0}.", "config_birthday_module_already_disabled": "the birthday module was already disabled.", diff --git a/modules/economy/coinflip.py b/modules/economy/coinflip.py index 7dd09a3..4dc2eb5 100644 --- a/modules/economy/coinflip.py +++ b/modules/economy/coinflip.py @@ -36,6 +36,18 @@ class Coinflip(commands.Cog): """ result = random.choice(["heads", "tails"]) + if prediction: + prediction = prediction.lower() + if prediction not in ["heads", "h", "tails", "t"]: + embed = Builder.create_embed( + Builder.ERROR, + user_name=ctx.author.name, + author_text=CONST.STRINGS["coinflip_invalid_prediction_author"], + description=CONST.STRINGS["coinflip_invalid_prediction_description"], + ) + await ctx.send(embed=embed) + return + flip_embed = Builder.create_embed( Builder.INFO, user_name=ctx.author.name, @@ -61,31 +73,22 @@ class Coinflip(commands.Cog): await asyncio.sleep(0.5) if prediction: - prediction = prediction.lower() - if prediction in ["heads", "h", "tails", "t"]: - predicted_correctly = (prediction.startswith("h") and result == "heads") or ( - prediction.startswith("t") and result == "tails" + predicted_correctly = (prediction.startswith("h") and result == "heads") or ( + prediction.startswith("t") and result == "tails" + ) + if predicted_correctly: + embed = Builder.create_embed( + Builder.SUCCESS, + user_name=ctx.author.name, + author_text=CONST.STRINGS["coinflip_correct_prediction_author"], + description=CONST.STRINGS["coinflip_correct_prediction_description"].format(result), ) - if predicted_correctly: - embed = Builder.create_embed( - Builder.SUCCESS, - user_name=ctx.author.name, - author_text=CONST.STRINGS["coinflip_correct_prediction_author"], - description=CONST.STRINGS["coinflip_correct_prediction_description"].format(result), - ) - else: - embed = Builder.create_embed( - Builder.ERROR, - user_name=ctx.author.name, - author_text=CONST.STRINGS["coinflip_wrong_prediction_author"], - description=CONST.STRINGS["coinflip_wrong_prediction_description"].format(result), - ) else: embed = Builder.create_embed( Builder.ERROR, user_name=ctx.author.name, - author_text=CONST.STRINGS["coinflip_invalid_prediction_author"], - description=CONST.STRINGS["coinflip_invalid_prediction_description"], + author_text=CONST.STRINGS["coinflip_wrong_prediction_author"], + description=CONST.STRINGS["coinflip_wrong_prediction_description"].format(result), ) else: embed = Builder.create_embed(