moved functions to their own modules
deleted unused _essentials.py
This commit is contained in:
parent
b023724e09
commit
b1beb57dfc
5 changed files with 88 additions and 192 deletions
60
src/bot.py
Normal file
60
src/bot.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import os
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
import aiohttp
|
||||
from dotenv import load_dotenv
|
||||
import src.logger as logger
|
||||
import src.utils as utils
|
||||
|
||||
intents = discord.Intents.all()
|
||||
intents.message_content = True
|
||||
bot = commands.Bot(command_prefix='!', intents=intents)
|
||||
bot.logger = logger.logger_setup()
|
||||
|
||||
async def create_session():
|
||||
return aiohttp.ClientSession()
|
||||
|
||||
async def close_session(http_session):
|
||||
await http_session.close()
|
||||
|
||||
async def load_cogs(bot: commands.Bot, cog_path: str) -> None:
|
||||
for plugin_file in os.listdir(cog_path):
|
||||
if plugin_file[-3:] == '.py':
|
||||
try:
|
||||
await bot.load_extension(f'extensions.{plugin_file[:-3]}')
|
||||
bot.logger.info(f"Successfully loaded plugin {plugin_file}")
|
||||
except:
|
||||
bot.logger.exception(f"Failed to load plugin {plugin_file}")
|
||||
|
||||
@bot.event
|
||||
async def on_connect():
|
||||
bot.http_session = await create_session()
|
||||
|
||||
@bot.event
|
||||
async def on_resumed():
|
||||
bot.http_session = await create_session()
|
||||
|
||||
@bot.event
|
||||
async def on_disconnect():
|
||||
await close_session(bot.http_session)
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
try:
|
||||
await utils.delete_all_files("tmp/")
|
||||
await utils.folder_setup()
|
||||
await load_cogs(bot, 'extensions/')
|
||||
bot.logger.info('We have logged in as {0.user}'.format(bot))
|
||||
except:
|
||||
bot.logger.warning(f"Error in on_ready")
|
||||
|
||||
@bot.event
|
||||
async def on_message(ctx):
|
||||
try:
|
||||
await bot.process_commands(ctx)
|
||||
except commands.CommandNotFound:
|
||||
bot.logger.info("Command not found.")
|
||||
except discord.ext.commands.errors.CommandNotFound:
|
||||
bot.logger.info("Command not found.")
|
||||
except Exception as e:
|
||||
bot.logger.warning(f"Error processing commands: {e}")
|
||||
|
|
@ -2,6 +2,15 @@ import logging
|
|||
import os
|
||||
from logging.config import dictConfig
|
||||
|
||||
def logger_setup():
|
||||
if not os.path.isdir("logs"):
|
||||
os.mkdir("logs")
|
||||
with open("logs/info.log", "a") as f:
|
||||
pass
|
||||
logger = logging.getLogger("bot")
|
||||
return logger
|
||||
|
||||
|
||||
LOGGING_CONFIG = {
|
||||
"version": 1,
|
||||
"disabled_existing_loggers": False,
|
||||
|
|
@ -41,8 +50,4 @@ LOGGING_CONFIG = {
|
|||
},
|
||||
}
|
||||
|
||||
if not os.path.isdir("logs"):
|
||||
os.mkdir("logs")
|
||||
with open("logs/info.log", "a") as f:
|
||||
pass
|
||||
dictConfig(LOGGING_CONFIG)
|
||||
12
src/utils.py
Normal file
12
src/utils.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import os
|
||||
|
||||
async def folder_setup() -> None:
|
||||
folder_names = ["tmp", "extensions", "data", "logs"]
|
||||
for folder_name in folder_names:
|
||||
if not os.path.exists(folder_name):
|
||||
os.mkdir(folder_name)
|
||||
|
||||
async def delete_all_files(path: str) -> None:
|
||||
for filename in os.listdir(path):
|
||||
if os.path.isfile(path+filename):
|
||||
os.remove(path+filename)
|
||||
Loading…
Add table
Add a link
Reference in a new issue