Last active
June 15, 2019 10:02
-
-
Save Justasic/d2a4a6489f53720151b3a748ef8bdfff to your computer and use it in GitHub Desktop.
a super basic Discord chat logging bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding: utf-8 | |
""" | |
Discord logging bot for logging basic channel messages. | |
Based on work by: | |
LogBot | |
A minimal IRC log bot | |
Written by Chris Oliver | |
Includes discord.py by https://github.com/Rapptz/discord.py | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation; either version 2 | |
of the License, or any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
""" | |
__author__ = "Justin Crawford <[email protected]>" | |
__version__ = "1.0.0" | |
__date__ = "10/20/2018" | |
__copyright__ = "Copyright (c) Chris Oliver and Justin Crawford" | |
__license__ = "GPL2" | |
####################################################### | |
# Config # | |
####################################################### | |
LOG_FOLDER = "/var/www/logs/" | |
#LOG_FOLDER = "/tmp/logs/" | |
DEFAULT_TIMEZONE = 'UTC' | |
#CHANNELS = [""] | |
import cgi | |
import os | |
import sys | |
import re | |
import itertools | |
import discord | |
import asyncio | |
from time import strftime | |
try: | |
from datetime import datetime | |
from pytz import timezone | |
except: pass | |
try: | |
from hashlib import sha256 | |
except: | |
pass | |
html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>%title%</title> | |
<style type="text/css"> | |
body { | |
background-color: #F8F8FF; | |
font-family: Fixed, monospace; | |
font-size: 13px; | |
} | |
h1 { | |
font-family: sans-serif; | |
font-size: 24px; | |
text-align: center; | |
} | |
a, .time { | |
color: #525552; | |
text-decoration: none; | |
} | |
a:hover, .time:hover { text-decoration: underline; } | |
.person { color: #DD1144; } | |
.notice { color: #AE768C; } | |
</style> | |
</head> | |
<body> | |
<h1>%title%</h1> | |
<a href=".."><-- Back</a><br /> | |
</body> | |
</html> | |
""" | |
pat1 = re.compile(r"(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL) | |
def urlify2(value): | |
return pat1.sub(r'\1<a href="\2" target="_blank">\3</a>', value) | |
### Helper functions | |
def append_line(filename, line): | |
data = open(filename, "rb").readlines()[:-2] | |
data += [line.encode(), b"\n<br />", b"\n</body>", b"\n</html>"] | |
write_lines(filename, data) | |
def write_lines(filename, lines): | |
f = open(filename, "wb") | |
f.writelines(lines) | |
f.close() | |
def write_string(filename, string): | |
f = open(filename, "wb") | |
f.write(string.encode()) | |
f.close() | |
def GetLineHTML(message): | |
author = message.author.display_name | |
color = "#{:x}".format(message.author.color.value) | |
time = message.created_at.strftime("%H:%M:%S") | |
msg = "<span class=\"person\" style=\"color:{color}\"><{author}></span> {content}".format(color=color, author=author, content=urlify2(message.content)) | |
return "<a href=\"#{time}\" name=\"{time}\" class=\"time\">[{time}]</a> {message} [<a href=\"{jump}\">Jump</a>]".format(time=time, message=msg, jump=message.jump_url) | |
class Lain(discord.Client): | |
async def on_ready(self): | |
print("Logging in as") | |
print(self.user.name) | |
print(self.user.id) | |
print("--------------") | |
async def on_message(self, message): | |
# Here we actually log messages from discord. | |
channel = message.channel.name | |
date = message.created_at.strftime("%Y-%m-%d") | |
chan_path= "{folder}/{channel}".format(folder=LOG_FOLDER, channel=channel) | |
log_path = "{chan_path}/{date}.html".format(chan_path=chan_path, date=date) | |
# Create our channel path and index file if it doesnt exist | |
if not os.path.exists(chan_path): | |
print("%s: directory doesn't exist for channel" % chan_path) | |
os.makedirs(chan_path) | |
write_string("{}/index.html".format(chan_path), html_header.replace("%title%", "{} | Logs".format(channel))) | |
append_line("{}/index.html".format(LOG_FOLDER), "<a href=\"{}/index.html\">{}</a>".format(channel.replace("#", "%23"), channel)) | |
# Create the log if it doesnt exist | |
if not os.path.exists(log_path): | |
print("Creating new log file %s" % log_path) | |
write_string(log_path, html_header.replace("%title%", "{} | Logs for {}".format(channel, date))) | |
# APpend to our index file. | |
append_line("{}/index.html".format(chan_path), "<a href=\"{0}.html\">{0}</a>".format(date)) | |
print("Appending to %s" % log_path) | |
# append the current message. | |
line = GetLineHTML(message) | |
append_line(log_path, line) | |
if __name__ == "__main__": | |
if not os.path.exists(LOG_FOLDER): | |
print("%s does not exist, creating and making index.html" % LOG_FOLDER) | |
os.makedirs(LOG_FOLDER) | |
write_string("%s/index.html" % LOG_FOLDER, html_header.replace("%title%", "Chat Logs")) | |
# Run as lain for now. | |
client = Lain() | |
client.run("your discord bot token here :)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment