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
2021-07-24 11:31:21 +01:00
import re
2021-07-15 22:54:09 +01:00
# logger and handler
2021-07-24 11:31:21 +01:00
from bot import configFile , yaml_load , yaml_dump , gmFile , categoriesFile , lookupFile , dataFile
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 )
2021-07-24 11:31:21 +01:00
gms = yaml_load ( gmFile )
categories = yaml_load ( categoriesFile )
lookup = yaml_load ( lookupFile )
data = yaml_load ( dataFile )
guildStr = str ( after . guild . id )
rStr = str ( after . id )
#### If the original role is in the config is 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 )
2021-07-24 11:31:21 +01:00
return
2021-07-15 22:54:09 +01:00
#### 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 )
2021-07-24 11:31:21 +01:00
#### If the role is one of the Admin roles and is stripped of admin permissions, updates the bot's config to delete that role, preventing unnecessary roles from accumulating.
return
#### If the original role is a game role:
if rStr in lookup [ guildStr ] :
#### Suppress invalid changes
revert = { }
# Check name change
if before . name != after . name :
if ' : ' not in after . name : revert [ ' name ' ] = before . name
else :
if after . name . split ( ' : ' , maxsplit = 1 ) [ 0 ] . lower ( ) not in conf [ guildStr ] [ ' timeslots ' ] : revert [ ' name ' ] = before . name
# Check role colour
if before . colour != after . colour : revert [ ' colour ' ] = before . colour
# Check role hoist
if before . hoist != after . hoist : revert [ ' hoist ' ] = before . hoist
# Check role mentionable
if before . mentionable != after . mentionable : revert [ ' mentionable ' ] = before . mentionable
# Check role permissions
if before . permissions != after . mentionable : revert [ ' permissions ' ] = before . permissions
#### Suppress changes if the new settings are invalid
if revert :
revert [ ' reason ' ] = ' Role Update Event Listener: Suppressing permission change for game role. '
await after . edit ( * * revert )
return
2021-07-15 22:54:09 +01:00
def setup ( client ) :
client . add_cog ( on_guild_role_update ( client ) )