aboutsummaryrefslogtreecommitdiffstats
path: root/bot/seasons/easter/easter_riddle.py
blob: 2cae299aa276b16d0401c96c780f0c5835abe88e (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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', 'easter_riddle.json'), 'r', encoding="utf8") as f:
    RIDDLE_QUESTIONS = load(f)

TIMELIMIT = 10


class EasterRiddle(commands.Cog):
    """This cog contains the command for the Easter quiz!"""

    def __init__(self, bot):
        self.bot = bot
        self.winners = []
        self.correct = ""
        self.current_channel = None

    @commands.command(aliases=["riddlemethis", "riddleme"])
    async def riddle(self, ctx):
        """Gives a random riddle questions, then provides 2 hints at 10 second intervals before revealing the answer"""
        if not self.current_channel:
            self.current_channel = ctx.message.channel

            random_question = random.choice(RIDDLE_QUESTIONS)
            question = random_question["question"]
            hints = random_question["riddles"]
            self.correct = random_question["correct_answer"]

            description = f"You have {TIMELIMIT} seconds before the first hint.\n\n"

            q_embed = discord.Embed(title=question, description=description, colour=Colours.pink)

            await ctx.send(embed=q_embed)
            await asyncio.sleep(TIMELIMIT)

            h_embed = discord.Embed(
                title=f"Here's a hint: {hints[0]}!",
                colour=Colours.pink
            )

            await ctx.send(embed=h_embed)
            await asyncio.sleep(TIMELIMIT)

            h_embed = discord.Embed(
                title=f"Here's a hint: {hints[1]}!",
                colour=Colours.pink
            )

            await ctx.send(embed=h_embed)
            await asyncio.sleep(TIMELIMIT)

            if self.winners:
                win_list = " ".join(self.winners)
                content = f"Well done {win_list} for getting it correct!"
                self.winners = []
            else:
                content = "Nobody got it right..."

            a_embed = discord.Embed(
                title=f"The answer is: {self.correct}!",
                colour=Colours.pink
            )

            await ctx.send(content, embed=a_embed)

            self.current_channel = None

    @commands.Cog.listener()
    async def on_message(self, message):
        """If a non-bot user enters a correct answer, their username gets added to self.winners"""
        if self.current_channel == message.channel:
            if self.bot.user != message.author:
                if message.content.lower() == self.correct.lower():
                    self.winners.append(message.author.mention)
        else:
            return


def setup(bot):
    """Cog load."""
    bot.add_cog(EasterRiddle(bot))
    log.info("Easter Riddle bot loaded")