aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/evergreen/wonder_twins.py
blob: 475ddcb74582fd08ac68cc3869040d25feb5f11d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import random
from pathlib import Path

import yaml
from discord.ext.commands import Bot, Cog, Context, command

with open(Path.cwd() / "bot" / "resources" / "evergreen" / "wonder_twins.yaml", "r", encoding="utf-8") as f:
    info = yaml.load(f, Loader=yaml.FullLoader)
    WATER_TYPES, OBJECTS, ADJECTIVES = info["water_types"], info["objects"], info["adjectives"]


class WonderTwins(Cog):
    """Cog for a Wonder Twins inspired command."""

    def __init__(self, bot: Bot):
        self.bot = bot

    @staticmethod
    def append_onto(phrase: str, insert_word: str) -> str:
        """Appends one word onto the end of another phrase, used to format the use of 'an' or 'a'."""
        if insert_word[-1] == "s":
            phrase = phrase.split()
            del phrase[0]
            phrase = " ".join(phrase)

        insert_word = insert_word.split()[-1]
        return " ".join([phrase, insert_word])

    def format_phrase(self) -> str:
        """Creates a transformation phrase from available words."""
        adjective = random.choice((None, random.choice(ADJECTIVES)))
        object_name = random.choice(OBJECTS)
        water_type = random.choice(WATER_TYPES)

        words = [object_name, water_type]
        if adjective:
            new_object = self.append_onto(adjective, object_name)
            words[0] = new_object
        return f"{words[0]} of {words[1]}"

    @command(name="formof", aliases=["wondertwins", "wondertwin", "fo"])
    async def form_of(self, ctx: Context) -> None:
        """Command to send a Wonder Twins inspired phrase to the user invoking the command."""
        await ctx.send(f"Form of {self.format_phrase()}")


def setup(bot: Bot) -> None:
    """Load the WonderTwins cog."""
    bot.add_cog(WonderTwins(bot))