summary refs log tree commit diff stats
path: root/components/TossupButtons.py
blob: 73edd215b0fecd0478af2e044dc447cc3b633b46 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from discord.ui import View, button, Button
from discord import ButtonStyle, Interaction, User, Member, Color
from .AnswerModal import Answer, SoloAnswer
from typing import Union

class TossupButtons(View):
    def __init__(self, tossup) -> None:
        self.tossup = tossup
        self.already_answered = []
        self.already_voted = []
        self.i = 0
        self.final_answer_votes = 0
        super().__init__()

    @button(label="Answer!", style=ButtonStyle.green)
    async def answer(self, interaction: Interaction, button: Button):
        if interaction.user.id not in self.already_answered:
            await interaction.response.send_modal(
                Answer(self.tossup.get('formatted_answer', self.tossup['answer']), self)
            )
        else:
            await interaction.response.send_message(
                "you've already answered!", ephemeral=True
            )

    @button(label="Add sentence", style=ButtonStyle.blurple)
    async def add_sentence(self, interaction: Interaction, button: Button):
        if interaction.user.id in self.already_answered:
            return await interaction.response.send_message(
                "you've already answered!", ephemeral=True
            )
        if interaction.message is None:
            return interaction.response.send_message("Couldn't find the message!", ephemeral=True)
        e = interaction.message.embeds[0]
        self.i += 1
        if self.i == len(self.tossup["sentences"]):
            button.disabled = True
        e.description = "\n".join(self.tossup["sentences"][0 : self.i + 1])
        await interaction.response.edit_message(embed=e, view=self)

    @button(label="Vote to Reveal Answer (0/3)", style=ButtonStyle.red)
    async def reveal_answer(self, interaction: Interaction, button: Button):
        if interaction.user.id in self.already_voted:
            return await interaction.response.send_message(
                "you've already voted!", ephemeral=True
            )
        self.already_voted.append(interaction.user.id)
        self.final_answer_votes += 1
        button.label = f"vote to reveal answer ({self.final_answer_votes}/3)"
        if self.final_answer_votes >= 3:
            if interaction.message is None:
                return interaction.response.send_message("Couldn't find the message!", ephemeral=True)
            e = interaction.message.embeds[0]
            e.title = '[SKIPPED] Random Tossup'
            e.color = Color.orange()
            e.description = self.tossup["question"]
            e.add_field(name="Answer", value=self.tossup["answer"])
            for item in self.children:
                if isinstance(item, Button):
                    item.disabled = True
            return await interaction.response.edit_message(embed=e, view=self)
        await interaction.response.edit_message(view=self)
        await interaction.followup.send("you've voted!", ephemeral=True)

class SoloTossupButtons(View):
    def __init__(self, tossup, user: Union[User, Member]) -> None:
        self.user = user
        self.tossup = tossup
        self.i = 0
        super().__init__()

    @button(label="Answer!", style=ButtonStyle.green)
    async def answer(self, interaction: Interaction, button: Button):
        if interaction.user.id != self.user.id:
            return interaction.response.send_message('This is not your tossup!', ephemeral=True)
        await interaction.response.send_modal(SoloAnswer(self.tossup.get('formatted_answer', self.tossup['answer']), self))

    @button(label="Add sentence", style=ButtonStyle.blurple)
    async def add_sentence(self, interaction: Interaction, button: Button):
        if interaction.user.id != self.user.id:
            return await interaction.response.send_message(
                "not your tossup!", ephemeral=True
            )
        if interaction.message is None:
            return interaction.response.send_message("Couldn't find the message!", ephemeral=True)
        e = interaction.message.embeds[0]
        self.i += 1
        if self.i == len(self.tossup["sentences"]) - 1:
            button.disabled = True
        e.description = "\n".join(self.tossup["sentences"][0 : self.i + 1])
        await interaction.response.edit_message(embed=e, view=self)

    @button(label="Reveal Answer", style=ButtonStyle.red)
    async def reveal_answer(self, interaction: Interaction, button: Button):
        if interaction.user.id != self.user.id:
            return await interaction.response.send_message(
                "not your tossup!", ephemeral=True
            )
        if interaction.message is None:
            return await interaction.response.send_message('Error!')
        e = interaction.message.embeds[0]
        e.title = f'[SKIPPED] Random Tossup'
        e.color = Color.orange()
        e.description = self.tossup["question"]
        e.add_field(name="Answer", value=self.tossup["answer"])
        for item in self.children:
            if isinstance(item, Button):
                item.disabled = True
        return await interaction.response.edit_message(embed=e, view=self)