From d337a1f6079a10bbbb97ef44c5cddecc4468d2ff Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:40:22 -0500 Subject: [PATCH 1/3] Make duck random better --- techsupport_bot/commands/duck.py | 48 +++++++++++++++++++------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/techsupport_bot/commands/duck.py b/techsupport_bot/commands/duck.py index 1f7cdfe2..d8f38758 100644 --- a/techsupport_bot/commands/duck.py +++ b/techsupport_bot/commands/duck.py @@ -125,6 +125,7 @@ async def wait(self: Self, config: munch.Munch, _: discord.Guild) -> None: Args: config (munch.Munch): The guild config to use to determine the min and max wait times """ + await asyncio.sleep(1) await asyncio.sleep( random.randint( config.extensions.duck.min_wait.value * 3600, @@ -361,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() @@ -398,7 +394,7 @@ def message_check( ) ) - return choice_ + return choice async def get_duck_user( self: Self, user_id: int, guild_id: int @@ -760,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.", @@ -838,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.", @@ -932,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: + ctx (commands.Context): The context in which the command was run + + 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_ From 8aab3ae9b5a2b852b8b697bbe27db0eaedaf6f9d Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:43:47 -0500 Subject: [PATCH 2/3] Update docstring --- techsupport_bot/commands/duck.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/techsupport_bot/commands/duck.py b/techsupport_bot/commands/duck.py index d8f38758..e82a6d29 100644 --- a/techsupport_bot/commands/duck.py +++ b/techsupport_bot/commands/duck.py @@ -923,7 +923,7 @@ 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: - ctx (commands.Context): The context in which the command was run + config (munch.Munch): The config for the guild Returns: bool: Whether the random choice should succeed or not From b889b601bc1b43c8d898975da5ad06ec431ba528 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Thu, 24 Apr 2025 12:42:41 -0400 Subject: [PATCH 3/3] Remove useless test line --- techsupport_bot/commands/duck.py | 1 - 1 file changed, 1 deletion(-) diff --git a/techsupport_bot/commands/duck.py b/techsupport_bot/commands/duck.py index c57d5191..71734f6c 100644 --- a/techsupport_bot/commands/duck.py +++ b/techsupport_bot/commands/duck.py @@ -126,7 +126,6 @@ async def wait(self: Self, config: munch.Munch, _: discord.Guild) -> None: Args: config (munch.Munch): The guild config to use to determine the min and max wait times """ - await asyncio.sleep(1) await asyncio.sleep( random.randint( config.extensions.duck.min_wait.value * 3600,