use the config-based bot plugin loader

This commit is contained in:
Brian S. Stephan 2018-01-09 15:37:53 -06:00
parent 8b7ee5ee19
commit 7ec643afda
6 changed files with 118 additions and 107 deletions

View File

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

20
dice/bot.py Normal file
View File

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

View File

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

80
logger/bot.py Normal file
View File

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

View File

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

18
weather/bot.py Normal file
View File

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