117 lines
3.5 KiB
Python
117 lines
3.5 KiB
Python
|
import os # OS Locations
|
||
|
from dotenv import load_dotenv # Import OS variables from Dotenv file.
|
||
|
load_dotenv() # Load Dotenv. Delete this for production
|
||
|
import yaml # Parser for yaml files for config settings.
|
||
|
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
|
||
|
|
||
|
## Define YAML functions
|
||
|
|
||
|
def yaml_load(filepath):
|
||
|
### Loads a YAML file
|
||
|
with open(filepath, 'r') as file:
|
||
|
data = yaml.load(file)
|
||
|
return data
|
||
|
|
||
|
def yaml_dump(data, filepath):
|
||
|
### Dumps a YAML file
|
||
|
with open(filepath, 'w') as file:
|
||
|
yaml.dump(data, file)
|
||
|
|
||
|
# Locate or create config File
|
||
|
configFile = './data/config.yml'
|
||
|
|
||
|
if not os.path.exists(configFile):
|
||
|
yaml_dump({},configFile)
|
||
|
|
||
|
# Locate Cogs Directory
|
||
|
cogsDir = 'dev_cogs'
|
||
|
|
||
|
## Logging configuration imported boilerplate from Discord Py Docs
|
||
|
logger = logging.getLogger('discord')
|
||
|
logger.setLevel(logging.DEBUG)
|
||
|
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
|
||
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
||
|
logger.addHandler(handler)
|
||
|
|
||
|
# Define Clients
|
||
|
client = commands.Bot(
|
||
|
intents=discord.Intents.all(),
|
||
|
command_prefix='¬'
|
||
|
)
|
||
|
slash = SlashCommand(
|
||
|
client,
|
||
|
sync_commands = True,
|
||
|
sync_on_cog_reload = True
|
||
|
)
|
||
|
# sync_on_reload is an important parameter that will become relevant when having to reload cogs on changing bot configs.
|
||
|
|
||
|
# Define Config keys
|
||
|
configKeys = ['adminroles', 'botrole', 'modchannel', 'name', 'owner', 'prefix']
|
||
|
|
||
|
# Create list of guild IDs the bot is currently in
|
||
|
# guild_ids = [864651943820525609]
|
||
|
guild_ids = []
|
||
|
|
||
|
# Retrieve IDs from config file
|
||
|
conf = yaml_load(configFile)
|
||
|
for guild in conf:
|
||
|
if int(guild) not in guild_ids:
|
||
|
guild_ids.append(int(guild))
|
||
|
# Will check on bot ready if any other guilds are missing fron configs.
|
||
|
|
||
|
@client.command(name='foobartest')
|
||
|
async def foobartest(ctx):
|
||
|
f = await utils.manage_commands.get_all_commands(client.user.id,os.getenv('TEST_3_TOKEN'),guild_id=ctx.guild.id)
|
||
|
print(f)
|
||
|
|
||
|
def loadCommands():
|
||
|
for cogfile in os.listdir(f'./{cogsDir}/commands'):
|
||
|
logging.info('Loading commands.')
|
||
|
if cogfile.endswith('.py'):
|
||
|
c = cogfile[:-3]
|
||
|
client.load_extension(f'{cogsDir}.commands.{c}')
|
||
|
print(f'Loaded Cog ./{cogsDir}/{c}')
|
||
|
logging.info(f'Loaded Cog ./app/{cogsDir}/commands/{c}')
|
||
|
|
||
|
def unloadCommands():
|
||
|
for cogfile in os.listdir(f'./{cogsDir}/commands'):
|
||
|
logging.info('Unloading commands.')
|
||
|
if cogfile.endswith('.py'):
|
||
|
c = cogfile[:-3]
|
||
|
client.unload_extension(f'{cogsDir}.commands.{c}')
|
||
|
print(f'Unloaded Cog ./{cogsDir}/{c}')
|
||
|
logging.info(f'Unloaded Cog ./{cogsDir}/commands/{c}')
|
||
|
|
||
|
def reloadCommands():
|
||
|
unloadCommands()
|
||
|
loadCommands()
|
||
|
|
||
|
def loadAllCogs():
|
||
|
for cogfile in os.listdir(f'./{cogsDir}'):
|
||
|
logging.info('Loading cogs.')
|
||
|
if cogfile.endswith('.py'):
|
||
|
c = cogfile[:-3]
|
||
|
client.load_extension(f'{cogsDir}.{c}')
|
||
|
print(f'Loaded Cog ./{cogsDir}/{c}')
|
||
|
logging.info(f'Loaded Cog ./app/{cogsDir}/{c}')
|
||
|
|
||
|
def unloadAllCogs():
|
||
|
for cogfile in os.listdir(f'./{cogsDir}'):
|
||
|
logging.info('Unloading cogs.')
|
||
|
if cogfile.endswith('.py'):
|
||
|
c = cogfile[:-3]
|
||
|
client.unload_extension(f'{cogsDir}.{c}')
|
||
|
print(f'Unloaded Cog ./{cogsDir}/{c}')
|
||
|
logging.info(f'Unloaded Cog ./{cogsDir}/{c}')
|
||
|
|
||
|
def reloadAllCogs():
|
||
|
unloadAllCogs()
|
||
|
loadAllCogs()
|
||
|
|
||
|
loadAllCogs()
|
||
|
client.run(os.getenv('TEST_3_TOKEN'))
|