diff options
Diffstat (limited to 'components')
-rw-r--r-- | components/AnswerModal.py | 85 | ||||
-rw-r--r-- | components/TossupButtons.py | 99 | ||||
-rw-r--r-- | components/__pycache__/AnswerModal.cpython-311.pyc | bin | 0 -> 7397 bytes | |||
-rw-r--r-- | components/__pycache__/TossupButtons.cpython-311.pyc | bin | 0 -> 8765 bytes |
4 files changed, 184 insertions, 0 deletions
diff --git a/components/AnswerModal.py b/components/AnswerModal.py new file mode 100644 index 0000000..9bd5f17 --- /dev/null +++ b/components/AnswerModal.py @@ -0,0 +1,85 @@ +from discord.ui import Modal, TextInput, View +from httpx import AsyncClient +from discord import Interaction, Color + + +class Answer(Modal, title="Submit Answer"): + def __init__(self, correct_answer: str, view: View) -> None: + self.correct_answer = correct_answer + self.view = view + super().__init__() + + answer = TextInput(label="Answer", placeholder="Your answer here") + + async def on_submit(self, interaction: Interaction) -> None: + c = AsyncClient() + answer_check_resp = await c.get( + "https://qbreader.org/api/check-answer", + params={ + "answerline": self.correct_answer, + "givenAnswer": self.answer.value, + }, + ) + answer_check_data = answer_check_resp.json() + + if answer_check_data["directive"] == "accept": + e = interaction.message.embeds[0] + e.color = Color.green() + e.title = '[CORRECT!] Random Tossup' + e.description = ".".join(self.view.tossup['sentences'][0:self.view.i+1]) + " **(BUZZ)** " + ".".join(self.view.tossup['sentences'][self.view.i+1:]) + e.add_field(name='Your answer', value=self.answer.value) + e.add_field(name='Official answer', value=self.view.tossup['answer']) + e.add_field(name='Answered by', value=interaction.user.mention) + items = self.view.children + for item in items: + item.disabled = True + await interaction.response.edit_message(embed=e, view=self.view) + elif answer_check_data['directive'] == 'prompt': + await interaction.response.send_message("Prompt! Try answering the question again", ephemeral=True) + else: + await interaction.response.send_message(f"Incorrect! You've been locked out from the question. The correct answer was {self.view.tossup['answer']}", ephemeral=True) + self.view.already_answered.append(interaction.user.id) + await c.aclose() + +class SoloAnswer(Modal, title="Submit Answer"): + def __init__(self, correct_answer: str, view: View) -> None: + self.correct_answer = correct_answer + self.view = view + super().__init__() + + answer = TextInput(label="Answer", placeholder="your answer here!") + + async def on_submit(self, interaction: Interaction) -> None: + c = AsyncClient() + answer_check_resp = await c.get( + "https://qbreader.org/api/check-answer", + params={ + "answerline": self.correct_answer, + "givenAnswer": self.answer.value, + }, + ) + answer_check_data = answer_check_resp.json() + + if answer_check_data["directive"] == "accept": + e = interaction.message.embeds[0] + e.color = Color.green() + e.title = '[CORRECT!] Random Tossup' + e.description = ".".join(self.view.tossup['sentences'][0:self.view.i+1]) + " **(BUZZ)** " + ".".join(self.view.tossup['sentences'][self.view.i+1:]) + e.add_field(name='Your answer', value=self.answer.value) + e.add_field(name='Official answer', value=self.view.tossup['answer']) + items = self.view.children + for item in items: + item.disabled = True + await interaction.response.edit_message(embed=e, view=self.view) + else: + e = interaction.message.embeds[0] + e.color = Color.red() + e.title = '[INCORRECT] Random Tossup' + e.description = ".".join(self.view.tossup['sentences'][0:self.view.i+1]) + " **(BUZZ)** " + ".".join(self.view.tossup['sentences'][self.view.i+1:]) + e.add_field(name='Correct answer was...', value=self.view.tossup['answer']) + e.add_field(name='Your answer', value=self.answer.value) + items = self.view.children + for item in items: + item.disabled = True + await interaction.response.edit_message(embed=e, view=self.view) + await c.aclose() diff --git a/components/TossupButtons.py b/components/TossupButtons.py new file mode 100644 index 0000000..7238cac --- /dev/null +++ b/components/TossupButtons.py @@ -0,0 +1,99 @@ +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 + ) + 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: + 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: + 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 + ) + 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 + ) + 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: + item.disabled = True + return await interaction.response.edit_message(embed=e, view=self) diff --git a/components/__pycache__/AnswerModal.cpython-311.pyc b/components/__pycache__/AnswerModal.cpython-311.pyc new file mode 100644 index 0000000..0ca3cb1 --- /dev/null +++ b/components/__pycache__/AnswerModal.cpython-311.pyc Binary files differdiff --git a/components/__pycache__/TossupButtons.cpython-311.pyc b/components/__pycache__/TossupButtons.cpython-311.pyc new file mode 100644 index 0000000..fc443d0 --- /dev/null +++ b/components/__pycache__/TossupButtons.cpython-311.pyc Binary files differ |