From 7ec643afda82958b057d5fc7e4dc0d696906fbb7 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Tue, 9 Jan 2018 15:37:53 -0600 Subject: [PATCH] use the config-based bot plugin loader --- dice/__init__.py | 15 --------- dice/bot.py | 20 ++++++++++++ logger/__init__.py | 79 -------------------------------------------- logger/bot.py | 80 +++++++++++++++++++++++++++++++++++++++++++++ weather/__init__.py | 13 -------- weather/bot.py | 18 ++++++++++ 6 files changed, 118 insertions(+), 107 deletions(-) create mode 100644 dice/bot.py create mode 100644 logger/bot.py create mode 100644 weather/bot.py diff --git a/dice/__init__.py b/dice/__init__.py index 7b1c49f..e69de29 100644 --- a/dice/__init__.py +++ b/dice/__init__.py @@ -1,15 +0,0 @@ -"""Roll dice and do other randomization type operations.""" -from bot import hitomi -from dice import lib - - -@hitomi.command() -async def choose(*choices: str): - """Randomly select one item from multiple choices.""" - await hitomi.say(lib.choose(*choices)) - - -@hitomi.command() -async def roll(*, roll_str: str): - """Provided a dice string, roll the dice and return the result.""" - await hitomi.say(lib.roll(roll_str)) diff --git a/dice/bot.py b/dice/bot.py new file mode 100644 index 0000000..cc1f4b7 --- /dev/null +++ b/dice/bot.py @@ -0,0 +1,20 @@ +"""Roll dice and do other randomization type operations.""" +import logging + +from bot import hitomi +from dice import lib + +logger = logging.getLogger(__name__) +logger.info("loading dice plugin") + + +@hitomi.command() +async def choose(*choices: str): + """Randomly select one item from multiple choices.""" + await hitomi.say(lib.choose(*choices)) + + +@hitomi.command() +async def roll(*, roll_str: str): + """Provided a dice string, roll the dice and return the result.""" + await hitomi.say(lib.roll(roll_str)) diff --git a/logger/__init__.py b/logger/__init__.py index 04d252e..e69de29 100644 --- a/logger/__init__.py +++ b/logger/__init__.py @@ -1,79 +0,0 @@ -"""Log messages showing up on Discord.""" -import logging -from os import makedirs - -from django.conf import settings - -from bot import hitomi - -logger = logging.getLogger(__name__) - - -def on_message(message): - """Log the seen message.""" - try: - log_file = log_file_for_message(message) - log_line = "{0:s}\t{1:s}\t{2:s}\t{3:s}".format(str(message.timestamp), str(message.author), str(message.id), - str(message.content)) - logger.info("{0:s} - {1:s}".format(log_file, log_line)) - with open(log_file, 'a') as log_fd: - print(log_line, file=log_fd) - except Exception as ex: - logger.error("error in creating log line") - logger.exception(ex) - - -hitomi.on_message_handlers.append(on_message) - - -@hitomi.event -async def on_message_delete(message): - """Log the message deletion.""" - try: - log_file = log_file_for_message(message) - log_line = "{0:s}\t{1:s}\tD:{2:s}\tDELETED: {3:s}".format(str(message.timestamp), str(message.author), - str(message.id), str(message.content)) - logger.info("{0:s} - {1:s}".format(log_file, log_line)) - with open(log_file, 'a') as log_fd: - print(log_line, file=log_fd) - except Exception as ex: - logger.error("error in creating log line") - logger.exception(ex) - - # let other stuff happen - await hitomi.process_commands(message) - - -@hitomi.event -async def on_message_edit(before, after): - """Log the message deletion.""" - try: - log_file = log_file_for_message(after) - log_line = "{0:s}\t{1:s}\tE:{2:s}\tEDIT: {3:s} (was \"{4:s}\")".format(str(after.timestamp), str(after.author), - str(after.id), str(after.content), - str(before.content)) - logger.info("{0:s} - {1:s}".format(log_file, log_line)) - with open(log_file, 'a') as log_fd: - print(log_line, file=log_fd) - except Exception as ex: - logger.error("error in creating log line") - logger.exception(ex) - - # let other stuff happen - await hitomi.process_commands(after) - - -def log_file_for_message(message): - """For the given message, figure out where we'd log the message.""" - if message.channel.is_private: - log_directory = 'DM/{0:s}/'.format(str(message.author)) - log_file = '{0:s}-{1:s}.log'.format(str(message.author), str(message.timestamp.strftime('%Y%m'))) - else: - log_directory = '{0:s}-{1:s}/{2:s}/'.format(str(message.server.id), str(message.server), - str(message.channel)) - log_file = '{0:s}-{1:s}.log'.format(str(message.channel), str(message.timestamp.strftime('%Y%m'))) - - destination_dir = '{0:s}/{1:s}'.format(settings.LOGGER_BASE_DIR, log_directory) - makedirs(destination_dir, exist_ok=True) - - return '{0:s}{1:s}'.format(destination_dir, log_file) diff --git a/logger/bot.py b/logger/bot.py new file mode 100644 index 0000000..f182e70 --- /dev/null +++ b/logger/bot.py @@ -0,0 +1,80 @@ +"""Log messages showing up on Discord.""" +import logging +from os import makedirs + +from django.conf import settings + +from bot import hitomi + +logger = logging.getLogger(__name__) +logger.info("loading logger plugin") + + +def on_message(message): + """Log the seen message.""" + try: + log_file = log_file_for_message(message) + log_line = "{0:s}\t{1:s}\t{2:s}\t{3:s}".format(str(message.timestamp), str(message.author), str(message.id), + str(message.content)) + logger.info("{0:s} - {1:s}".format(log_file, log_line)) + with open(log_file, 'a') as log_fd: + print(log_line, file=log_fd) + except Exception as ex: + logger.error("error in creating log line") + logger.exception(ex) + + +hitomi.on_message_handlers.append(on_message) + + +@hitomi.event +async def on_message_delete(message): + """Log the message deletion.""" + try: + log_file = log_file_for_message(message) + log_line = "{0:s}\t{1:s}\tD:{2:s}\tDELETED: {3:s}".format(str(message.timestamp), str(message.author), + str(message.id), str(message.content)) + logger.info("{0:s} - {1:s}".format(log_file, log_line)) + with open(log_file, 'a') as log_fd: + print(log_line, file=log_fd) + except Exception as ex: + logger.error("error in creating log line") + logger.exception(ex) + + # let other stuff happen + await hitomi.process_commands(message) + + +@hitomi.event +async def on_message_edit(before, after): + """Log the message deletion.""" + try: + log_file = log_file_for_message(after) + log_line = "{0:s}\t{1:s}\tE:{2:s}\tEDIT: {3:s} (was \"{4:s}\")".format(str(after.timestamp), str(after.author), + str(after.id), str(after.content), + str(before.content)) + logger.info("{0:s} - {1:s}".format(log_file, log_line)) + with open(log_file, 'a') as log_fd: + print(log_line, file=log_fd) + except Exception as ex: + logger.error("error in creating log line") + logger.exception(ex) + + # let other stuff happen + await hitomi.process_commands(after) + + +def log_file_for_message(message): + """For the given message, figure out where we'd log the message.""" + if message.channel.is_private: + log_directory = 'DM/{0:s}/'.format(str(message.author)) + log_file = '{0:s}-{1:s}.log'.format(str(message.author), str(message.timestamp.strftime('%Y%m'))) + else: + log_directory = '{0:s}-{1:s}/{2:s}/'.format(str(message.server.id), str(message.server), + str(message.channel)) + log_file = '{0:s}-{1:s}.log'.format(str(message.channel), str(message.timestamp.strftime('%Y%m'))) + + destination_dir = '{0:s}/{1:s}'.format(settings.LOGGER_BASE_DIR, log_directory) + makedirs(destination_dir, exist_ok=True) + + return '{0:s}{1:s}'.format(destination_dir, log_file) diff --git a/weather/__init__.py b/weather/__init__.py index 8b3011f..e69de29 100644 --- a/weather/__init__.py +++ b/weather/__init__.py @@ -1,13 +0,0 @@ -"""Provide weather information over Discord.""" -from bot import hitomi -from weather import lib - - -@hitomi.command() -async def forecast(query: str): - await hitomi.say(lib.get_forecast_for_query([query])) - - -@hitomi.command() -async def weather(query: str): - await hitomi.say(lib.get_conditions_for_query([query])) diff --git a/weather/bot.py b/weather/bot.py new file mode 100644 index 0000000..6409aa3 --- /dev/null +++ b/weather/bot.py @@ -0,0 +1,18 @@ +"""Provide weather information over Discord.""" +import logging + +from bot import hitomi +from weather import lib + +logger = logging.getLogger(__name__) +logger.info("loading weather plugin") + + +@hitomi.command() +async def forecast(query: str): + await hitomi.say(lib.get_forecast_for_query([query])) + + +@hitomi.command() +async def weather(query: str): + await hitomi.say(lib.get_conditions_for_query([query]))