geas-bot/app/cogs/slashcommands/secondary/game_management.py

94 lines
3.9 KiB
Python

import os # OS Locations
import yaml # YAML parser for Bot config files
import asyncio # Discord Py Dependency
import discord # Main Lib
from discord.ext import commands # Commands module
from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash Command Library
from discord_slash.utils.manage_commands import create_choice, create_option, create_permission # Slash Command features
from discord_slash.model import SlashCommandPermissionType
from discord_slash.client import SlashCommand
import re
from bot import configFile, yaml_load, yaml_dump, reloadCog, cogsDir, unloadCog, dataFile, lookupFile, gmFile
class GameManagement(commands.Cog, name='Game Management'):
def __init__(self, client):
self.client = client
conf = yaml_load(configFile)
lookup = yaml_load(lookupFile)
data = yaml_load(dataFile)
guild_ids= [ int(x) for x in list(lookup)]
### Move delete, Modify, and Reset commands to a separate secondary cog to enable when games exist?
@cog_ext.cog_subcommand(
base='game',
# subcommand_group='',
name='delete',
description='Deletes a game role and accompanying category, text, voice channels, and data.',
# base_description='Commands for setting up and removing games on the Guild.',
# base_default_permission=False,
# base_permissions=permissions,
# subcommand_group_description='Adds a time slot available to the channel for games.',
guild_ids=guild_ids,
options=[
create_option(
name='game_role',
description='The role representing the game you want to interact with.',
option_type=8,
required=True
)
]
)
async def _game_delete(self, ctx:SlashContext, game_role:discord.Role):
await ctx.channel.trigger_typing()
conf = yaml_load(configFile)
data = yaml_load(dataFile)
gms = yaml_load(gmFile)
lookup = yaml_load(lookupFile)
guildStr = str(ctx.guild.id)
if str(game_role.id) not in lookup[guildStr]:
await ctx.send(f'```This is not a valid game role. Please mention a role that is associated with a game.```')
return
game_title = lookup[guildStr][str(game_role.id)]['game_title']
time = lookup[guildStr][str(game_role.id)]['time']
c = ctx.guild.get_channel(lookup[guildStr][str(game_role.id)]['category'])
gm = lookup[guildStr][str(game_role.id)]['gm']
channelsFound = False
for g in list(data[guildStr][time]):
if game_role.id in g.values():
data[guildStr][time].remove(g)
break
if c is not None:
channelsFound = True
for t in ctx.guild.text_channels:
if t.category == c:
await t.delete(reason=f'/game delete command issued by `{ctx.author.display_name}`')
for v in ctx.guild.voice_channels:
if v.category == c:
await v.delete(reason=f'/game delete command issued by `{ctx.author.display_name}`')
await c.delete(reason=f'/game delete command issued by `{ctx.author.display_name}`')
lookup[guildStr].pop(str(game_role.id), None)
gm_m = await ctx.guild.fetch_member(gm)
output = f'The game `{game_title}` for timeslot `{conf[guildStr]["timeslots"][time]}` and with GM `{gm_m.display_name}` has been deleted.'
if channelsFound:
output = ''.join([output,' All associated text, voice, and category channels have been deleted.'])
else:
output = ''.join([output,' No associated text, voice, or category channels were found. Please delete them manually if they still persist.'])
await ctx.send(f'```{output}```')
await game_role.delete(reason=f'/game delete command issued by `{ctx.author.display_name}`')
gms[guildStr][str(gm)].remove(game_role.id)
if not gms[guildStr][str(gm)]:
gms[guildStr].pop(str(gm))
yaml_dump(lookup, lookupFile)
yaml_dump(data, dataFile)
yaml_dump(gms, gmFile)
if not any([x for x in yaml_load(lookupFile).values()]):
unloadCog(f'./{cogsDir}/slashcommands/secondary/game_management.py')
await self.client.slash.sync_all_commands()
def setup(client):
client.add_cog(GameManagement(client))