diff --git a/run.py b/run.py index dffb7a8..3c16799 100644 --- a/run.py +++ b/run.py @@ -6,11 +6,15 @@ import asyncio import discord from discord.ext import commands import json +import logging import traceback import random # Import custom files from src.config.config import LoadConfig +from src.shared_libs.loggable import Loggable + +logging.basicConfig(level='INFO') # If uvloop is installed, change to that eventloop policy as it # is more efficient @@ -19,34 +23,34 @@ try: asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) del uvloop except BaseException as ex: - print(f'Could not load uvloop. {type(ex).__name__}: {ex};', + logging.warning(f'Could not load uvloop. {type(ex).__name__}: {ex};', 'reverting to default impl.') else: - print(f'Using uvloop for asyncio event loop policy.') + logging.info(f'Using uvloop for asyncio event loop policy.') # Bot Class -class SebiMachine(commands.Bot, LoadConfig): +class SebiMachine(commands.Bot, LoadConfig, Loggable): """This discord is dedicated to http://www.discord.gg/GWdhBSp""" def __init__(self): # Initialize and attach config / settings LoadConfig.__init__(self) commands.Bot.__init__(self, command_prefix=self.defaultprefix) - # Load plugins # Add your cog file name in this list with open('cogs.txt', 'r') as cog_file: cogs = cog_file.readlines() + for cog in cogs: - print(f'Loaded:{cog}') + # Could this just be replaced with `strip()`? cog = cog.replace('\n', '') self.load_extension(f'src.cogs.{cog}') - + self.logger.info(f'Loaded: {cog}') + async def on_ready(self): """On ready function""" - if self.maintenance: - print('MAINTENANCE ACTIVE') + self.maintenance and self.logger.warning('MAINTENANCE ACTIVE') async def on_command_error(self, ctx, error): """ @@ -71,7 +75,12 @@ class SebiMachine(commands.Bot, LoadConfig): joke = random.choice(jokes) fmt = f'**`{self.defaultprefix}{ctx.command}`**\n{joke}\n\n**{type(error).__name__}:**:\n```py\n{tb}\n```' simple_fmt = f'**`{self.defaultprefix}{ctx.command}`**\n{joke}\n\n**{type(error).__name__}:**:\n**`{error}`**' - await ctx.send(fmt) + + # Stops the error handler erroring. + try: + await ctx.send(fmt) + except: + traceback.print_exc() if __name__ == '__main__': @@ -79,5 +88,5 @@ if __name__ == '__main__': # Make sure the key stays private. with open('src/config/PrivateConfig.json') as fp: PrivateConfig = json.load(fp) - fp.close() + client.run(PrivateConfig["bot-key"]) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..0f704d9 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3.6 +# -*- coding: utf-8 -*- +""" +Sebi-Machine. +""" + +__author__ = 'Annihilator708' +# TODO: add yourselves here. I can't remember everyones handles. +__contributors__ = (__author__, 'Neko404NotFound',) +__license__ = 'MIT' +__title__ = 'Sebi-Machine' +__version__ = 'tbd' + +__repository__ = f'https://github.com/{__author__}/{__title__}' +__url__ = __repository__ diff --git a/src/cogs/__init__.py b/src/cogs/__init__.py new file mode 100644 index 0000000..ed9dcc7 --- /dev/null +++ b/src/cogs/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3.6 +# -*- coding: utf-8 -*- diff --git a/src/config/__init__.py b/src/config/__init__.py new file mode 100644 index 0000000..ed9dcc7 --- /dev/null +++ b/src/config/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3.6 +# -*- coding: utf-8 -*- diff --git a/src/shared_libs/__init__.py b/src/shared_libs/__init__.py new file mode 100644 index 0000000..ed9dcc7 --- /dev/null +++ b/src/shared_libs/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3.6 +# -*- coding: utf-8 -*- diff --git a/src/shared_libs/loggable.py b/src/shared_libs/loggable.py new file mode 100644 index 0000000..10e9e59 --- /dev/null +++ b/src/shared_libs/loggable.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3.6 +# -*- coding: utf-8 -*- +""" +Neko404NotFound 2018, MIT + +A mixin class that injects a suitably named logger into class scope +at runtime. + +Chosen to make this a slotted class, which means (as far as I can remember) +that it is not suitable to be made into an abc.ABC class. Slots will +enable derived slotted classes to be a bit more efficient at runtime and +boast faster lookups. +""" +import logging + + +class Loggable: + __slots__ = ('logger',) + + def __init_subclass__(cls, **_): + cls.logger = logging.getLogger(cls.__qualname__)