From f46753ed306232a679a79b1987af7b063fd68d68 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Tue, 22 May 2018 22:08:01 +0100 Subject: [PATCH 1/5] Update run.py --- run.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/run.py b/run.py index dffb7a8..a6723d3 100644 --- a/run.py +++ b/run.py @@ -6,12 +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 +logging.basicConfig(level='INFO') + # If uvloop is installed, change to that eventloop policy as it # is more efficient try: @@ -19,34 +22,36 @@ try: asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) del uvloop except BaseException as ex: - print(f'Could not load uvloop. {type(ex).__name__}: {ex};', - 'reverting to default impl.') + 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(print(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.logger.warning('MAINTENANCE ACTIVE') async def on_command_error(self, ctx, error): """ @@ -79,5 +84,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"]) From f898302d97c10a139cb392d8c9ee2f085b5d0c64 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Tue, 22 May 2018 22:16:03 +0100 Subject: [PATCH 2/5] Create loggable --- src/shared_libs/loggable | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/shared_libs/loggable diff --git a/src/shared_libs/loggable b/src/shared_libs/loggable new file mode 100644 index 0000000..10e9e59 --- /dev/null +++ b/src/shared_libs/loggable @@ -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__) From 83cee5c1a660ec210847761f539d0791981eff67 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Tue, 22 May 2018 22:16:43 +0100 Subject: [PATCH 3/5] Rename loggable to loggable.py --- src/shared_libs/{loggable => loggable.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/shared_libs/{loggable => loggable.py} (100%) diff --git a/src/shared_libs/loggable b/src/shared_libs/loggable.py similarity index 100% rename from src/shared_libs/loggable rename to src/shared_libs/loggable.py From 88d94529ad72c731c091fdaf1edc683659a9fd22 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Tue, 22 May 2018 22:21:05 +0100 Subject: [PATCH 4/5] Update run.py --- run.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/run.py b/run.py index a6723d3..3c16799 100644 --- a/run.py +++ b/run.py @@ -12,6 +12,7 @@ import random # Import custom files from src.config.config import LoadConfig +from src.shared_libs.loggable import Loggable logging.basicConfig(level='INFO') @@ -23,9 +24,9 @@ try: del uvloop except BaseException as ex: logging.warning(f'Could not load uvloop. {type(ex).__name__}: {ex};', - 'reverting to default impl.')) + 'reverting to default impl.') else: - logging.info(print(f'Using uvloop for asyncio event loop policy.')) + logging.info(f'Using uvloop for asyncio event loop policy.') # Bot Class @@ -47,11 +48,9 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable): self.load_extension(f'src.cogs.{cog}') self.logger.info(f'Loaded: {cog}') - async def on_ready(self): """On ready function""" - if self.maintenance: - self.logger.warning('MAINTENANCE ACTIVE') + self.maintenance and self.logger.warning('MAINTENANCE ACTIVE') async def on_command_error(self, ctx, error): """ @@ -76,7 +75,12 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable): 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__': From 4ce771d0eaaa0653d451bac8481d155d6d8ad117 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Tue, 22 May 2018 22:22:06 +0100 Subject: [PATCH 5/5] Init files (#10) * Create __init__.py * Create __init__.py * Update __init__.py * Update __init__.py * Create __init__.py * Create __init__.py --- src/__init__.py | 15 +++++++++++++++ src/cogs/__init__.py | 2 ++ src/config/__init__.py | 2 ++ src/shared_libs/__init__.py | 2 ++ 4 files changed, 21 insertions(+) create mode 100644 src/__init__.py create mode 100644 src/cogs/__init__.py create mode 100644 src/config/__init__.py create mode 100644 src/shared_libs/__init__.py 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 -*-