90 lines
4.0 KiB
Python
90 lines
4.0 KiB
Python
|
import os # OS Locations
|
||
|
import yaml # YAML parser for Bot config files
|
||
|
import json # Json Library to manage json Data files
|
||
|
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
|
||
|
|
||
|
from dev import configFile, guild_ids, unloadAllCogs, loadAllCogs, reloadAllCogs, loadCommands, unloadCommands, reloadCommands, logger, handler, yaml_load, yaml_dump
|
||
|
|
||
|
##### Event Listener Cog
|
||
|
class Events(commands.Cog):
|
||
|
def __init__(self, client):
|
||
|
self.client = client
|
||
|
|
||
|
@commands.Cog.listener()
|
||
|
async def on_connect(self): ## Actions for when bot logs in and enters ready state
|
||
|
print('Bot has connected.')
|
||
|
logging.info('Bot has connected.')
|
||
|
|
||
|
#### Check if the bot is missing any config entries for the guilds it is in, and if it is then add it in.
|
||
|
#### N.B.: The way the commands work, the bot will have to list specific guilds in which it will synchronise the commands when it is defining them. So it needs to give a list of all the guilds it is part of when the bot loads, which it draws from the config files.
|
||
|
#### Because the bot connects to Discord after it loads, it will not be able to introspect and see what guilds it is part of before the commands are first loaded, and it will only add new guilds to the config files after it has already connected.
|
||
|
#### So the bot will have to wait until it first connects, then check if any guilds are missing, and if there are any missing guilds it will need to add them to the list of guilds it has configured, then re-load all of the commands and re-sync them.
|
||
|
|
||
|
gFlag = False
|
||
|
print('Checking for guilds with missing configs.') #### The bot will try to update its configs for any missing values. It will check the accuracy of these values if any relevant parameters of the guild change later.
|
||
|
conf = yaml_load(configFile)
|
||
|
for g in self.client.guilds:
|
||
|
if g.id not in guild_ids:
|
||
|
gFlag = True
|
||
|
guild_ids.append(g.id)
|
||
|
conf[str(g.id)] = {}
|
||
|
if 'name' not in conf[str(g.id)]:
|
||
|
conf[str(g.id)]['name'] = g.name
|
||
|
if 'owner' not in conf[str(g.id)]:
|
||
|
conf[str(g.id)]['owner'] = await self.client.fetch_guild(g.id).owner.id
|
||
|
if 'adminroles' not in conf[str(g.id)]:
|
||
|
conf[str(g.id)]['adminroles'] = []
|
||
|
for role in g.roles:
|
||
|
if not (role.is_bot_managed() or role.is_integration()) and role.permissions.administrator:
|
||
|
conf[str(g.id)]['adminroles'].append(role.id)
|
||
|
yaml_dump(conf, configFile)
|
||
|
if gFlag:
|
||
|
reloadAllCogs()
|
||
|
## Because the bot will need to re-sync all the commands with an updated list of guilds, it needs to re-load all cogs.
|
||
|
## This should hopefully work with the setting declared earlier to sync on cog reload.
|
||
|
|
||
|
#### Delete Configs for Guilds that the Bot is no longer in
|
||
|
conf = yaml_load(configFile)
|
||
|
a = []
|
||
|
for g in self.client.guilds:
|
||
|
a.append(str(g.id))
|
||
|
for g in list(conf):
|
||
|
if g not in a:
|
||
|
del conf[g]
|
||
|
yaml_dump(conf, configFile)
|
||
|
|
||
|
# for g in self.client.guilds:
|
||
|
|
||
|
|
||
|
|
||
|
@commands.Cog.listener()
|
||
|
async def on_guild_join(self, guild): ## Actions for when bot is added to a new guild for the first time.
|
||
|
if guild.id not in guild_ids:
|
||
|
guild_ids.append(guild.id)
|
||
|
o = f'Adding config entry for guild {g.name}.'
|
||
|
print(o)
|
||
|
logging.info(o)
|
||
|
conf = yaml_load(configFile)
|
||
|
conf[str(g.id)] = {}
|
||
|
yaml_dump(conf, configFile)
|
||
|
reloadAllCogs()
|
||
|
## Same reason as above, when the bot is added to a new guild, it will need to re-load all cogs and re-sync commands.
|
||
|
|
||
|
@commands.Cog.listener()
|
||
|
async def on_guild_remove(self, guild): ## Actions for when the bot is removed from a guild.
|
||
|
if guild.id in guild_ids:
|
||
|
guild_ids.remove(guild.id)
|
||
|
o = f'Removing config entry for guild {g.name}.'
|
||
|
print(o)
|
||
|
logging.info(o)
|
||
|
conf = yaml_load(configFile)
|
||
|
conf.pop(str(guild.id), None)
|
||
|
yaml_dump(conf, configFile)
|
||
|
reloadAllCogs()
|
||
|
|
||
|
def setup(client):
|
||
|
client.add_cog(Events(client))
|