add logger module, logs new/edit/delete msgs
this logs these messages to a file directory. more stuff may get added eventually
This commit is contained in:
parent
9c551d37a0
commit
213b9ff400
@ -39,6 +39,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'bot',
|
'bot',
|
||||||
'dice',
|
'dice',
|
||||||
|
'logger',
|
||||||
'weather',
|
'weather',
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -126,6 +127,7 @@ STATIC_URL = '/static/'
|
|||||||
# default bot settings
|
# default bot settings
|
||||||
|
|
||||||
DISCORD_BOT_TOKEN = 'token'
|
DISCORD_BOT_TOKEN = 'token'
|
||||||
|
LOGGER_BASE_DIR = './logs'
|
||||||
WEATHER_WEATHER_UNDERGROUND_API_KEY = 'key'
|
WEATHER_WEATHER_UNDERGROUND_API_KEY = 'key'
|
||||||
|
|
||||||
|
|
||||||
|
80
logger/__init__.py
Normal file
80
logger/__init__.py
Normal 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__)
|
||||||
|
|
||||||
|
|
||||||
|
@hitomi.event
|
||||||
|
async 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)
|
||||||
|
|
||||||
|
# let other stuff happen
|
||||||
|
await hitomi.process_commands(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)
|
Loading…
x
Reference in New Issue
Block a user