Added config checking, event listeners, etc.

Can track added features by comparing TODO list.
This commit is contained in:
2021-07-16 23:53:31 +01:00
parent 1d355e3b2d
commit 51a01bab49
22 changed files with 346 additions and 77 deletions

View File

@ -0,0 +1,27 @@
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
#### Error Handler Event Listener
class on_command_error(commands.Cog, name='On Command Error'):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, discord.DiscordException):
if isinstance(error, commands.CheckFailure):
print(f'Error: User {ctx.author.name}#{ctx.author.discriminator} / {ctx.author.display_name} is not authorised to issue the command <{ctx.command.name}> in the guild {ctx.guild.name}.')
await ctx.reply(f'```Error: You are not authorised to issue this command.```')
else:
print(f'User {ctx.author.name}#{ctx.author.discriminator} / {ctx.author.display_name} received error: "{error}" when attempting to issue command <{ctx.command.name}> in the guild {ctx.guild.name}.')
await ctx.reply(f'```Error: {error}```')
def setup(client):
client.add_cog(on_command_error(client))

View File

@ -9,7 +9,7 @@ import logging
from bot import configFile, yaml_load, yaml_dump
#### Actions for the Bot to take on connecting to Discord.
class on_connect(commands.Cog):
class on_connect(commands.Cog, name='On Connect Events'):
def __init__(self, client):
self.client = client

View File

@ -9,7 +9,7 @@ import logging
from bot import configFile, yaml_load, yaml_dump
##### Actions for the bot to take whenever a channel in a guild is deleted
class on_guild_channel_delete(commands.Cog):
class on_guild_channel_delete(commands.Cog, name='On Guild Channel Delete Events'):
def __init__(self, client):
self.client = client
@ -17,16 +17,16 @@ class on_guild_channel_delete(commands.Cog):
@commands.Cog.listener()
async def on_guild_channel_delete(self, channel):
conf = yaml_load(configFile)
if conf[str(channel.guild.id)]['modchannel'] == channel.id:
if conf[str(channel.guild.id)]['channels']['mod'] == channel.id:
if channel.guild.system_channel is None:
p = len(channel.guild.channels)
c = None
for t in channel.guild.text_channels:
if t.position < p:
p = t.position
conf[str(channel.guild.id)]['modchannel'] = t.id
conf[str(channel.guild.id)]['channels']['mod'] = t.id
else:
conf[str(channel.guild.id)]['modchannel'] = channel.guild.system_channel.id
conf[str(channel.guild.id)]['channels']['mod'] = channel.guild.system_channel.id
yaml_dump(conf, configFile)
def setup(client):

View File

@ -6,17 +6,21 @@ from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash C
from discord_slash.utils.manage_commands import create_choice, create_option # Slash Command features
import logging
# logger and handler
from bot import configFile, setConfig, yaml_load, yaml_dump
from bot import checkConfig, parseConfigCheck, configFile, setConfig, yaml_load, yaml_dump
#### Actions for the bot to take when the Bot joins a guild.
class on_guild_join(commands.Cog):
class on_guild_join(commands.Cog, name='On Guild Join Events'):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_guild_join(self, guild):
setConfig(guild)
status, output = checkConfig(guild)
conf = yaml_load(configFile)
if not status:
await guild.get_channel(conf[str(guild.id)]['channels']['mod']).send(f"The Bot's configurations are incomplete for the guild `{guild.name}`. Some limited functions will still be available, but most features cannot be used until the configurations are complete.\n{parseConfigCheck(output)}\nYou can set these configuration values using the `/config` command.")
def setup(client):
client.add_cog(on_guild_join(client))

View File

@ -10,7 +10,7 @@ from bot import clearConfig, configFile, yaml_load, yaml_dump
#### Actions for the bot to take when removed from a guild.
class on_guild_remove(commands.Cog):
class on_guild_remove(commands.Cog, name='On Guild Remove Events'):
def __init__(self, client):
self.client = client

View File

@ -10,7 +10,7 @@ from bot import configFile, yaml_load, yaml_dump
##### Actions for the bot to take whenever there is a new role created.
class on_guild_role_create(commands.Cog):
class on_guild_role_create(commands.Cog, name='On Guild Role Create Events'):
def __init__(self, client):
self.client = client
@ -19,7 +19,7 @@ class on_guild_role_create(commands.Cog):
conf = yaml_load(configFile)
#### Bot will only respond if the role is not a bot-managed role, and the role is an admin role
if not (role.is_bot_managed() or role.is_integration()) and role.permissions.administrator:
conf[str(role.guild.id)]['adminroles'].append(role.id)
conf[str(role.guild.id)]['roles']['admin'].append(role.id)
yaml_dump(conf, configFile)
#### If the role is created with admin privileges, the bot adds the role to its configs.

View File

@ -10,7 +10,7 @@ from bot import configFile, yaml_load, yaml_dump
##### Actions for the bot to take whenever there is a new role deleted.
class on_guild_role_delete(commands.Cog):
class on_guild_role_delete(commands.Cog, name='On Guild Role Delete Events'):
def __init__(self, client):
self.client = client
@ -18,8 +18,8 @@ class on_guild_role_delete(commands.Cog):
async def on_guild_role_delete(self, role):
conf = yaml_load(configFile)
#### 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[str(role.guild.id)]['adminroles']:
conf[str(role.guild.id)]['adminroles'].remove(role.id)
if role.id in conf[str(role.guild.id)]['roles']['admin']:
conf[str(role.guild.id)]['roles']['adminroles'].remove(role.id)
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.

View File

@ -10,7 +10,7 @@ from bot import configFile, yaml_load, yaml_dump
##### Actions for the bot to take whenever there is a new role deleted.
class on_guild_role_update(commands.Cog):
class on_guild_role_update(commands.Cog, name='On Guild Role Update Events'):
def __init__(self, client):
self.client = client
@ -18,15 +18,15 @@ class on_guild_role_update(commands.Cog):
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
if before.id in conf[str(before.guild.id)]['adminroles']:
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)]['adminroles'].remove(after.id)
conf[str(after.guild.id)]['roles']['admin'].remove(after.id)
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:
if after.id not in conf[str(after.guild.id)]['adminroles']:
conf[str(after.guild.id)]['adminroles'].remove(after.id)
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 deleted, updates the bot's config to delete that role, preventing unnecessary roles from accumulating.

View File

@ -9,7 +9,7 @@ import logging
from bot import configFile, yaml_load, yaml_dump
##### Actions for the bot to take whenever the guild info or ownership are updated.
class on_guild_update(commands.Cog):
class on_guild_update(commands.Cog, name='On Guild Update Events'):
def __init__(self, client):
self.client = client

View File

@ -0,0 +1,47 @@
import os # OS Locations
import yaml # YAML parser for Bot config files
import asyncio # Discord Py Dependency
import discord # Main Lib
from discord.ext import commands, tasks # 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
from datetime import datetime
import logging
# logger and handler
from bot import configFile, yaml_load
#### Actions the bot will take on messages being sent in the channel.
##### Message Listener Cog
class on_message(commands.Cog, name='On Message Events'):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_message(self,message):
if message.author.bot or message.author.id == message.guild.owner_id:
return
for role in message.author.roles:
if role.permissions.administrator:
return
conf = yaml_load(configFile)
guild = message.guild
guildStr = str(guild.id)
if 'notifications' in conf[guildStr]:
if 'help' in conf[guildStr]['notifications']:
if conf[guildStr]['notifications']['help']:
if 'help' in conf[guildStr]['channels'] and 'committee' in conf[guildStr]['roles']:
if message.channel.id == conf[guildStr]['channels']['help'] and isinstance(guild.get_role(conf[guildStr]['roles']['committee']), discord.Role):
modChannel = self.client.get_channel(conf[guildStr]['channels']['mod'])
committeeRole = guild.get_role(conf[guildStr]['roles']['committee'])
embed = discord.Embed(
title = f'[New Query in Help]({message.jump_url})',
description = message.content,
colour = discord.Colour.orange()
)
embed.set_footer(text=datetime.now().strftime('%a %-d %b %y, %-I:%M %p'))
embed.set_author(name=message.author.display_name, icon_url=message.author.avatar_url)
await modChannel.send(f'{committeeRole.mention}\n```There has been a new help query posted.```\n{message.author.mention}` posted in `{message.channel.mention}`.`', embed = embed)
def setup(client):
client.add_cog(on_message(client))

View File

@ -7,10 +7,10 @@ from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash C
from discord_slash.utils.manage_commands import create_choice, create_option # Slash Command features
import logging
# logger and handler
from bot import clearConfig, configFile, setConfig, yaml_dump, yaml_load
from bot import checkConfig, clearConfig, configFile, parseConfigCheck, setConfig, yaml_dump, yaml_load
#### Actions for the Bot to take once it is ready to interact with commands.
class on_ready(commands.Cog):
class on_ready(commands.Cog, name='On Ready Events'):
def __init__(self, client):
self.client = client
@ -26,5 +26,12 @@ class on_ready(commands.Cog):
for key in list(conf):
clearConfig(key)
#### Check completeness of configurations
for guild in self.client.guilds:
status, output = checkConfig(guild)
conf = yaml_load(configFile)
if not status:
await guild.get_channel(conf[str(guild.id)]['channels']['mod']).send(f"```The Bot's configurations are incomplete for the guild `{guild.name}`. Some limited functions will still be available, but most features cannot be used until the configurations are complete.\n{parseConfigCheck(output)}\nYou can set these configuration values using the `/config` command.```")
def setup(client):
client.add_cog(on_ready(client))