-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Enabled the bot to run standalone, with an Atlas Database, or with a Local Database - Split event handlers into their own folder - Created helper scripts for environment variables Signed-off-by: Diana <[email protected]>
- Loading branch information
Showing
18 changed files
with
659 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
**/__pycache__ | ||
**/.venv | ||
**/.classpath | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml | ||
LICENSE | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
This file contains the operations to setup a new guild | ||
in the database, and is used by the bot when a new guild is added. | ||
""" | ||
|
||
import logging | ||
from discord.ext import commands | ||
from event_handlers.guilds import addMissingGuilds, setupGuildEvents | ||
from helpers.env import getEnvVar | ||
from helpers.terminal_colors import TerminalColors | ||
from helpers.core_cogs import loadCoreCogs | ||
|
||
|
||
logger = logging.getLogger("discord.bot.events") | ||
|
||
|
||
async def setupBotEvents(bot: commands.Bot): | ||
""" | ||
This function sets up the bot events | ||
""" | ||
|
||
@bot.event | ||
async def on_ready(): # pylint: disable=invalid-name | ||
""" | ||
Event: Bot is ready | ||
This event is called when the bot is ready to be used and | ||
prints information about the bot. | ||
""" | ||
|
||
if bot.user is not None: | ||
|
||
# Print the join URL | ||
logger.info( | ||
"Invite URL: \ | ||
%shttps://discord.com/api/oauth2/authorize?\ | ||
client_id=%s&permissions=8&scope=bot%s", | ||
TerminalColors.GREEN_BOLD, | ||
bot.user.id, | ||
TerminalColors.RESET_COLOR, | ||
) | ||
|
||
logger.info( | ||
"%s%s%s is connected to %s%s guilds %s", | ||
TerminalColors.GREEN_BOLD, | ||
bot.user.name, | ||
TerminalColors.RESET_COLOR, | ||
TerminalColors.GREEN_BOLD, | ||
len(bot.guilds), | ||
TerminalColors.RESET_COLOR, | ||
) | ||
|
||
logger.info("Loading core cogs...") | ||
await loadCoreCogs(bot, "core") | ||
|
||
if getEnvVar("DISCORD_USE_DATABASE", "False") == "True": | ||
await setupGuildEvents(bot) | ||
addMissingGuilds(bot) | ||
|
||
logger.info("Bot is ready") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
""" | ||
This file contains the operations to setup a new guild | ||
in the database, and is used by the bot when a new guild is added. | ||
""" | ||
|
||
import logging | ||
from pymongo import MongoClient | ||
from discord import Guild | ||
from discord.ext import commands | ||
from helpers.database.connection import DatabaseConnection | ||
from helpers.env import getEnvVar | ||
|
||
logger = logging.getLogger("discord.guilds") | ||
|
||
|
||
async def setupGuildEvents(bot: commands.Bot): | ||
""" | ||
This function sets up the bot events for the guildis | ||
""" | ||
|
||
# get the database connection | ||
db_connection = DatabaseConnection().get_connection() | ||
|
||
@bot.event | ||
async def on_guild_join(guild: Guild): # pylint: disable=invalid-name | ||
""" | ||
Add the guild to the database when the bot joins a new guild. | ||
""" | ||
if getEnvVar("DISCORD_USE_DATABASE", "False") == "True": | ||
# Add the guild to the database | ||
addGuild(db_connection, guild) | ||
|
||
@bot.event | ||
async def on_guild_remove(guild: Guild): # pylint: disable=invalid-name | ||
""" | ||
Remove the guild from the database when the bot leaves a guild. | ||
""" | ||
if getEnvVar("DISCORD_USE_DATABASE", "False") == "True": | ||
# Remove the guild to the database | ||
removeGuild(db_connection, guild) | ||
|
||
|
||
def addGuild(db_connection: MongoClient, guild: Guild) -> bool: | ||
""" | ||
Add a new guild to the database. | ||
""" | ||
# create the database | ||
guild_collection = db_connection["guilds"].create_collection(str(guild.id)) | ||
|
||
if guild_collection.name == str(guild.id): | ||
logger.info( | ||
"Database %s for guild %s created successfully", guild.id, guild.name | ||
) | ||
return True | ||
|
||
logger.error( | ||
"You shouldn't see this message, check that guild %s was created successfully", | ||
guild.name, | ||
) | ||
|
||
return False | ||
|
||
|
||
def removeGuild(db_connection: MongoClient, guild: Guild) -> bool: | ||
""" | ||
Remove a guild from the database. | ||
""" | ||
|
||
# Connect to the guilds database | ||
db_connection["guilds"].drop_collection(str(guild.id)) | ||
|
||
# check if the database was deleted | ||
if str(guild.id) not in db_connection["guilds"].list_collection_names(): | ||
logger.info( | ||
"Database %s for guild %s removed successfully", guild.id, guild.name | ||
) | ||
return True | ||
|
||
logger.error( | ||
"You shouldn't see this message, check that guild %s was removed successfully", | ||
guild.name, | ||
) | ||
|
||
return False | ||
|
||
|
||
def addMissingGuilds(bot: commands.Bot): | ||
""" | ||
Add the guilds that the bot is already in to the database. | ||
""" | ||
# get the database connection | ||
db_connection = DatabaseConnection().get_connection() | ||
|
||
# existing guilds in the database | ||
guilds = db_connection["guilds"].list_collection_names() | ||
|
||
# add the guilds to the database | ||
for guild in bot.guilds: | ||
if str(guild.id) not in guilds: | ||
addGuild(db_connection, guild) |
Oops, something went wrong.