diff options
| -rw-r--r-- | bot/resources/easter/egghead_questions.json | 181 | ||||
| -rw-r--r-- | bot/seasons/easter/egghead_quiz.py | 121 | 
2 files changed, 302 insertions, 0 deletions
| diff --git a/bot/resources/easter/egghead_questions.json b/bot/resources/easter/egghead_questions.json new file mode 100644 index 00000000..e4e21ebe --- /dev/null +++ b/bot/resources/easter/egghead_questions.json @@ -0,0 +1,181 @@ +[ +    { +        "question": "Where did the idea of the Easter Bunny originate?", +        "answers": [ +            "Russia", +            "The United States", +            "The UK", +            "Germany" +        ], +        "correct_answer": 3 +    }, +    { +        "question": "The Easter Bunny was originally going to be a...", +        "answers": [ +            "hare", +            "possum", +            "cat", +            "dove" +        ], +        "correct_answer": 0 +    }, +    { +        "question": "Which of the following is NOT a movie about Easter?", +        "answers": [ +            "Winnie the Pooh - Springtime with Roo", +            "It's a Wonderful Life", +            "The Passion of the Christ", +            "Here Comes Peter Cottontail" +        ], +        "correct_answer": 1 +    }, +    { +        "question": "In Australia, what animal is used instead of the Easter Bunny?", +        "answers": [ +            "kangaroo", +            "wombat", +            "koala", +            "bilby" +        ], +        "correct_answer": 3 +    }, +    { +        "question": "When was the first Earth Day?", +        "answers": [ +            "1982", +            "2003", +            "1999", +            "1970" +        ], +        "correct_answer": 2 +    }, +    { +        "question": "Who is considered to be the founder of Earth Day?", +        "answers": [ +            "President Jimmy Carter", +            "President John F. Kennedy", +            "Vice President Al Gore", +            "Senator Gaylord Nelson" +        ], +        "correct_answer": 3 +    }, +    { +        "question": "Approximately how many countries participated in Earth Day 2000?", +        "answers": [ +            "60", +            "140", +            "180", +            "240" +        ], +        "correct_answer": 2 +    }, +    { +        "question": "As Earth Day is this month, how old is the Earth?", +        "answers": [ +            "4.5 billion years old", +            "5 million years old", +            "10 billion years old", +            "6.7 billion years old" +        ], +        "correct_answer": 0 +    }, +    { +        "question": "As a celebration of Earth Day, what is the percentage of Oxygen in the Earth's atmosphere?", +        "answers": [ +            "18%", +            "21%", +            "25%", +            "31%" +        ], +        "correct_answer": 1 +    }, +    { +        "question": "In what year did Google begin its tradition of April Fools Jokes?", +        "answers": [ +            "1997", +            "2000", +            "2003", +            "2007" +        ], +        "correct_answer": 1 +    }, +    { +        "question": "Which type of chocolate is the most healthy?", +        "answers": [ +            "Dark", +            "White", +            "Milk" +        ], +        "correct_answer": 0 +    }, +    { +        "question": "How many bars of milk chocolate would you have to eat to get the same amount of caffeine as in one cup of coffee?", +        "answers": [ +            "3", +            "9", +            "14", +            "20" +        ], +        "correct_answer": 2 +    }, +    { +        "question": "Aztecs used to use one of the ingedients of chocolate, cocoa beans, as...", +        "answers": [ +            "currency", +            "medicine", +            "dye", +            "fertilizer" +        ], +        "correct_answer": 0 +    }, +    { +        "question": "Which European country was the first to enjoy chocolate?", +        "answers": [ +            "France", +            "Spain", +            "England", +            "Switzerland" +        ], +        "correct_answer": 1 +    }, +    { +        "question": "The first European Chocolate Shop opened in what city in 1657?", +        "answers": [ +            "Paris, France", +            "Madrid, Spain", +            "Zürich, Switzerland", +            "London, England" +        ], +        "correct_answer": 3 +    }, +    { +        "question": "On average, how many eggs does a hen lay in a year?", +        "answers": [ +            "Between 200-230", +            "Between 250-270", +            "Between 300-330", +            "Between 370-400" +        ], +        "correct_answer": 1 +    }, +    { +        "question": "What determines the colour of an egg yolk?", +        "answers": [ +            "The size of the hen", +            "The age of a hen", +            "The diet of a hen", +            "The colour of a hen's feathers" +        ], +        "correct_answer": 2 +    }, +    { +        "question": "What country produces the most eggs in a year?", +        "answers": [ +            "China", +            "India", +            "The United States", +            "Japan" +        ], +        "correct_answer": 0 +    } +]
\ No newline at end of file diff --git a/bot/seasons/easter/egghead_quiz.py b/bot/seasons/easter/egghead_quiz.py new file mode 100644 index 00000000..8dd2c21d --- /dev/null +++ b/bot/seasons/easter/egghead_quiz.py @@ -0,0 +1,121 @@ +import asyncio +import logging +import random +from json import load +from pathlib import Path + +import discord +from discord.ext import commands + +from bot.constants import Colours + +log = logging.getLogger(__name__) + +with open(Path('bot', 'resources', 'easter', 'egghead_questions.json'), 'r', encoding="utf8") as f: +    EGGHEAD_QUESTIONS = load(f) + + +EMOJIS = [ +    '\U0001f1e6', '\U0001f1e7', '\U0001f1e8', '\U0001f1e9', '\U0001f1ea', +    '\U0001f1eb', '\U0001f1ec', '\U0001f1ed', '\U0001f1ee', '\U0001f1ef', +    '\U0001f1f0', '\U0001f1f1', '\U0001f1f2', '\U0001f1f3', '\U0001f1f4', +    '\U0001f1f5', '\U0001f1f6', '\U0001f1f7', '\U0001f1f8', '\U0001f1f9', +    '\U0001f1fa', '\U0001f1fb', '\U0001f1fc', '\U0001f1fd', '\U0001f1fe', +    '\U0001f1ff' +]  # Regional Indicators A-Z (used for voting) + +TIMELIMIT = 30 + + +class EggheadQuiz(commands.Cog): +    """This cog contains the command for the Easter quiz!""" + +    def __init__(self, bot): +        self.bot = bot +        self.quiz_messages = {} + +    @commands.command(aliases=["eggheadquiz", "easterquiz"]) +    async def eggquiz(self, ctx): +        """ +        Gives a random quiz question, waits 30 seconds and then outputs the answer + +        Also informs of the percentages and votes of each option +        """ + +        random_question = random.choice(EGGHEAD_QUESTIONS) +        question, answers = random_question["question"], random_question["answers"] +        answers = [(EMOJIS[i], a) for i, a in enumerate(answers)] +        correct = EMOJIS[random_question["correct_answer"]] + +        valid_emojis = [emoji for emoji, _ in answers] + +        description = f"You have {TIMELIMIT} seconds to vote.\n\n" +        description += "\n".join([f"{emoji} -> **{answer}**" for emoji, answer in answers]) + +        q_embed = discord.Embed(title=question, description=description, colour=Colours.pink) + +        msg = await ctx.send(embed=q_embed) +        for emoji in valid_emojis: +            await msg.add_reaction(emoji) + +        self.quiz_messages[msg.id] = valid_emojis + +        await asyncio.sleep(TIMELIMIT) + +        del self.quiz_messages[msg.id] + +        msg = await ctx.channel.fetch_message(msg.id)  # Refreshes message + +        total_no = sum([len(await r.users().flatten()) for r in msg.reactions]) - len(valid_emojis)  # - bot's reactions + +        if total_no == 0: +            return await msg.delete()  # to avoid ZeroDivisionError if nobody reacts + +        results = ["**VOTES:**"] +        for emoji, _ in answers: +            num = [len(await r.users().flatten()) for r in msg.reactions if str(r.emoji) == emoji][0] - 1 +            percent = round(100 * num / total_no) +            s = "" if num == 1 else "s" +            string = f"{emoji} - {num} vote{s} ({percent}%)" +            results.append(string) + +        mentions = " ".join([ +            u.mention for u in [ +                await r.users().flatten() for r in msg.reactions if str(r.emoji) == correct +            ][0] if not u.bot +        ]) + +        content = f"Well done {mentions} for getting it correct!" if mentions else "Nobody got it right..." + +        a_embed = discord.Embed( +            title=f"The correct answer was {correct}!", +            description="\n".join(results), +            colour=Colours.pink +        ) + +        await ctx.send(content, embed=a_embed) + +    @staticmethod +    async def already_reacted(message, user): +        """Returns whether a given user has reacted more than once to a given message""" +        users = [u.id for reaction in [await r.users().flatten() for r in message.reactions] for u in reaction] +        return users.count(user.id) > 1  # Old reaction plus new reaction + +    @commands.Cog.listener() +    async def on_reaction_add(self, reaction, user): +        """Listener to listen specifically for reactions of quiz messages""" +        if user.bot: +            return +        if reaction.message.id not in self.quiz_messages: +            return +        if str(reaction.emoji) not in self.quiz_messages[reaction.message.id]: +            return await reaction.message.remove_reaction(reaction, user) +        if await self.already_reacted(reaction.message, user): +            return await reaction.message.remove_reaction(reaction, user) + + +def setup(bot): +    """Cog load.""" + +    bot.add_cog(EggheadQuiz(bot)) +    log.info("EggheadQuiz bot loaded") | 
