SecretBot: added get_running_game_or_error_message utility function

This commit is contained in:
Elnath 2021-06-10 23:57:52 +02:00
parent f6d8bcb955
commit 21810d3b53
1 changed files with 20 additions and 18 deletions

View File

@ -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