diff --git a/techsupport_bot/commands/duck.py b/techsupport_bot/commands/duck.py index ca141834..71734f6c 100644 --- a/techsupport_bot/commands/duck.py +++ b/techsupport_bot/commands/duck.py @@ -362,14 +362,9 @@ def message_check( ) return False - weights = ( - config.extensions.duck.success_rate.value, - 100 - config.extensions.duck.success_rate.value, - ) - # Check to see if random failure - choice_ = random.choice(random.choices([True, False], weights=weights, k=1000)) - if not choice_: + choice = self.random_choice(config) + if not choice: time = message.created_at - duck_message.created_at duration_exact = float(str(time.seconds) + "." + str(time.microseconds)) cooldowns[message.author.id] = datetime.datetime.now() @@ -399,7 +394,7 @@ def message_check( ) ) - return choice_ + return choice async def get_duck_user( self: Self, user_id: int, guild_id: int @@ -761,12 +756,7 @@ async def kill(self: Self, ctx: commands.Context) -> None: await duck_user.update(befriend_count=duck_user.befriend_count - 1).apply() - weights = ( - config.extensions.duck.success_rate.value, - 100 - config.extensions.duck.success_rate.value, - ) - - passed = random.choice(random.choices([True, False], weights=weights, k=1000)) + passed = self.random_choice(config) if not passed: await auxiliary.send_deny_embed( message="The duck got away before you could kill it.", @@ -839,12 +829,7 @@ async def donate(self: Self, ctx: commands.Context, user: discord.Member) -> Non await duck_user.update(befriend_count=duck_user.befriend_count - 1).apply() - weights = ( - config.extensions.duck.success_rate.value, - 100 - config.extensions.duck.success_rate.value, - ) - - passed = random.choice(random.choices([True, False], weights=weights, k=1000)) + passed = self.random_choice(config) if not passed: await auxiliary.send_deny_embed( message="The duck got away before you could donate it.", @@ -933,3 +918,25 @@ async def spawn(self: Self, ctx: commands.Context) -> None: message="It looks like you don't have permissions to spawn a duck", channel=ctx.channel, ) + + def random_choice(self: Self, config: munch.Munch) -> bool: + """A function to pick true or false randomly based on the success_rate in the config + + Args: + config (munch.Munch): The config for the guild + + Returns: + bool: Whether the random choice should succeed or not + """ + + weights = ( + config.extensions.duck.success_rate.value, + 100 - config.extensions.duck.success_rate.value, + ) + + # Check to see if random failure + choice_ = random.choice( + random.choices([True, False], weights=weights, k=100000) + ) + + return choice_