From 21810d3b53251fbf3bd6193481339dd6dc171502 Mon Sep 17 00:00:00 2001 From: Elnath Date: Thu, 10 Jun 2021 23:57:52 +0200 Subject: [PATCH] SecretBot: added get_running_game_or_error_message utility function --- SecretBot.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/SecretBot.py b/SecretBot.py index c96e817..1dc8ec8 100755 --- a/SecretBot.py +++ b/SecretBot.py @@ -9,7 +9,7 @@ import discord.utils from discord.ext import commands import utils -from GameFiles import GamesFile +from GameFiles import Game, GamesFile logger = logging.getLogger("SecretBot") @@ -94,6 +94,17 @@ class SecretBot(commands.Cog): await ctx.reply(f":dragon_face: You have no power here!") raise utils.CheckFailDoNotNotify + async def get_running_game_or_error_message(self, ctx: commands.Context) -> Game: + """ + Return the game running on the guild on which a command was executed or print an error message and raise an exception if there is no game running + """ + game = self.games_file[ctx.guild] + if game.is_started(): + return game + else: + await ctx.reply(":x: Game is not running") + raise utils.CheckFailDoNotNotify + @commands.command(help = "See if I'm alive") async def ping(self, ctx: commands.Context): await ctx.reply(":ping_pong:", mention_author = True) @@ -111,23 +122,17 @@ class SecretBot(commands.Cog): @commands.command("DeleteGame", help = "Delete a running game and all of its associated channels") async def delete_game(self, ctx: commands.Context): - game = self.games_file[ctx.guild] - if game.is_started(): - await self.check_is_administrator_or_gm(ctx) - gm_role = game.get_gm_role() - await game.delete() - await ctx.guild.get_member(self.bot.user.id).remove_roles(gm_role) - await ctx.reply(":white_check_mark: Game deleted!") - else: - await ctx.reply(":x: Game is not running") + game = await self.get_running_game_or_error_message(ctx) + await self.check_is_administrator_or_gm(ctx) + gm_role = game.get_gm_role() + await game.delete() + await ctx.guild.get_member(self.bot.user.id).remove_roles(gm_role) + await ctx.reply(":white_check_mark: Game deleted!") @commands.command("StartVote") async def start_vote(self, ctx: commands.Context, president: discord.Member, chancellor: discord.Member): + game = await self.get_running_game_or_error_message(ctx) await self.check_is_administrator_or_gm(ctx) - game = self.games_file[ctx.guild] - if not game.is_started(): - await ctx.reply(":x: Game is not running") - return if game.is_vote_running(): await ctx.reply(":x: A vote is already running") return @@ -158,11 +163,8 @@ class SecretBot(commands.Cog): @commands.command("StopTheCount") async def stop_vote(self, ctx: commands.Context): + game = await self.get_running_game_or_error_message(ctx) await self.check_is_administrator_or_gm(ctx) - game = self.games_file[ctx.guild] - if not game.is_started(): - await ctx.reply(":x: Game is not running") - return if not game.is_vote_running(): await ctx.reply(":x: No vote is running") return