we were using the message author before, which made sense for incoming messages, but then all the replies were getting dumped to a directory for the bot, which doesn't make much sense. this will use the "first recipient", which sounds kind of like a backwards-compat attribute but does what we want for single-user DMs if we ever care heavily about multi-user DMs we should maybe look at channel.recipients, but that probably changes over time, so we'd need to use channel.id?
81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
"""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.channel.user.name))
|
|
log_file = '{0:s}-{1:s}.log'.format(str(message.channel.user.name), 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)
|