Start and delete game: check if the user has rights to execute the command

This commit is contained in:
Elnath 2021-06-09 23:21:10 +02:00
parent b268527fa7
commit b9d5b17954
3 changed files with 23 additions and 7 deletions

View File

@ -42,6 +42,9 @@ class Game:
def get_gm_role(self) -> discord.Role:
return self.guild.get_role(self.get_gm_role_id())
def is_gm(self, user: discord.Member) -> bool:
return self.get_gm_role() in user.roles
def get_game_category_id(self) -> int:
return self.config["category"]

View File

@ -76,11 +76,12 @@ class SecretBot(commands.Cog):
raise utils.CheckFailDoNotNotify
return True
async def check_is_administrator_or_owner(self, ctx: commands.Context):
@staticmethod
async def check_is_administrator(ctx: commands.Context):
"""
Verify if the user has administrator rights or if it is the bot's owner. Notifies the user and raises utils.CheckFailDoNotNotify if not.
If the user that sent the command is not an administrator of the server, notify them and raise utils.CheckFailDoNotNotify.
"""
if not (ctx.author.guild_permissions.administrator or await self.bot.is_owner(ctx.author)):
if not utils.is_administrator(ctx.author):
await ctx.reply(f":dragon_face: You have no power here!")
raise utils.CheckFailDoNotNotify
@ -90,6 +91,7 @@ class SecretBot(commands.Cog):
@commands.command("StartGame")
async def start_game(self, ctx: commands.Context, gm_role: discord.Role, player_role: discord.Role):
await self.check_is_administrator(ctx)
game = self.games_file[ctx.guild]
if game.is_started():
await ctx.reply(":x: a game is already running")
@ -102,10 +104,13 @@ class SecretBot(commands.Cog):
async def delete_game(self, ctx: commands.Context):
game = self.games_file[ctx.guild]
if game.is_started():
if game.is_gm(ctx.author) or utils.is_administrator(ctx.author):
gm_role = game.get_gm_role()
await game.delete(ctx.guild)
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(f":dragon_face: You have no power here!")
else:
await ctx.reply(":x: Game is not running")

View File

@ -10,6 +10,14 @@ class CheckFailDoNotNotify(commands.CheckFailure):
"""
pass
def is_administrator(member: discord.Member) -> bool:
"""
:return: Whether the given guild member has administrator powers
"""
return member.guild_permissions.administrator
@singledispatch
def to_string(obj) -> str:
"""