From 5e73471999330b4bfd12437c90ee33c9b0c02656 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Mon, 7 Oct 2019 21:29:34 +0530 Subject: Corrected a few typos in the json file and also the following refinements for the quiz game: New scoreboard for every game. Store overall score board which refreshs when bot restarts. --- bot/resources/evergreen/trivia_quiz.json | 3 +- bot/seasons/evergreen/trivia_quiz.py | 82 ++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/bot/resources/evergreen/trivia_quiz.json b/bot/resources/evergreen/trivia_quiz.json index 1ad2a1e1..66df3df7 100644 --- a/bot/resources/evergreen/trivia_quiz.json +++ b/bot/resources/evergreen/trivia_quiz.json @@ -71,6 +71,7 @@ { "id": 106, "question": "Which country is known as the \"Land of Thunderbolt\"?", + "answer": "Bhutan", "info": "Bhutan is known as the \"Land of Thunder Dragon\" or \"Land of Thunderbolt\" due to the violent and large thunderstorms that whip down through the valleys from the Himalayas. The dragon reference was due to people thinking the sparkling light of thunderbolts was the red fire of a dragon." }, { @@ -129,7 +130,7 @@ }, { "id": 116, - "question": "The Vally Of The Kings is located in which country?", + "question": "The Valley Of The Kings is located in which country?", "answer": "Egypt", "info": "The Valley of the Kings, also known as the Valley of the Gates of the Kings, is a valley in Egypt where, for a period of nearly 500 years from the 16th to 11th century BC, rock cut tombs were excavated for the pharaohs and powerful nobles of the New Kingdom (the Eighteenth to the Twentieth Dynasties of Ancient Egypt)." }, diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 798523e6..1c2ba969 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -28,10 +28,11 @@ class TriviaQuiz(commands.Cog): def __init__(self, bot: commands.Bot) -> None: self.bot = bot self.questions = self.load_questions() - self.game_status = {} - self.game_owners = {} + self.game_status = {} # A variable to store the game status: either running or not running. + self.game_owners = {} # A variable to store the person's ID who started the quiz game in a channel. self.question_limit = 4 - self.player_dict = {} + self.player_scores = {} # A variable to store all player's scores for a bot session. + self.game_player_scores = {} # A variable to store temporary game player's scores. self.categories = { "general": "Test your general knowledge" # "retro": "Questions related to retro gaming." @@ -50,21 +51,22 @@ class TriviaQuiz(commands.Cog): """ Start/Stop a quiz! - arguments: - option: - - start : to start a quiz in a channel - - stop : stop the quiz running in that channel. + If the quiz game is running, then the owner or a mod can stop it by using this command + without providing any arguments and vice versa. Questions for the quiz can be selected from the following categories: - general : Test your general knowledge. (default) - (we wil be adding more later) + (we wil be adding more later.) """ category = category.lower() if ctx.channel.id not in self.game_status: self.game_status[ctx.channel.id] = False - self.player_dict[ctx.channel.id] = {} + if ctx.channel.id not in self.game_player_scores: + self.game_player_scores[ctx.channel.id] = {} + + # Start game if not running. if not self.game_status[ctx.channel.id]: self.game_owners[ctx.channel.id] = ctx.author self.game_status[ctx.channel.id] = True @@ -77,16 +79,18 @@ class TriviaQuiz(commands.Cog): ) await ctx.send(embed=start_embed) # send an embed with the rules await asyncio.sleep(1) - + # Stop game is running. else: + # Check if the author is the owner or a mod. if ( ctx.author == self.game_owners[ctx.channel.id] or Roles.moderator in [role.id for role in ctx.author.roles] ): await ctx.send("Quiz is no longer running.") - await self.declare_winner(ctx.channel, self.player_dict[ctx.channel.id]) + await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) self.game_status[ctx.channel.id] = False del self.game_owners[ctx.channel.id] + self.game_player_scores[ctx.channel.id] = {} else: await ctx.send(f"{ctx.author.mention}, you are not authorised to stop this game :ghost: !") @@ -94,21 +98,23 @@ class TriviaQuiz(commands.Cog): embed = self.category_embed await ctx.send(embed=embed) return + topic = self.questions[category] - unanswered = 0 done_question = [] hint_no = 0 answer = None while self.game_status[ctx.channel.id]: + + # Exit quiz if number of questions for a round are already sent. if len(done_question) > self.question_limit and hint_no == 0: await ctx.send("The round ends here.") - await self.declare_winner(ctx.channel, self.player_dict[ctx.channel.id]) - break - if unanswered > 3: - await ctx.send("Game stopped due to inactivity.") - await self.declare_winner(ctx.channel, self.player_dict[ctx.channel.id]) + await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) + self.game_status[ctx.channel.id] = False + del self.game_owners[ctx.channel.id] + self.game_player_scores[ctx.channel.id] = {} break + # If no hint has been sent or any time alert. Basically if hint_no = 0 means it is a new question. if hint_no == 0: while True: question_dict = random.choice(topic) @@ -121,16 +127,20 @@ class TriviaQuiz(commands.Cog): embed = discord.Embed(colour=discord.Colour.gold()) embed.title = f"Question #{len(done_question)}" embed.description = q - await ctx.send(embed=embed) + await ctx.send(embed=embed) # Send question embed. + # A function to check whether user input is the correct answer(close to the right answer) def check(m: discord.Message) -> bool: ratio = fuzz.ratio(answer.lower(), m.content.lower()) return ratio > 85 and m.channel == ctx.channel try: msg = await self.bot.wait_for('message', check=check, timeout=10) except asyncio.TimeoutError: + # In case of TimeoutError and the game has been stopped, then do nothing. if self.game_status[ctx.channel.id] is False: break + + # if number of hints sent or time alerts sent is less than 2, then send one. if hint_no < 2: hint_no += 1 if "hints" in question_dict: @@ -139,36 +149,56 @@ class TriviaQuiz(commands.Cog): else: await ctx.send(f"Cmon guys, {30-hint_no*10}s left!") + # Once hint or time alerts has been sent 2 times, the hint_no value will be 3 + # If hint_no > 2, then it means that all hints/time alerts have been sent. + # Also means that the answer is not yet given and the bot sends the answer and the next question. else: response = random.choice(WRONG_ANS_RESPONSE) expression = random.choice(ANNOYED_EXPRESSIONS) await ctx.send(f"{response} {expression}") await self.send_answer(ctx.channel, question_dict) await asyncio.sleep(1) - hint_no = 0 - unanswered += 1 - await self.send_score(ctx.channel, self.player_dict[ctx.channel.id]) + + hint_no = 0 # init hint_no = 0 so that 2 hints/time alerts can be sent for the new question. + + await self.send_score(ctx.channel, self.game_player_scores[ctx.channel.id]) await asyncio.sleep(2) else: + # Reduce points by 25 for every hint/time alert that has been sent. points = 100 - 25*hint_no - if msg.author in self.player_dict[ctx.channel.id]: - self.player_dict[ctx.channel.id][msg.author] += points + if msg.author in self.game_player_scores[ctx.channel.id]: + self.game_player_scores[ctx.channel.id][msg.author] += points + else: + self.game_player_scores[ctx.channel.id][msg.author] = points + + # Also updating the overall scoreboard. + if msg.author in self.player_scores: + self.player_scores[msg.author] += points else: - self.player_dict[ctx.channel.id][msg.author] = points + self.player_scores[msg.author] = points + hint_no = 0 - unanswered = 0 + await ctx.send(f"{msg.author.mention} got the correct answer :tada: {points} points for ya.") await self.send_answer(ctx.channel, question_dict) - await self.send_score(ctx.channel, self.player_dict[ctx.channel.id]) + await self.send_score(ctx.channel, self.game_player_scores[ctx.channel.id]) await asyncio.sleep(2) + @commands.command(name="scoreboard") + async def overall_scoreboard(self, ctx): + """View everyone's score for this bot session.""" + await self.send_score(ctx.channel, self.player_scores) + @staticmethod async def send_score(channel: discord.TextChannel, player_data: dict) -> None: """A function which sends the score.""" embed = discord.Embed(colour=discord.Colour.blue()) embed.title = "Score Board" embed.description = "" + if len(player_data) == 0: + await channel.send("No one has made it to the scoreboard yet.") + return for k, v in player_data.items(): embed.description += f"{k} : {v}\n" await channel.send(embed=embed) -- cgit v1.2.3 From d7510a32f408c7cdca3355051987e7df0806b3b4 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Mon, 7 Oct 2019 21:38:24 +0530 Subject: added function annotations to new command --- bot/seasons/evergreen/trivia_quiz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 1c2ba969..5b8eb65e 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -186,7 +186,7 @@ class TriviaQuiz(commands.Cog): await asyncio.sleep(2) @commands.command(name="scoreboard") - async def overall_scoreboard(self, ctx): + async def overall_scoreboard(self, ctx: commands.Context)-> None: """View everyone's score for this bot session.""" await self.send_score(ctx.channel, self.player_scores) -- cgit v1.2.3 From da1596f983e5ef19a56da5f9a62ffcbd8969283d Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Mon, 7 Oct 2019 21:43:15 +0530 Subject: fixed lint errors --- bot/seasons/evergreen/trivia_quiz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 5b8eb65e..9a5cf43c 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -186,7 +186,7 @@ class TriviaQuiz(commands.Cog): await asyncio.sleep(2) @commands.command(name="scoreboard") - async def overall_scoreboard(self, ctx: commands.Context)-> None: + async def overall_scoreboard(self, ctx: commands.Context) -> None: """View everyone's score for this bot session.""" await self.send_score(ctx.channel, self.player_scores) -- cgit v1.2.3 From 137a90c323364f8e521a9843cb8b929d7e2b90b4 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Sun, 13 Oct 2019 21:47:25 +0530 Subject: Updated the code as per the suggestions and they are the following: - Corrected all typos. - the quiz command is now a command group to allocate the leaderboard command(a command which shows the leaderboard for the current bot session). - Few other refinements with language and stuff. --- bot/seasons/evergreen/trivia_quiz.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 9a5cf43c..f7ad9f3a 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -46,7 +46,7 @@ class TriviaQuiz(commands.Cog): questions = json.load(json_data) return questions - @commands.command(name="quiz", aliases=["trivia"]) + @commands.group(name="quiz", aliases=["trivia"], invoke_without_command=True) async def quiz_game(self, ctx: commands.Context, category: str = "general") -> None: """ Start/Stop a quiz! @@ -56,7 +56,7 @@ class TriviaQuiz(commands.Cog): Questions for the quiz can be selected from the following categories: - general : Test your general knowledge. (default) - (we wil be adding more later.) + (More to come!) """ category = category.lower() @@ -79,12 +79,12 @@ class TriviaQuiz(commands.Cog): ) await ctx.send(embed=start_embed) # send an embed with the rules await asyncio.sleep(1) - # Stop game is running. + # Stop game if running. else: - # Check if the author is the owner or a mod. + # Check if the author is the game starter or a moderator. if ( ctx.author == self.game_owners[ctx.channel.id] - or Roles.moderator in [role.id for role in ctx.author.roles] + or any(Roles.moderator == role.id for role in ctx.author.roles) ): await ctx.send("Quiz is no longer running.") await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) @@ -185,8 +185,8 @@ class TriviaQuiz(commands.Cog): await self.send_score(ctx.channel, self.game_player_scores[ctx.channel.id]) await asyncio.sleep(2) - @commands.command(name="scoreboard") - async def overall_scoreboard(self, ctx: commands.Context) -> None: + @quiz_game.command(name="leaderboard") + async def leaderboard(self, ctx: commands.Context) -> None: """View everyone's score for this bot session.""" await self.send_score(ctx.channel, self.player_scores) @@ -197,10 +197,12 @@ class TriviaQuiz(commands.Cog): embed.title = "Score Board" embed.description = "" if len(player_data) == 0: - await channel.send("No one has made it to the scoreboard yet.") + await channel.send("No one has made it to the leaderboard yet.") return - for k, v in player_data.items(): - embed.description += f"{k} : {v}\n" + sorted_dict = sorted(player_data.items(), key=lambda a: a[1], reverse=True) + for item in sorted_dict: + + embed.description += f"{item[0]} : {item[1]}\n" await channel.send(embed=embed) @staticmethod -- cgit v1.2.3 From 1332d9e5eaf9e2d7fafa8f6c90216c2b012f3743 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Thu, 17 Oct 2019 18:03:25 +0530 Subject: fixed bugs such as incorrect time to send the category embed, game will no longer run again if stopped n stuff. --- bot/seasons/evergreen/trivia_quiz.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index f7ad9f3a..d4e582af 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -58,29 +58,14 @@ class TriviaQuiz(commands.Cog): - general : Test your general knowledge. (default) (More to come!) """ - category = category.lower() - if ctx.channel.id not in self.game_status: self.game_status[ctx.channel.id] = False if ctx.channel.id not in self.game_player_scores: self.game_player_scores[ctx.channel.id] = {} - # Start game if not running. - if not self.game_status[ctx.channel.id]: - self.game_owners[ctx.channel.id] = ctx.author - self.game_status[ctx.channel.id] = True - start_embed = discord.Embed(colour=discord.Colour.red()) - start_embed.title = "Quiz game Starting!!" - start_embed.description = "Each game consists of 5 questions.\n" - start_embed.description += "**Rules :**\nNo cheating and have fun!" - start_embed.set_footer( - text="Points for a question reduces by 25 after 10s or after a hint. Total time is 30s per question" - ) - await ctx.send(embed=start_embed) # send an embed with the rules - await asyncio.sleep(1) # Stop game if running. - else: + if self.game_status[ctx.channel.id] is True: # Check if the author is the game starter or a moderator. if ( ctx.author == self.game_owners[ctx.channel.id] @@ -93,12 +78,29 @@ class TriviaQuiz(commands.Cog): self.game_player_scores[ctx.channel.id] = {} else: await ctx.send(f"{ctx.author.mention}, you are not authorised to stop this game :ghost: !") + return + category = category.lower() + # Send embed showing available categories if inputted category is invalid. if category not in self.categories: embed = self.category_embed await ctx.send(embed=embed) return + # Start game if not running. + if self.game_status[ctx.channel.id] is False: + self.game_owners[ctx.channel.id] = ctx.author + self.game_status[ctx.channel.id] = True + start_embed = discord.Embed(colour=discord.Colour.red()) + start_embed.title = "Quiz game Starting!!" + start_embed.description = "Each game consists of 5 questions.\n" + start_embed.description += "**Rules :**\nNo cheating and have fun!" + start_embed.set_footer( + text="Points for a question reduces by 25 after 10s or after a hint. Total time is 30s per question" + ) + await ctx.send(embed=start_embed) # send an embed with the rules + await asyncio.sleep(1) + topic = self.questions[category] done_question = [] -- cgit v1.2.3 From 21fdbdb4ee062e4a7b59320b64da773a42dad6e0 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 18 Oct 2019 22:35:24 +0530 Subject: Made some language corrections in the json file and also made seperate functions for the start and end quiz. Added another check to check if the game is still running before sending the answer. --- bot/resources/evergreen/trivia_quiz.json | 4 +-- bot/seasons/evergreen/trivia_quiz.py | 58 ++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/bot/resources/evergreen/trivia_quiz.json b/bot/resources/evergreen/trivia_quiz.json index 66df3df7..48ee2ce4 100644 --- a/bot/resources/evergreen/trivia_quiz.json +++ b/bot/resources/evergreen/trivia_quiz.json @@ -113,7 +113,7 @@ { "id": 113, "question": "What's the name of the tallest waterfall in the world.", - "answer": "Angel", + "answer": "Angel Falls", "info": "Angel Falls (Salto Ángel) in Venezuela is the highest waterfall in the world. The falls are 3230 feet in height, with an uninterrupted drop of 2647 feet. Angel Falls is located on a tributary of the Rio Caroni." }, { @@ -143,7 +143,7 @@ { "id": 118, "question": "Where is the \"International Court Of Justice\" located at?", - "answer": "Hague", + "answer": "The Hague", "info": "" }, { diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index d4e582af..efc3cd98 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -24,7 +24,6 @@ WRONG_ANS_RESPONSE = [ class TriviaQuiz(commands.Cog): """A cog for all quiz commands.""" - def __init__(self, bot: commands.Bot) -> None: self.bot = bot self.questions = self.load_questions() @@ -66,19 +65,7 @@ class TriviaQuiz(commands.Cog): # Stop game if running. if self.game_status[ctx.channel.id] is True: - # Check if the author is the game starter or a moderator. - if ( - ctx.author == self.game_owners[ctx.channel.id] - or any(Roles.moderator == role.id for role in ctx.author.roles) - ): - await ctx.send("Quiz is no longer running.") - await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) - self.game_status[ctx.channel.id] = False - del self.game_owners[ctx.channel.id] - self.game_player_scores[ctx.channel.id] = {} - else: - await ctx.send(f"{ctx.author.mention}, you are not authorised to stop this game :ghost: !") - return + await self.stop_quiz(ctx.author, ctx.channel) category = category.lower() # Send embed showing available categories if inputted category is invalid. @@ -89,15 +76,11 @@ class TriviaQuiz(commands.Cog): # Start game if not running. if self.game_status[ctx.channel.id] is False: + self.game_owners[ctx.channel.id] = ctx.author self.game_status[ctx.channel.id] = True - start_embed = discord.Embed(colour=discord.Colour.red()) - start_embed.title = "Quiz game Starting!!" - start_embed.description = "Each game consists of 5 questions.\n" - start_embed.description += "**Rules :**\nNo cheating and have fun!" - start_embed.set_footer( - text="Points for a question reduces by 25 after 10s or after a hint. Total time is 30s per question" - ) + start_embed = self.make_start_embed() + await ctx.send(embed=start_embed) # send an embed with the rules await asyncio.sleep(1) @@ -116,8 +99,10 @@ class TriviaQuiz(commands.Cog): del self.game_owners[ctx.channel.id] self.game_player_scores[ctx.channel.id] = {} break + # If no hint has been sent or any time alert. Basically if hint_no = 0 means it is a new question. if hint_no == 0: + # Select a random question which has not been used yet. while True: question_dict = random.choice(topic) if question_dict["id"] not in done_question: @@ -155,6 +140,8 @@ class TriviaQuiz(commands.Cog): # If hint_no > 2, then it means that all hints/time alerts have been sent. # Also means that the answer is not yet given and the bot sends the answer and the next question. else: + if self.game_status[ctx.channel.id] is False: + break response = random.choice(WRONG_ANS_RESPONSE) expression = random.choice(ANNOYED_EXPRESSIONS) await ctx.send(f"{response} {expression}") @@ -167,6 +154,8 @@ class TriviaQuiz(commands.Cog): await asyncio.sleep(2) else: + if self.game_status[ctx.channel.id] is False: + break # Reduce points by 25 for every hint/time alert that has been sent. points = 100 - 25*hint_no if msg.author in self.game_player_scores[ctx.channel.id]: @@ -187,6 +176,33 @@ class TriviaQuiz(commands.Cog): await self.send_score(ctx.channel, self.game_player_scores[ctx.channel.id]) await asyncio.sleep(2) + @staticmethod + def make_start_embed(): + """Generate a starting/introduction embed for the quiz.""" + start_embed = discord.Embed(colour=discord.Colour.red()) + start_embed.title = "Quiz game Starting!!" + start_embed.description = "Each game consists of 5 questions.\n" + start_embed.description += "**Rules :**\nNo cheating and have fun!" + start_embed.set_footer( + text="Points for a question reduces by 25 after 10s or after a hint. Total time is 30s per question" + ) + return start_embed + + async def stop_quiz(self, author, channel): + # Check if the author is the game starter or a moderator. + if ( + author == self.game_owners[channel.id] + or any(Roles.moderator == role.id for role in author.roles) + ): + await channel.send("Quiz is no longer running.") + await self.declare_winner(channel, self.game_player_scores[channel.id]) + self.game_status[channel.id] = False + del self.game_owners[channel.id] + self.game_player_scores[channel.id] = {} + else: + await channel.send(f"{author.mention}, you are not authorised to stop this game :ghost: !") + return + @quiz_game.command(name="leaderboard") async def leaderboard(self, ctx: commands.Context) -> None: """View everyone's score for this bot session.""" -- cgit v1.2.3 From e63cc06bd8287467a6eaffd00360fc6b75ea4169 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Sat, 19 Oct 2019 23:27:35 +0530 Subject: added a cool down per channel to the quiz command --- bot/seasons/evergreen/trivia_quiz.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index efc3cd98..2398cb40 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -6,6 +6,8 @@ from pathlib import Path import discord from discord.ext import commands +from discord.ext.commands import cooldown +from discord.ext.commands.cooldowns import BucketType from fuzzywuzzy import fuzz from bot.constants import Roles @@ -45,6 +47,7 @@ class TriviaQuiz(commands.Cog): questions = json.load(json_data) return questions + @cooldown(1, 20, BucketType.channel) @commands.group(name="quiz", aliases=["trivia"], invoke_without_command=True) async def quiz_game(self, ctx: commands.Context, category: str = "general") -> None: """ @@ -177,7 +180,7 @@ class TriviaQuiz(commands.Cog): await asyncio.sleep(2) @staticmethod - def make_start_embed(): + def make_start_embed() -> discord.Embed: """Generate a starting/introduction embed for the quiz.""" start_embed = discord.Embed(colour=discord.Colour.red()) start_embed.title = "Quiz game Starting!!" @@ -188,7 +191,8 @@ class TriviaQuiz(commands.Cog): ) return start_embed - async def stop_quiz(self, author, channel): + async def stop_quiz(self, author: discord.Member, channel: discord.TextChannel) -> None: + """Stop the quiz.""" # Check if the author is the game starter or a moderator. if ( author == self.game_owners[channel.id] -- cgit v1.2.3 From d24bc073b32887a060eb1f9f17b9e2076984859a Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Sun, 20 Oct 2019 08:39:10 +0530 Subject: added cooldown to the quiz command and more refinement and also tested. --- bot/seasons/evergreen/trivia_quiz.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 2398cb40..9a7738a4 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -26,6 +26,7 @@ WRONG_ANS_RESPONSE = [ class TriviaQuiz(commands.Cog): """A cog for all quiz commands.""" + def __init__(self, bot: commands.Bot) -> None: self.bot = bot self.questions = self.load_questions() @@ -69,6 +70,7 @@ class TriviaQuiz(commands.Cog): # Stop game if running. if self.game_status[ctx.channel.id] is True: await self.stop_quiz(ctx.author, ctx.channel) + return category = category.lower() # Send embed showing available categories if inputted category is invalid. @@ -205,7 +207,6 @@ class TriviaQuiz(commands.Cog): self.game_player_scores[channel.id] = {} else: await channel.send(f"{author.mention}, you are not authorised to stop this game :ghost: !") - return @quiz_game.command(name="leaderboard") async def leaderboard(self, ctx: commands.Context) -> None: -- cgit v1.2.3 From 322a6ae1b505d919a62d18a8c901da33e2233d9a Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Fri, 22 Nov 2019 18:29:15 +0000 Subject: Grammar fixes --- bot/seasons/evergreen/trivia_quiz.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 9a7738a4..bf304644 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -16,10 +16,8 @@ from bot.constants import Roles logger = logging.getLogger(__name__) -ANNOYED_EXPRESSIONS = ["-_-", "-.-"] - WRONG_ANS_RESPONSE = [ - "No one gave the correct answer", + "No answered correctly!", "Better luck next time" ] @@ -42,7 +40,7 @@ class TriviaQuiz(commands.Cog): @staticmethod def load_questions() -> dict: - """Load the questions from json file.""" + """Load the questions from the JSON file.""" p = Path("bot", "resources", "evergreen", "trivia_quiz.json") with p.open() as json_data: questions = json.load(json_data) @@ -55,7 +53,7 @@ class TriviaQuiz(commands.Cog): Start/Stop a quiz! If the quiz game is running, then the owner or a mod can stop it by using this command - without providing any arguments and vice versa. + without providing any arguments or vice versa. Questions for the quiz can be selected from the following categories: - general : Test your general knowledge. (default) @@ -98,7 +96,7 @@ class TriviaQuiz(commands.Cog): # Exit quiz if number of questions for a round are already sent. if len(done_question) > self.question_limit and hint_no == 0: - await ctx.send("The round ends here.") + await ctx.send("The round has ended.") await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) self.game_status[ctx.channel.id] = False del self.game_owners[ctx.channel.id] @@ -139,7 +137,7 @@ class TriviaQuiz(commands.Cog): hints = question_dict["hints"] await ctx.send(f"**Hint #{hint_no+1}\n**{hints[hint_no]}") else: - await ctx.send(f"Cmon guys, {30-hint_no*10}s left!") + await ctx.send(f"{30-hint_no*10}s left!") # Once hint or time alerts has been sent 2 times, the hint_no value will be 3 # If hint_no > 2, then it means that all hints/time alerts have been sent. @@ -148,8 +146,7 @@ class TriviaQuiz(commands.Cog): if self.game_status[ctx.channel.id] is False: break response = random.choice(WRONG_ANS_RESPONSE) - expression = random.choice(ANNOYED_EXPRESSIONS) - await ctx.send(f"{response} {expression}") + await ctx.send(response) await self.send_answer(ctx.channel, question_dict) await asyncio.sleep(1) @@ -176,7 +173,7 @@ class TriviaQuiz(commands.Cog): hint_no = 0 - await ctx.send(f"{msg.author.mention} got the correct answer :tada: {points} points for ya.") + await ctx.send(f"{msg.author.mention} got the correct answer :tada: {points} points!") await self.send_answer(ctx.channel, question_dict) await self.send_score(ctx.channel, self.game_player_scores[ctx.channel.id]) await asyncio.sleep(2) @@ -189,7 +186,7 @@ class TriviaQuiz(commands.Cog): start_embed.description = "Each game consists of 5 questions.\n" start_embed.description += "**Rules :**\nNo cheating and have fun!" start_embed.set_footer( - text="Points for a question reduces by 25 after 10s or after a hint. Total time is 30s per question" + text="Points for each question reduces by 25 after 10s or after a hint. Total time is 30s per question" ) return start_embed @@ -200,13 +197,13 @@ class TriviaQuiz(commands.Cog): author == self.game_owners[channel.id] or any(Roles.moderator == role.id for role in author.roles) ): - await channel.send("Quiz is no longer running.") + await channel.send("Quiz stopped.") await self.declare_winner(channel, self.game_player_scores[channel.id]) self.game_status[channel.id] = False del self.game_owners[channel.id] self.game_player_scores[channel.id] = {} else: - await channel.send(f"{author.mention}, you are not authorised to stop this game :ghost: !") + await channel.send(f"{author.mention}, you are not authorised to stop this game :ghost:!") @quiz_game.command(name="leaderboard") async def leaderboard(self, ctx: commands.Context) -> None: @@ -220,7 +217,7 @@ class TriviaQuiz(commands.Cog): embed.title = "Score Board" embed.description = "" if len(player_data) == 0: - await channel.send("No one has made it to the leaderboard yet.") + await channel.send("No one has made it onto the leaderboard yet.") return sorted_dict = sorted(player_data.items(), key=lambda a: a[1], reverse=True) for item in sorted_dict: @@ -247,15 +244,15 @@ class TriviaQuiz(commands.Cog): winners_mention = None for winner in winners: winners_mention += f"{winner.mention} " - else: word = "You" author_index = list(player_data.values()).index(highest_points) winner = list(player_data.keys())[author_index] winners_mention = winner.mention + await channel.send( - f"Congratz {winners_mention} :tada: " - f"{word} have won this quiz game with a grand total of {highest_points} points!!" + f"Congratulations {winners_mention} :tada: " + f"{word} have won this quiz game with a grand total of {highest_points} points!" ) @property @@ -279,11 +276,11 @@ class TriviaQuiz(commands.Cog): embed.description = "" if info != "": embed.description += f"**Information**\n{info}\n\n" - embed.description += "Lets move to the next question.\nRemaining questions: " + embed.description += "Let's move to the next question.\nRemaining questions: " await channel.send(embed=embed) def setup(bot: commands.Bot) -> None: - """Loading the cog.""" + """Load the cog.""" bot.add_cog(TriviaQuiz(bot)) logger.debug("TriviaQuiz cog loaded") -- cgit v1.2.3 From 14d723731122108b06d3d0648118c0ab25c8e6c7 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 29 Nov 2019 17:25:55 +0530 Subject: added a new command to stop the quiz and removed the cooldown on the quiz command --- bot/seasons/evergreen/trivia_quiz.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index bf304644..69fecb3a 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -6,8 +6,6 @@ from pathlib import Path import discord from discord.ext import commands -from discord.ext.commands import cooldown -from discord.ext.commands.cooldowns import BucketType from fuzzywuzzy import fuzz from bot.constants import Roles @@ -17,7 +15,7 @@ logger = logging.getLogger(__name__) WRONG_ANS_RESPONSE = [ - "No answered correctly!", + "No one answered correctly!", "Better luck next time" ] @@ -46,11 +44,10 @@ class TriviaQuiz(commands.Cog): questions = json.load(json_data) return questions - @cooldown(1, 20, BucketType.channel) @commands.group(name="quiz", aliases=["trivia"], invoke_without_command=True) async def quiz_game(self, ctx: commands.Context, category: str = "general") -> None: """ - Start/Stop a quiz! + Start a quiz! If the quiz game is running, then the owner or a mod can stop it by using this command without providing any arguments or vice versa. @@ -67,6 +64,10 @@ class TriviaQuiz(commands.Cog): # Stop game if running. if self.game_status[ctx.channel.id] is True: + return await ctx.send( + f"Game is already running..." + f"do `{self.bot.command_prefix}quiz stop`" + ) await self.stop_quiz(ctx.author, ctx.channel) return @@ -190,7 +191,15 @@ class TriviaQuiz(commands.Cog): ) return start_embed - async def stop_quiz(self, author: discord.Member, channel: discord.TextChannel) -> None: + @quiz_game.command(name="stop") + async def stop_quiz(self, ctx: commands.Context) -> None: + """Stop a quiz game if its running in the channel.""" + if self.game_status[ctx.channel.id] is True: + await self.stop(ctx.author, ctx.channel) + else: + await ctx.send("No quiz running.") + + async def stop(self, author: discord.Member, channel: discord.TextChannel) -> None: """Stop the quiz.""" # Check if the author is the game starter or a moderator. if ( -- cgit v1.2.3 From 23e59f291ba0b186bdbcdc30d6e846837b504ad4 Mon Sep 17 00:00:00 2001 From: Rohan_Iceman Date: Mon, 2 Dec 2019 15:46:16 +0530 Subject: Update bot/seasons/evergreen/trivia_quiz.py adding space around special chars Co-Authored-By: Mark --- bot/seasons/evergreen/trivia_quiz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 69fecb3a..4e465052 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -138,7 +138,7 @@ class TriviaQuiz(commands.Cog): hints = question_dict["hints"] await ctx.send(f"**Hint #{hint_no+1}\n**{hints[hint_no]}") else: - await ctx.send(f"{30-hint_no*10}s left!") + await ctx.send(f"{30 - hint_no * 10}s left!") # Once hint or time alerts has been sent 2 times, the hint_no value will be 3 # If hint_no > 2, then it means that all hints/time alerts have been sent. -- cgit v1.2.3 From c55342ff0cc65b603378382372d3a29d232e02bd Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Mon, 16 Dec 2019 20:40:31 +0530 Subject: Fixed indentation, the category_embed function is no longer a property. --- bot/seasons/evergreen/trivia_quiz.py | 50 ++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 69fecb3a..38bd3f72 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -74,7 +74,7 @@ class TriviaQuiz(commands.Cog): category = category.lower() # Send embed showing available categories if inputted category is invalid. if category not in self.categories: - embed = self.category_embed + embed = self.category_embed() await ctx.send(embed=embed) return @@ -124,12 +124,12 @@ class TriviaQuiz(commands.Cog): def check(m: discord.Message) -> bool: ratio = fuzz.ratio(answer.lower(), m.content.lower()) return ratio > 85 and m.channel == ctx.channel - try: - msg = await self.bot.wait_for('message', check=check, timeout=10) - except asyncio.TimeoutError: - # In case of TimeoutError and the game has been stopped, then do nothing. - if self.game_status[ctx.channel.id] is False: - break + try: + msg = await self.bot.wait_for('message', check=check, timeout=10) + except asyncio.TimeoutError: + # In case of TimeoutError and the game has been stopped, then do nothing. + if self.game_status[ctx.channel.id] is False: + break # if number of hints sent or time alerts sent is less than 2, then send one. if hint_no < 2: @@ -195,25 +195,21 @@ class TriviaQuiz(commands.Cog): async def stop_quiz(self, ctx: commands.Context) -> None: """Stop a quiz game if its running in the channel.""" if self.game_status[ctx.channel.id] is True: - await self.stop(ctx.author, ctx.channel) + # Check if the author is the game starter or a moderator. + if ( + ctx.author == self.game_owners[ctx.channel.id] + or any(Roles.moderator == role.id for role in ctx.author.roles) + ): + await ctx.send("Quiz stopped.") + await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) + self.game_status[ctx.channel.id] = False + del self.game_owners[ctx.channel.id] + self.game_player_scores[ctx.channel.id] = {} + else: + await ctx.send(f"{ctx.author.mention}, you are not authorised to stop this game :ghost:!") else: await ctx.send("No quiz running.") - async def stop(self, author: discord.Member, channel: discord.TextChannel) -> None: - """Stop the quiz.""" - # Check if the author is the game starter or a moderator. - if ( - author == self.game_owners[channel.id] - or any(Roles.moderator == role.id for role in author.roles) - ): - await channel.send("Quiz stopped.") - await self.declare_winner(channel, self.game_player_scores[channel.id]) - self.game_status[channel.id] = False - del self.game_owners[channel.id] - self.game_player_scores[channel.id] = {} - else: - await channel.send(f"{author.mention}, you are not authorised to stop this game :ghost:!") - @quiz_game.command(name="leaderboard") async def leaderboard(self, ctx: commands.Context) -> None: """View everyone's score for this bot session.""" @@ -222,15 +218,14 @@ class TriviaQuiz(commands.Cog): @staticmethod async def send_score(channel: discord.TextChannel, player_data: dict) -> None: """A function which sends the score.""" - embed = discord.Embed(colour=discord.Colour.blue()) - embed.title = "Score Board" - embed.description = "" if len(player_data) == 0: await channel.send("No one has made it onto the leaderboard yet.") return + embed = discord.Embed(colour=discord.Colour.blue()) + embed.title = "Score Board" + embed.description = "" sorted_dict = sorted(player_data.items(), key=lambda a: a[1], reverse=True) for item in sorted_dict: - embed.description += f"{item[0]} : {item[1]}\n" await channel.send(embed=embed) @@ -264,7 +259,6 @@ class TriviaQuiz(commands.Cog): f"{word} have won this quiz game with a grand total of {highest_points} points!" ) - @property def category_embed(self) -> discord.Embed: """Build an embed showing all available trivia categories.""" embed = discord.Embed(colour=discord.Colour.blue()) -- cgit v1.2.3 From f9530fc0a3e09a5d6be159831fe63b9cb70209ba Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Mon, 16 Dec 2019 20:53:22 +0530 Subject: fixed a indent error --- bot/seasons/evergreen/trivia_quiz.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 70e28e4b..fb7c4ba8 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -124,12 +124,12 @@ class TriviaQuiz(commands.Cog): def check(m: discord.Message) -> bool: ratio = fuzz.ratio(answer.lower(), m.content.lower()) return ratio > 85 and m.channel == ctx.channel - try: - msg = await self.bot.wait_for('message', check=check, timeout=10) - except asyncio.TimeoutError: - # In case of TimeoutError and the game has been stopped, then do nothing. - if self.game_status[ctx.channel.id] is False: - break + try: + msg = await self.bot.wait_for('message', check=check, timeout=10) + except asyncio.TimeoutError: + # In case of TimeoutError and the game has been stopped, then do nothing. + if self.game_status[ctx.channel.id] is False: + break # if number of hints sent or time alerts sent is less than 2, then send one. if hint_no < 2: @@ -155,7 +155,6 @@ class TriviaQuiz(commands.Cog): await self.send_score(ctx.channel, self.game_player_scores[ctx.channel.id]) await asyncio.sleep(2) - else: if self.game_status[ctx.channel.id] is False: break -- cgit v1.2.3 From 529fc617dae7636eb5948bc79f51da9afceb1710 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 17 Dec 2019 08:24:34 -0800 Subject: Trivia: space out some of lines of code --- bot/seasons/evergreen/trivia_quiz.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index fb7c4ba8..2470ec0f 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -80,7 +80,6 @@ class TriviaQuiz(commands.Cog): # Start game if not running. if self.game_status[ctx.channel.id] is False: - self.game_owners[ctx.channel.id] = ctx.author self.game_status[ctx.channel.id] = True start_embed = self.make_start_embed() @@ -94,14 +93,15 @@ class TriviaQuiz(commands.Cog): hint_no = 0 answer = None while self.game_status[ctx.channel.id]: - # Exit quiz if number of questions for a round are already sent. if len(done_question) > self.question_limit and hint_no == 0: await ctx.send("The round has ended.") await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) + self.game_status[ctx.channel.id] = False del self.game_owners[ctx.channel.id] self.game_player_scores[ctx.channel.id] = {} + break # If no hint has been sent or any time alert. Basically if hint_no = 0 means it is a new question. @@ -112,6 +112,7 @@ class TriviaQuiz(commands.Cog): if question_dict["id"] not in done_question: done_question.append(question_dict["id"]) break + q = question_dict["question"] answer = question_dict["answer"] @@ -124,6 +125,7 @@ class TriviaQuiz(commands.Cog): def check(m: discord.Message) -> bool: ratio = fuzz.ratio(answer.lower(), m.content.lower()) return ratio > 85 and m.channel == ctx.channel + try: msg = await self.bot.wait_for('message', check=check, timeout=10) except asyncio.TimeoutError: @@ -146,6 +148,7 @@ class TriviaQuiz(commands.Cog): else: if self.game_status[ctx.channel.id] is False: break + response = random.choice(WRONG_ANS_RESPONSE) await ctx.send(response) await self.send_answer(ctx.channel, question_dict) @@ -158,6 +161,7 @@ class TriviaQuiz(commands.Cog): else: if self.game_status[ctx.channel.id] is False: break + # Reduce points by 25 for every hint/time alert that has been sent. points = 100 - 25*hint_no if msg.author in self.game_player_scores[ctx.channel.id]: @@ -201,6 +205,7 @@ class TriviaQuiz(commands.Cog): ): await ctx.send("Quiz stopped.") await self.declare_winner(ctx.channel, self.game_player_scores[ctx.channel.id]) + self.game_status[ctx.channel.id] = False del self.game_owners[ctx.channel.id] self.game_player_scores[ctx.channel.id] = {} @@ -220,12 +225,15 @@ class TriviaQuiz(commands.Cog): if len(player_data) == 0: await channel.send("No one has made it onto the leaderboard yet.") return + embed = discord.Embed(colour=discord.Colour.blue()) embed.title = "Score Board" embed.description = "" + sorted_dict = sorted(player_data.items(), key=lambda a: a[1], reverse=True) for item in sorted_dict: embed.description += f"{item[0]} : {item[1]}\n" + await channel.send(embed=embed) @staticmethod @@ -240,10 +248,12 @@ class TriviaQuiz(commands.Cog): word = "You guys" winners = [] points_copy = list(player_data.values()).copy() + for _ in range(no_of_winners): index = points_copy.index(highest_points) winners.append(list(player_data.keys())[index]) points_copy[index] = 0 + winners_mention = None for winner in winners: winners_mention += f"{winner.mention} " @@ -262,10 +272,12 @@ class TriviaQuiz(commands.Cog): """Build an embed showing all available trivia categories.""" embed = discord.Embed(colour=discord.Colour.blue()) embed.title = "The available question categories are:" + embed.set_footer(text="If not category is chosen, then a random one will be selected.") embed.description = "" + for cat, description in self.categories.items(): embed.description += f"**- {cat.capitalize()}**\n{description.capitalize()}\n" - embed.set_footer(text="If not category is chosen, then a random one will be selected.") + return embed @staticmethod @@ -276,8 +288,10 @@ class TriviaQuiz(commands.Cog): embed = discord.Embed(color=discord.Colour.red()) embed.title = f"The correct answer is **{answer}**\n" embed.description = "" + if info != "": embed.description += f"**Information**\n{info}\n\n" + embed.description += "Let's move to the next question.\nRemaining questions: " await channel.send(embed=embed) -- cgit v1.2.3 From 27d65c155681a0c720ce405909527fadb8b54b31 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 17 Dec 2019 08:30:48 -0800 Subject: Trivia: fix concatenation of winner mentions --- bot/seasons/evergreen/trivia_quiz.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 2470ec0f..345f1abd 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -254,9 +254,7 @@ class TriviaQuiz(commands.Cog): winners.append(list(player_data.keys())[index]) points_copy[index] = 0 - winners_mention = None - for winner in winners: - winners_mention += f"{winner.mention} " + winners_mention = " ".join(winner.mention for winner in winners) else: word = "You" author_index = list(player_data.values()).index(highest_points) -- cgit v1.2.3 From 145eda56bedb105428822264e10305c1bc0c2114 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 17 Dec 2019 08:51:59 -0800 Subject: Trivia: fix spelling error in category embed footer --- bot/seasons/evergreen/trivia_quiz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 345f1abd..49e75b79 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -270,7 +270,7 @@ class TriviaQuiz(commands.Cog): """Build an embed showing all available trivia categories.""" embed = discord.Embed(colour=discord.Colour.blue()) embed.title = "The available question categories are:" - embed.set_footer(text="If not category is chosen, then a random one will be selected.") + embed.set_footer(text="If a category is not chosen, a random one will be selected.") embed.description = "" for cat, description in self.categories.items(): -- cgit v1.2.3 From 1993fe6c77faaabcaf847abbedd9cc126e459556 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 20 Dec 2019 11:02:49 +0530 Subject: In this commit: - I cleaned up some residue code - The quiz now selects a random category if None provided - Updated doc strings - Displaying the category when the quiz is starting --- bot/seasons/evergreen/trivia_quiz.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index fb7c4ba8..ebb9077a 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -45,13 +45,10 @@ class TriviaQuiz(commands.Cog): return questions @commands.group(name="quiz", aliases=["trivia"], invoke_without_command=True) - async def quiz_game(self, ctx: commands.Context, category: str = "general") -> None: + async def quiz_game(self, ctx: commands.Context, category: str = None) -> None: """ Start a quiz! - If the quiz game is running, then the owner or a mod can stop it by using this command - without providing any arguments or vice versa. - Questions for the quiz can be selected from the following categories: - general : Test your general knowledge. (default) (More to come!) @@ -68,22 +65,22 @@ class TriviaQuiz(commands.Cog): f"Game is already running..." f"do `{self.bot.command_prefix}quiz stop`" ) - await self.stop_quiz(ctx.author, ctx.channel) - return - category = category.lower() - # Send embed showing available categories if inputted category is invalid. - if category not in self.categories: + # Send embed showing available categori es if inputted category is invalid. + if category is None: + category = random.choice(list(self.categories)) + if category.lower() not in self.categories: embed = self.category_embed() await ctx.send(embed=embed) return + # Start game if not running. if self.game_status[ctx.channel.id] is False: self.game_owners[ctx.channel.id] = ctx.author self.game_status[ctx.channel.id] = True - start_embed = self.make_start_embed() + start_embed = self.make_start_embed(category) await ctx.send(embed=start_embed) # send an embed with the rules await asyncio.sleep(1) @@ -179,12 +176,13 @@ class TriviaQuiz(commands.Cog): await asyncio.sleep(2) @staticmethod - def make_start_embed() -> discord.Embed: + def make_start_embed(category: str) -> discord.Embed: """Generate a starting/introduction embed for the quiz.""" start_embed = discord.Embed(colour=discord.Colour.red()) start_embed.title = "Quiz game Starting!!" start_embed.description = "Each game consists of 5 questions.\n" start_embed.description += "**Rules :**\nNo cheating and have fun!" + start_embed.description += f"\n **Category** : {category}" start_embed.set_footer( text="Points for each question reduces by 25 after 10s or after a hint. Total time is 30s per question" ) @@ -192,7 +190,10 @@ class TriviaQuiz(commands.Cog): @quiz_game.command(name="stop") async def stop_quiz(self, ctx: commands.Context) -> None: - """Stop a quiz game if its running in the channel.""" + """ + Stop a quiz game if its running in the channel. + Note: Only mods or the owner of the quiz can stop it. + """ if self.game_status[ctx.channel.id] is True: # Check if the author is the game starter or a moderator. if ( -- cgit v1.2.3 From 303128f4b5bd31764bd02ec98a0d1f3ec226ed2c Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 20 Dec 2019 11:04:52 +0530 Subject: fixed lint errors --- bot/seasons/evergreen/trivia_quiz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 770402e5..2b43bb8b 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -73,7 +73,6 @@ class TriviaQuiz(commands.Cog): embed = self.category_embed() await ctx.send(embed=embed) return - # Start game if not running. if self.game_status[ctx.channel.id] is False: @@ -196,6 +195,7 @@ class TriviaQuiz(commands.Cog): async def stop_quiz(self, ctx: commands.Context) -> None: """ Stop a quiz game if its running in the channel. + Note: Only mods or the owner of the quiz can stop it. """ if self.game_status[ctx.channel.id] is True: -- cgit v1.2.3 From c9e6bf2ac3fe85086a4d1ca69d8b37469d47d095 Mon Sep 17 00:00:00 2001 From: Rohan_Iceman Date: Fri, 20 Dec 2019 13:58:54 +0530 Subject: Update bot/seasons/evergreen/trivia_quiz.py Co-Authored-By: Mark --- bot/seasons/evergreen/trivia_quiz.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 2b43bb8b..67c202f0 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -69,7 +69,9 @@ class TriviaQuiz(commands.Cog): # Send embed showing available categori es if inputted category is invalid. if category is None: category = random.choice(list(self.categories)) - if category.lower() not in self.categories: + + category = category.lower() + if category not in self.categories: embed = self.category_embed() await ctx.send(embed=embed) return -- cgit v1.2.3 From 0758be812d40dd64f33262ef366fdfb5917bb1fc Mon Sep 17 00:00:00 2001 From: Rohan_Iceman Date: Fri, 20 Dec 2019 13:59:13 +0530 Subject: Update bot/seasons/evergreen/trivia_quiz.py Co-Authored-By: Mark --- bot/seasons/evergreen/trivia_quiz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/seasons/evergreen/trivia_quiz.py b/bot/seasons/evergreen/trivia_quiz.py index 67c202f0..99b64497 100644 --- a/bot/seasons/evergreen/trivia_quiz.py +++ b/bot/seasons/evergreen/trivia_quiz.py @@ -66,7 +66,7 @@ class TriviaQuiz(commands.Cog): f"do `{self.bot.command_prefix}quiz stop`" ) - # Send embed showing available categori es if inputted category is invalid. + # Send embed showing available categories if inputted category is invalid. if category is None: category = random.choice(list(self.categories)) -- cgit v1.2.3