diff --git a/commands/__pycache__/help.cpython-313.pyc b/commands/__pycache__/help.cpython-313.pyc new file mode 100644 index 0000000..f55d09c Binary files /dev/null and b/commands/__pycache__/help.cpython-313.pyc differ diff --git a/commands/__pycache__/levels.cpython-313.pyc b/commands/__pycache__/levels.cpython-313.pyc new file mode 100644 index 0000000..4995e2c Binary files /dev/null and b/commands/__pycache__/levels.cpython-313.pyc differ diff --git a/commands/__pycache__/ping.cpython-313.pyc b/commands/__pycache__/ping.cpython-313.pyc new file mode 100644 index 0000000..45fe315 Binary files /dev/null and b/commands/__pycache__/ping.cpython-313.pyc differ diff --git a/commands/__pycache__/quiz.cpython-313.pyc b/commands/__pycache__/quiz.cpython-313.pyc new file mode 100644 index 0000000..6a7170d Binary files /dev/null and b/commands/__pycache__/quiz.cpython-313.pyc differ diff --git a/commands/__pycache__/sync.cpython-313.pyc b/commands/__pycache__/sync.cpython-313.pyc new file mode 100644 index 0000000..e0be655 Binary files /dev/null and b/commands/__pycache__/sync.cpython-313.pyc differ diff --git a/commands/__pycache__/tickets.cpython-313.pyc b/commands/__pycache__/tickets.cpython-313.pyc new file mode 100644 index 0000000..a382635 Binary files /dev/null and b/commands/__pycache__/tickets.cpython-313.pyc differ diff --git a/commands/help.py b/commands/help.py index 24f0ca6..66b60b8 100644 --- a/commands/help.py +++ b/commands/help.py @@ -15,6 +15,7 @@ class Help(commands.Cog): em = discord.Embed(title="Help Command", description="These are all the commands", color=interaction.user.color) em.add_field(name="Levels", value="Setup - Admins Only\nEdit - Admins Only\nRank - Show your rank\nGive - Admins Only\nRole - Admins Only") em.add_field(name="Tickets", value="Setup - Admins Only\nCreate - Create a ticket\nClose - Close your ticket\nDelete - Admins Only") + em.add_field(name="Quiz", value="Quiz - Test your knowledge\nLeaderboard - Check who has the highest score") em.add_field(name="Others", value="Ping - Show the latency of the bot") await interaction.response.send_message(embed=em) diff --git a/commands/quiz.py b/commands/quiz.py new file mode 100644 index 0000000..691119d --- /dev/null +++ b/commands/quiz.py @@ -0,0 +1,49 @@ +import discord +from discord import app_commands +from discord.ext import commands +from lib.quiz_helper import * + +class Select(discord.ui.Select): + def __init__(self): + options = [ + discord.SelectOption(label="History", emoji="1️⃣", description="Do you know your past?"), + discord.SelectOption(label="Geography", emoji="2️⃣", description="Do you know your geography?"), + discord.SelectOption(label="Pop Culture", emoji="3️⃣", description="What about pop culture?"), + discord.SelectOption(label="Math", emoji="4️⃣", description="Do you know your math?"), + discord.SelectOption(label="Riddles", emoji="5️⃣", description="Can you answer these riddles?") + ] + super().__init__(custom_id="quizSelect", placeholder="Please select only one set of questions", max_values=1, min_values=1, options=options) + + async def callback(self, interaction: discord.Interaction): + if self.values[0] == "History": + questions = load_history_questions() + for i in range(len(questions)): + em = discord.Embed(title=f"Question {i + 1}", description=questions[i]) + await interaction.response.send_message(embed=em, view=load_history_view(i, questions)) + +class SelectView(discord.ui.View): + def __init__(self): + super().__init__(timeout=None) + self.add_item(Select()) + +class Quiz(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.Cog.listener() + async def on_ready(self): + print("Quiz online") + + quiz = app_commands.Group(name="quiz", description="All the commands related to quiz") + + @quiz.command(name="leaderboard", description="See who has the highest amount of questions correct") + async def leaderboard(self, interaction: discord.Interaction): + await interaction.response.send_message(f"This command is not finished") + + @app_commands.command(name="quiz", description="Test your knowledge") + async def quiz(self, interaction: discord.Interaction): + em = discord.Embed(title="Quiz", description="Are you ready to test your knowledge? Select a theme from the dropdown below", color=interaction.user.color) + await interaction.response.send_message(embed=em, view=SelectView()) + +async def setup(bot): + await bot.add_cog(Quiz(bot)) \ No newline at end of file diff --git a/lib/__pycache__/leaderboard_helper.cpython-313.pyc b/lib/__pycache__/leaderboard_helper.cpython-313.pyc new file mode 100644 index 0000000..b490aa2 Binary files /dev/null and b/lib/__pycache__/leaderboard_helper.cpython-313.pyc differ diff --git a/lib/__pycache__/quiz_helper.cpython-313.pyc b/lib/__pycache__/quiz_helper.cpython-313.pyc new file mode 100644 index 0000000..ed2362b Binary files /dev/null and b/lib/__pycache__/quiz_helper.cpython-313.pyc differ diff --git a/lib/leaderboard_helper.py b/lib/leaderboard_helper.py new file mode 100644 index 0000000..931de77 --- /dev/null +++ b/lib/leaderboard_helper.py @@ -0,0 +1,25 @@ +import pymongo +import os + +client = pymongo.MongoClient(os.getenv('mongo_url')) +db = client.quiz +coll = db.leaderboard + +def increment_history_correct(user_id): + user = coll.find_one({"_id": user_id}) + + if user is None: + coll.insert_one({"_id": user_id, "amount_correct": 1}) + return True + + coll.update_one({"_id": user_id}, {"$inc": {"amount_correct": 1}}) + return True + +def get_correct_answers(user_id): + user = coll.find_one({"_id": user_id}) + + if user is None: + return 0 + + correct = int(user["amount_correct"]) + return correct \ No newline at end of file diff --git a/lib/quiz_helper.py b/lib/quiz_helper.py new file mode 100644 index 0000000..63464cd --- /dev/null +++ b/lib/quiz_helper.py @@ -0,0 +1,128 @@ +import discord +import random +from lib.leaderboard_helper import * + +def load_history_questions(): + questions = [ + "When did the second world war start (based on allied views)?", + "When did the second world war end?", + "Which of these was one of the superpowers in the cold war?", + "Who was the loader of North Korea in 1945?", + "Which country here gained independence between 1945 and 1950?", + "When was the first Punic War?", + "Who was the leader of the Soviet Union in 1957?", + "Who was the leader of the United States in 1966?", + "What combat method was used by the Vietnam in the Vietnam war?", + "What was the 1st Reich?", + "Which battle defeated Harald Hardrada of Norway in 1066?", + "Approximately how many people died in World War 2?", + "What ideology was Hungary in 1919?", + "Who developed the ideology of Communism?", + "What happened in Tiananmen Square?" + ] + + return questions + +def load_history_answers(question): + answers = [ + '0 1939', + '1 1945', + '2 USA,USSR', + '3 Terentii Shtykov,Kim Il-Sung', + '4 Vietnam,Indonesia,Philippines,Jordan,India,Pakistan,Myanmar,SriLanka (Ceylon),Israel,Laos,Cambodia,Syria', + '5 264 BC - 241 BC', + '6 Nikita Khrushchev', + '7 Lyndon B Johnson', + '8 Guerrilla Warfare', + '9 Holy Roman Empire', + '10 Battle of Stamford Bridge', + '11 70 - 85 million', + '12 Communist', + '13 Karl Marx', + '14 Nothing,Massacre' + ] + + answer = answers[question] + if ',' in answer: + only_answer = answer.split(f"{str(question)} ")[1] + possible_answers = only_answer.split(',') + return possible_answers + else: + return answer.split(f"{str(question)} ")[1] + +def load_history_choices(question): + choices = [ + '0 1940,1938,1914', + '1 1946,1944,1918,1920', + '2 China,The UK,Germany', + '3 Syngman Rhee,Chiang Kai-shek,Kim Jong Un', + '4 Bahrain,Canada,Australia', + '5 300 BC - 200 BC,264 BC - 146 BC,120 BC -116 BC', + '6 Leonid Brezhnev,Joseph Stalin,Mikhail Gorbachev', + '7 John F. Kennedy,Dwight D. Eisenhower,Richard Nixon', + '8 Air Strikes,Nuclear Warfare,Hand-to-Hand Combat', + '9 Byzantine Empire,Ottoman Empire,Russian Empire', + '10 Battle of Hastings,Battle of Waterloo,Battle of Gettysburg', + '11 100 - 120 million,15 to 30 million,30 to 45 million', + '12 Capitalist,Monarchist,Fascist', + '13 Vladimir Lenin,Joseph Stalin,Friedrich Engels', + '14 The Fall of the Great Wall of China,A Soccer/Football Match,The signing of the Magna Carta' + ] + + choice = choices[question] + only_choice = choice.split(f"{str(question)} ")[1] + question_choices = only_choice.split(',') + return question_choices + +class load_history_select(discord.ui.Select): + def __init__(self, question, questions): + super().__init__() + self.question = question + self.questions = questions + self.answer = load_history_answers(question) + self.correct = 0 + choices = load_history_choices(question) + + options = [] + responses = [] + + if isinstance(self.answer, list): + for ans in self.answer: + responses.append(ans) + else: + responses.append(self.answer) + + for choice in choices: + responses.append(choice) + + while responses: + response = random.choice(responses) + options.append(discord.SelectOption(label=response)) + responses.remove(response) + + self.options = options + + async def callback(self, interaction: discord.Interaction): + if isinstance(self.answer, list): + if any(value in self.answer for value in self.values): + self.correct += 1 + increment_history_correct(interaction.user.id) + else: + if self.values[0] == self.answer: + increment_history_correct(interaction.user.id) + + await interaction.response.send_message("Answer recorded..") + + # Send the next question if available + next_question_index = self.question + 1 + if next_question_index < len(self.questions): + next_question = self.questions[next_question_index] + em = discord.Embed(title=f"Question {next_question_index + 1}", description=next_question) + await interaction.followup.send(embed=em, view=load_history_view(next_question_index, self.questions)) + else: + await interaction.followup.send(f"You finished the quiz. You got {self.correct} answers correct!") + +class load_history_view(discord.ui.View): + def __init__(self, question_index, questions): + super().__init__(timeout=None) + self.add_item(load_history_select(question_index, questions)) \ No newline at end of file