geas-bot/app/cogs/events/on_guild_role_update.py
Vivek Santayana 0f447264ec This branch will not work.
Purging not possible because data files keep getting overwritten
by parallel processes.
2021-07-24 11:31:21 +01:00

73 lines
3.0 KiB
Python

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
import re
# logger and handler
from bot import configFile, yaml_load, yaml_dump, gmFile, categoriesFile, lookupFile, dataFile
##### Actions for the bot to take whenever there is a new role deleted.
class on_guild_role_update(commands.Cog, name='On Guild Role Update Events'):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_guild_role_update(self, before, after):
conf = yaml_load(configFile)
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
if before.id in conf[str(before.guild.id)]['roles']['admin']:
if after.is_bot_managed() or after.is_integration() or not after.permissions.administrator:
conf[str(after.guild.id)]['roles']['admin'].remove(after.id)
yaml_dump(conf, configFile)
return
#### 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:
if after.id not in conf[str(after.guild.id)]['roles']['admin']:
conf[str(after.guild.id)]['roles']['admin'].remove(after.id)
yaml_dump(conf, configFile)
#### 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
def setup(client):
client.add_cog(on_guild_role_update(client))