-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from mrwilson/master
feat(8ball): Add cog for !8ball
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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 | ||
|
||
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, 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 eight_ball(self, ctx: Context, *, args: str = ""): | ||
args = await clean_content().convert(ctx, args) | ||
display_name = get_name_string(ctx.message) | ||
|
||
if response := self.execute(display_name, split(args)): | ||
await ctx.send(response) | ||
|
||
|
||
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, Random(), options)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
] |