15 July Build
Implemented YAML Implemented basic client introspection for guild metadata Added todo tracker
This commit is contained in:
37
app/dev_cogs/commands/config.py
Normal file
37
app/dev_cogs/commands/config.py
Normal file
@ -0,0 +1,37 @@
|
||||
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
|
||||
|
||||
from dev import guild_ids
|
||||
|
||||
##### Configuration Cog
|
||||
class Configuration(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@cog_ext.cog_slash(
|
||||
# base='botrole',
|
||||
# subcommand_group='configure',
|
||||
name='configure',
|
||||
description='Parameter to define the role assigned to the dice bots.',
|
||||
# base_description='Command to configure the various guild parameters.',
|
||||
# subcommand_group_description='These are configuration commands to set up the various guild parameters.',
|
||||
guild_ids=guild_ids
|
||||
# options=[
|
||||
# create_option(
|
||||
# name='botrole',
|
||||
# description='The role that the dice bots are assigned in order to access the text channels.'
|
||||
# type=8,
|
||||
# required=True
|
||||
# )
|
||||
# ]
|
||||
)
|
||||
async def _configure(self, ctx:SlashContext, option):
|
||||
await ctx.send(f'The `botrole` for the guild `{ctx.guild.name}` has been set to `{option}`.')
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(Configuration(client))
|
51
app/dev_cogs/commands/debug.py
Normal file
51
app/dev_cogs/commands/debug.py
Normal file
@ -0,0 +1,51 @@
|
||||
import os
|
||||
from dotenv import load_dotenv # Import OS variables from Dotenv file.
|
||||
load_dotenv() # Load Dotenv. Delete this for production
|
||||
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
|
||||
|
||||
from dev import guild_ids, unloadAllCogs, loadAllCogs, reloadAllCogs
|
||||
|
||||
##### Debug Cog
|
||||
class Debug(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@cog_ext.cog_slash(
|
||||
name='reload',
|
||||
description='Reloads all cogs',
|
||||
guild_ids=guild_ids
|
||||
)
|
||||
async def _reload(self, ctx:SlashContext):
|
||||
reloadAllCogs()
|
||||
await ctx.send('Reloading Cogs.')
|
||||
|
||||
@cog_ext.cog_slash(
|
||||
name='deleteAll',
|
||||
description='Deletes all Slash Commands',
|
||||
guild_ids=guild_ids
|
||||
)
|
||||
async def _deleteAll(self, ctx:SlashContext):
|
||||
await utils.manage_commands.remove_all_commands(
|
||||
bot_id=self.client.user.id,
|
||||
bot_token=os.getenv('TEST_3_TOKEN'),
|
||||
guild_ids=None
|
||||
)
|
||||
await utils.manage_commands.remove_all_commands(
|
||||
bot_id=self.client.user.id,
|
||||
bot_token=os.getenv('TEST_3_TOKEN'),
|
||||
guild_ids=guild_ids
|
||||
)
|
||||
await ctx.send('Deleted all commands.')
|
||||
|
||||
@commands.command(
|
||||
name='reloadAll'
|
||||
)
|
||||
async def _reloadAll(self, ctx):
|
||||
await ctx.send('Reloading all cogs.')
|
||||
reloadAllCogs()
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(Debug(client))
|
90
app/dev_cogs/events.py
Normal file
90
app/dev_cogs/events.py
Normal file
@ -0,0 +1,90 @@
|
||||
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))
|
Reference in New Issue
Block a user