Merge remote-tracking branch 'origin/logging' into development

This commit is contained in:
annihilator708 2018-05-22 23:37:45 +02:00
commit dd7a9c8d4b
6 changed files with 61 additions and 10 deletions

29
run.py
View File

@ -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"])

15
src/__init__.py Normal file
View File

@ -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__

2
src/cogs/__init__.py Normal file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-

2
src/config/__init__.py Normal file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-

View File

@ -0,0 +1,2 @@
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-

View File

@ -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__)