2021-07-15 22:54:09 +01:00
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
2021-07-15 23:13:01 +01:00
from bot import configFile , yaml_load , yaml_dump
2021-07-15 22:54:09 +01:00
##### Actions for the bot to take whenever there is a new role deleted.
2021-07-16 23:53:31 +01:00
class on_guild_role_update ( commands . Cog , name = ' On Guild Role Update Events ' ) :
2021-07-15 22:54:09 +01:00
def __init__ ( self , client ) :
self . client = client
@commands.Cog.listener ( )
async def on_guild_role_update ( self , before , after ) :
conf = yaml_load ( configFile )
#### If the original role is in the config as an admin role, and it subsequently is run by a bot or is not an admin, remove it from config
2021-07-16 23:53:31 +01:00
if before . id in conf [ str ( before . guild . id ) ] [ ' roles ' ] [ ' admin ' ] :
2021-07-15 22:54:09 +01:00
if after . is_bot_managed ( ) or after . is_integration ( ) or not after . permissions . administrator :
2021-07-16 23:53:31 +01:00
conf [ str ( after . guild . id ) ] [ ' roles ' ] [ ' admin ' ] . remove ( after . id )
2021-07-15 22:54:09 +01:00
yaml_dump ( conf , configFile )
#### If the new role is an admin and is not already in the config, add it.
if not ( after . is_bot_managed ( ) or after . is_integration ( ) ) and after . permissions . administrator :
2021-07-16 23:53:31 +01:00
if after . id not in conf [ str ( after . guild . id ) ] [ ' roles ' ] [ ' admin ' ] :
conf [ str ( after . guild . id ) ] [ ' roles ' ] [ ' admin ' ] . remove ( after . id )
2021-07-15 22:54:09 +01:00
yaml_dump ( conf , configFile )
#### 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.
def setup ( client ) :
client . add_cog ( on_guild_role_update ( client ) )