Changed file structure.

Moved code to main bot and cog files.
This commit is contained in:
2021-07-15 23:13:01 +01:00
parent b0b417a8d2
commit 1fa5029212
23 changed files with 139 additions and 1454 deletions

View 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 bot 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))

View 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 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):
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))

View 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 bot 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))

View 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 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):
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))

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
##### 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))

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
##### 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))

View 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 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):
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))

View 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 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):
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))

View 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 bot 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))