From 230240ae704326ae60d6350d36b5247a07a423e7 Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Sat, 27 Jul 2024 11:55:04 +0100 Subject: [PATCH 1/2] feat(8ball): Add cog for !8ball It just picks a random response from the list of standard options --- apollo.py | 1 + cogs/commands/eightball.py | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 cogs/commands/eightball.py diff --git a/apollo.py b/apollo.py index fc6ee9d..159e230 100644 --- a/apollo.py +++ b/apollo.py @@ -25,6 +25,7 @@ "cogs.commands.chatgpt", "cogs.commands.dalle", "cogs.commands.date", + "cogs.commands.eightball", "cogs.commands.event_sync", "cogs.commands.flip", "cogs.commands.karma_admin", diff --git a/cogs/commands/eightball.py b/cogs/commands/eightball.py new file mode 100644 index 0000000..53d7993 --- /dev/null +++ b/cogs/commands/eightball.py @@ -0,0 +1,62 @@ +import random +import shlex +import string +from typing import List + +from discord.ext import commands +from discord.ext.commands import Bot, Context, clean_content + +from utils import get_name_string + +LONG_HELP_TEXT = """ +Shakes a Magic 8-ball to determine the answer to any question you ask e.g. `!8ball should I get a takeaway?` +""" + +SHORT_HELP_TEXT = """Asks a question to a Magic 8-ball""" + + +class EightBall(commands.Cog): + def __init__(self, bot: Bot, options: List[string]): + self.bot = bot + self.options = options + + @commands.hybrid_command(name="8ball", help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) + async def execute(self, ctx: Context, *, args: str = ""): + args = await clean_content().convert(ctx, args) + args = shlex.split(args) + display_name = get_name_string(ctx.message) + + if len(args) == 1: + await ctx.send(f"You must pose the Magic 8-ball a dilemma {display_name}!") + else: + await ctx.send(f"{display_name}: {(random.choice(self.options))}") + + +async def setup(bot: Bot): + options = [ + # Positive + "It is certain", + "It is decidedly so", + "Without a doubt", + "Yes definitely", + "You may rely on it", + "As I see it, yes", + "Most likely", + "Outlook good", + "Yes", + "Signs point to yes", + # Indeterminate + "Reply hazy, try again", + "Ask again later", + "Better not tell you now", + "Cannot predict now", + "Concentrate and ask again", + # Negative + "Don't count on it", + "My reply is no", + "My sources say no", + "Outlook not so good", + "Very doubtful", + ] + + await bot.add_cog(EightBall(bot, options)) From 827df42ad1b2567e6124d52aa5fd75ec4716fc25 Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Sat, 27 Jul 2024 12:32:20 +0100 Subject: [PATCH 2/2] test(8ball): Refactor to allow testing of choice logic We parametrise the class with a source of randomness so it can be fixed with a seed in the tests --- cogs/commands/eightball.py | 27 +++++++++++++++------------ tests/test_eightball.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 tests/test_eightball.py diff --git a/cogs/commands/eightball.py b/cogs/commands/eightball.py index 53d7993..040ba97 100644 --- a/cogs/commands/eightball.py +++ b/cogs/commands/eightball.py @@ -1,7 +1,6 @@ -import random -import shlex -import string -from typing import List +from random import Random +from shlex import split +from typing import List, Optional from discord.ext import commands from discord.ext.commands import Bot, Context, clean_content @@ -16,20 +15,24 @@ class EightBall(commands.Cog): - def __init__(self, bot: Bot, options: List[string]): + def __init__(self, bot: Bot, random: Random, options: List[str]): self.bot = bot + self.random = random self.options = options + def execute(self, author: str, args: List[str]) -> Optional[str]: + if len(args) <= 1: + return f"{author}: You must pose the Magic 8-ball a dilemma!" + else: + return f"{author}: {(self.random.choice(self.options))}" + @commands.hybrid_command(name="8ball", help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) - async def execute(self, ctx: Context, *, args: str = ""): + async def eight_ball(self, ctx: Context, *, args: str = ""): args = await clean_content().convert(ctx, args) - args = shlex.split(args) display_name = get_name_string(ctx.message) - if len(args) == 1: - await ctx.send(f"You must pose the Magic 8-ball a dilemma {display_name}!") - else: - await ctx.send(f"{display_name}: {(random.choice(self.options))}") + if response := self.execute(display_name, split(args)): + await ctx.send(response) async def setup(bot: Bot): @@ -59,4 +62,4 @@ async def setup(bot: Bot): "Very doubtful", ] - await bot.add_cog(EightBall(bot, options)) + await bot.add_cog(EightBall(bot, Random(), options)) diff --git a/tests/test_eightball.py b/tests/test_eightball.py new file mode 100644 index 0000000..f90002f --- /dev/null +++ b/tests/test_eightball.py @@ -0,0 +1,35 @@ +from random import Random + +from discord import Intents +from discord.ext.commands import Bot + +from cogs.commands.eightball import EightBall + + +def test_eightball_no_question(): + eightball = EightBall(Bot("/", intents=Intents()), Random(1), []) + + assert ( + eightball.execute("test", []) + == "test: You must pose the Magic 8-ball a dilemma!" + ) + + +def test_eightball_question_with_fixed_randomness(): + eightball = EightBall( + Bot("/", intents=Intents()), Random(1), ["Yes", "No", "Maybe"] + ) + + answers = [ + eightball.execute("test", ["Should", "I", "eat", "cake?"]) for i in range(0, 7) + ] + + assert answers == [ + "test: Yes", + "test: Maybe", + "test: Yes", + "test: No", + "test: Yes", + "test: No", + "test: No", + ]