geas-bot/app/cogs/events/on_guild_role_delete.py

87 lines
4.2 KiB
Python
Raw Normal View History

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 # Slash Command features
import logging
# logger and handler
from bot import configFile, yaml_load, yaml_dump, dataFile, lookupFile, gmFile, categoriesFile, unloadCog, cogsDir
##### Actions for the bot to take whenever there is a new role deleted.
class on_guild_role_delete(commands.Cog, name='On Guild Role Delete Events'):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_guild_role_delete(self, role):
conf = yaml_load(configFile)
gms = yaml_load(gmFile)
categories = yaml_load(categoriesFile)
lookup = yaml_load(lookupFile)
data = yaml_load(dataFile)
guildStr = str(role.guild.id)
rStr = str(role.id)
#### Bot will only respond if the role is not a bot-managed role, and the role is an admin role
if role.id in conf[guildStr]['roles']['admin']:
conf[guildStr]['roles']['admin'].remove(role.id)
yaml_dump(conf, configFile)
return
#### If the role is one of the Admin roles and is deleted, updates the bot's config to delete that role, preventing unnecessary roles from accumulating.
#### Bot will delete membership type if the membership role is deleted.
if role.id in conf[guildStr]['membership']:
conf[guildStr]['membership'].remove(role.id)
yaml_dump(conf, configFile)
if not any([x['membership'] for x in yaml_load(configFile).values()]):
unloadCog(f'./{cogsDir}/slashcommands/secondary/edit_membership.py')
await self.client.slash.sync_all_commands()
return
#### Synchronising with the Game Creation Process
# Whenever a game role is deleted, the Bot will update the guild's configurations and channels to match.
if rStr in lookup[guildStr]:
game_title = lookup[guildStr][rStr]['game_title']
time = lookup[guildStr][rStr]['time']
c = role.guild.get_channel(lookup[guildStr][rStr]['category'])
gm = await role.guild.fetch_member(lookup[guildStr][rStr]['gm'])
del data[guildStr][time][rStr]
channelsFound = False
if c is not None:
channelsFound = True
for t in role.guild.text_channels:
if t.category == c:
await t.delete(reason=f'Role Delete Event Listener: Synchronising with deletion of role `{role.name}`')
for v in role.guild.voice_channels:
if v.category == c:
await v.delete(reason=f'Role Delete Event Listener: Synchronising with deletion of role `{role.name}`')
del categories[guildStr][str(c.id)]
await c.delete(reason=f'Role Delete Event Listener: Synchronising with deletion of role `{role.name}`')
lookup[guildStr].pop(rStr, None)
output = f'The game `{game_title}` for timeslot `{conf[guildStr]["timeslots"][time]}` and with GM `{gm.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.'])
if 'mod' in conf[guildStr]['channels'] and 'committee' in conf[guildStr]['roles']:
c = discord.utils.find(lambda x: x.id == conf[guildStr]['channels']['mod'], role.guild.channels)
await c.send(
content = f'```{output}```'
)
gms[guildStr][str(gm.id)].remove(role.id)
if not gms[guildStr][str(gm.id)]: del gms[guildStr][str(gm.id)]
if not data[guildStr][time]: del data[guildStr][time]
yaml_dump(lookup, lookupFile)
yaml_dump(data, dataFile)
yaml_dump(gms, gmFile)
yaml_dump(categories, categoriesFile)
if not any([x for x in yaml_load(lookupFile).values()]):
unloadCog(f'./{cogsDir}/slashcommands/secondary/game_management.py')
if self.client.get_cog('Player Commands') is not None: unloadCog(f'./{cogsDir}/slashcommands/secondary/player_commands.py')
if self.client.get_cog('Pitch Command') is not None: unloadCog(f'./{cogsDir}/slashcommands/secondary/pitch.py')
await self.client.slash.sync_all_commands()
def setup(client):
client.add_cog(on_guild_role_delete(client))