15 July Nightly Commit
Split cogs into different files About to change file structuring to move dev file to main file
This commit is contained in:
26
app/dev_cogs/botcommands/prefix.py
Normal file
26
app/dev_cogs/botcommands/prefix.py
Normal file
@ -0,0 +1,26 @@
|
||||
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 dev import configFile, yaml_load, yaml_dump
|
||||
|
||||
### Cog for handling the non-Slash prefix for native Bot commands.
|
||||
|
||||
class Prefix(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.command(
|
||||
name = 'changeprefix',
|
||||
aliases = ['prefix'],
|
||||
description = 'This command changes the prefix string for the native, non-slash command functionality of the bot. Defaults to `-`. It does not affect the workings of /commands.',
|
||||
brief = 'Changes the bot prefix.'
|
||||
)
|
||||
async def _changePrefix(self, ctx, prefix:str = '-'):
|
||||
conf = yaml_load(configFile)
|
||||
conf[str(ctx.guild.id)]['prefix'] = prefix.lower()
|
||||
yaml_dump(conf, configFile)
|
||||
await ctx.send(f"`{self.client.user.name}`'s prefix for native bot commands has been changed to `{prefix}` for the guild `{ctx.guild.name}`.\n`Note: This will not affect /commands.`")
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(Prefix(client))
|
@ -1,51 +0,0 @@
|
||||
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))
|
31
app/dev_cogs/dev/devcog.py
Normal file
31
app/dev_cogs/dev/devcog.py
Normal file
@ -0,0 +1,31 @@
|
||||
import os
|
||||
from dotenv import load_dotenv # Import OS variables from Dotenv file.
|
||||
load_dotenv() # Load Dotenv. Delete this for production
|
||||
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
|
||||
|
||||
from dev import loadCog, unloadCog
|
||||
|
||||
##### Debug Cog
|
||||
class DevCog(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.command(
|
||||
name='debug',
|
||||
description='Toggles debug feature for the guild. Enter either `on` or `off`.',
|
||||
brief='Toggle debug features.'
|
||||
)
|
||||
async def _debug(self, ctx, toggle:str):
|
||||
if toggle.lower() == 'on':
|
||||
loadCog(f'./debug/debug.py')
|
||||
await ctx.reply(f'Debug commands enabled. Use them carefully.')
|
||||
elif toggle.lower() == 'off':
|
||||
unloadCog(f'./debug/debug.py')
|
||||
await ctx.reply(f'Debug commands disabled.')
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(DevCog(client))
|
@ -1,90 +0,0 @@
|
||||
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))
|
30
app/dev_cogs/events/on_connect.py
Normal file
30
app/dev_cogs/events/on_connect.py
Normal file
@ -0,0 +1,30 @@
|
||||
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 dev import configFile, yaml_load, yaml_dump
|
||||
|
||||
#### Actions for the Bot to take on connecting to Discord.
|
||||
class on_connect(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.')
|
||||
await self.client.change_presence(
|
||||
status = discord.Status.online,
|
||||
activity = discord.Activity(
|
||||
type = discord.ActivityType.listening,
|
||||
name = f'/commands'
|
||||
)
|
||||
)
|
||||
# for g in self.client.guilds:
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_connect(client))
|
33
app/dev_cogs/events/on_guild_channel_delete.py
Normal file
33
app/dev_cogs/events/on_guild_channel_delete.py
Normal file
@ -0,0 +1,33 @@
|
||||
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 dev 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):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
#### What to do if a mod channel gets deleted: try and pull default system channel, and if not then top-most channel
|
||||
@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 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
|
||||
else:
|
||||
conf[str(channel.guild.id)]['modchannel'] = channel.guild.system_channel.id
|
||||
yaml_dump(conf, configFile)
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_guild_channel_delete(client))
|
22
app/dev_cogs/events/on_guild_join.py
Normal file
22
app/dev_cogs/events/on_guild_join.py
Normal file
@ -0,0 +1,22 @@
|
||||
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 dev import configFile, setConfig, yaml_load, yaml_dump
|
||||
|
||||
#### Actions for the bot to take when the Bot joins a guild.
|
||||
|
||||
class on_guild_join(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_guild_join(self, guild):
|
||||
setConfig(guild)
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_guild_join(client))
|
22
app/dev_cogs/events/on_guild_remove.py
Normal file
22
app/dev_cogs/events/on_guild_remove.py
Normal file
@ -0,0 +1,22 @@
|
||||
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 dev import clearConfig, configFile, yaml_load, yaml_dump
|
||||
|
||||
#### Actions for the bot to take when removed from a guild.
|
||||
|
||||
class on_guild_remove(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_guild_remove(self, guild): ## Actions for when the bot is removed from a guild.
|
||||
clearConfig(str(guild.id))
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_guild_remove(client))
|
27
app/dev_cogs/events/on_guild_role_create.py
Normal file
27
app/dev_cogs/events/on_guild_role_create.py
Normal 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 dev 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):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_guild_role_create(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 not (role.is_bot_managed() or role.is_integration()) and role.permissions.administrator:
|
||||
conf[str(role.guild.id)]['adminroles'].append(role.id)
|
||||
yaml_dump(conf, configFile)
|
||||
#### If the role is created with admin privileges, the bot adds the role to its configs.
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_guild_role_create(client))
|
27
app/dev_cogs/events/on_guild_role_delete.py
Normal file
27
app/dev_cogs/events/on_guild_role_delete.py
Normal 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 dev 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):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
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)
|
||||
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.
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_guild_role_delete(client))
|
34
app/dev_cogs/events/on_guild_role_update.py
Normal file
34
app/dev_cogs/events/on_guild_role_update.py
Normal file
@ -0,0 +1,34 @@
|
||||
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 dev 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):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
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 after.is_bot_managed() or after.is_integration() or not after.permissions.administrator:
|
||||
conf[str(after.guild.id)]['adminroles'].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)
|
||||
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.
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_guild_role_update(client))
|
25
app/dev_cogs/events/on_guild_update.py
Normal file
25
app/dev_cogs/events/on_guild_update.py
Normal file
@ -0,0 +1,25 @@
|
||||
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 dev 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):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_guild_update(self, before, after):
|
||||
conf = yaml_load(configFile)
|
||||
conf[str(after.id)]['name'] = after.name
|
||||
conf[str(after.id)]['owner'] = after.owner_id
|
||||
# Updates guild name and channel
|
||||
yaml_dump(conf,configFile)
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_guild_update(client))
|
30
app/dev_cogs/events/on_ready.py
Normal file
30
app/dev_cogs/events/on_ready.py
Normal file
@ -0,0 +1,30 @@
|
||||
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 # 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 dev import clearConfig, configFile, setConfig, yaml_dump, yaml_load
|
||||
|
||||
#### Actions for the Bot to take once it is ready to interact with commands.
|
||||
class on_ready(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
|
||||
#### Create any missing config entries for guilds
|
||||
for guild in self.client.guilds:
|
||||
setConfig(guild)
|
||||
|
||||
#### Delete any extra config entries for guilds the bot is not in
|
||||
conf = yaml_load(configFile)
|
||||
for key in list(conf):
|
||||
clearConfig(key)
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(on_ready(client))
|
@ -1,13 +1,11 @@
|
||||
import os # OS Locations
|
||||
import yaml # YAML parser for Bot config files
|
||||
import json # Json Library to manage json Data 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
|
||||
|
||||
from dev import guild_ids
|
||||
|
||||
##### Configuration Cog
|
||||
class Configuration(commands.Cog):
|
||||
def __init__(self, client):
|
Reference in New Issue
Block a user