|
@ -2,92 +2,101 @@ from xbotlib import Bot |
|
|
import json |
|
|
import json |
|
|
import jinja2 |
|
|
import jinja2 |
|
|
import re |
|
|
import re |
|
|
|
|
|
import os |
|
|
|
|
|
import shutil |
|
|
|
|
|
|
|
|
db = 'storage.json' |
|
|
output = './' |
|
|
|
|
|
|
|
|
def readdb(): |
|
|
def add_to_db(self, message): |
|
|
storage = open(db, 'r').read() |
|
|
keys = [int(x) for x in self.db[message.room].keys()] |
|
|
messages = json.loads(storage) |
|
|
if not keys: |
|
|
return messages |
|
|
new_key = '0' |
|
|
|
|
|
|
|
|
def writedb(message): |
|
|
|
|
|
try: |
|
|
|
|
|
with open(db, 'r') as storage: |
|
|
|
|
|
messages = json.loads(storage.read()) |
|
|
|
|
|
if messages.keys(): |
|
|
|
|
|
keys = [int(x) for x in messages.keys()] |
|
|
|
|
|
keys.sort() |
|
|
|
|
|
lastid = keys[-1] |
|
|
|
|
|
nextid = lastid + 1 |
|
|
|
|
|
else: |
|
|
else: |
|
|
nextid = 0 |
|
|
new_key = str(keys[-1] + 1) |
|
|
messages[f'{ nextid }'] = message |
|
|
self.db[message.room][new_key] = message.content |
|
|
storage = open(db, 'w') |
|
|
|
|
|
storage.write(json.dumps(messages, indent=4)) |
|
|
def del_from_db(self, message, key): |
|
|
except IOError: |
|
|
del self.db[message.room][key] |
|
|
with open(db, 'w') as storage: |
|
|
|
|
|
storage.write(json.dumps('{}')) |
|
|
def write_log(self, message): |
|
|
writedb(message) |
|
|
template_file = 'index.html.j2' # Hmm... how to read the self.template ? |
|
|
return messages |
|
|
print('using the template: ', template_file) |
|
|
|
|
|
template = jinja2.Template(open(template_file).read()) |
|
|
def deletefromdb(id): |
|
|
if not os.path.isdir(message.room): |
|
|
with open(db, 'r') as storage: |
|
|
os.mkdir(message.room) |
|
|
messages = json.loads(storage.read()) |
|
|
shutil.copy('stylesheet.css', message.room) |
|
|
del messages[id] |
|
|
with open(f'{ output }/{ message.room }/index.html','w') as out: |
|
|
storage = open(db, 'w') |
|
|
html = template.render(room=message.room, db=self.db[message.room]) |
|
|
storage.write(json.dumps(messages, indent=4)) |
|
|
|
|
|
return messages |
|
|
|
|
|
|
|
|
|
|
|
def writelog(messages): |
|
|
|
|
|
template = jinja2.Template(open('index.html.j2').read()) |
|
|
|
|
|
with open('log.html','w') as out: |
|
|
|
|
|
html = template.render(messages=messages) |
|
|
|
|
|
out.write(html) |
|
|
out.write(html) |
|
|
|
|
|
print('writing to: ', f'{ message.room }/index.html') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class logbot(Bot): |
|
|
class logbot(Bot): |
|
|
|
|
|
|
|
|
help = '''Oh dear, logbot is here! |
|
|
help = '''Oh dear, logbot is here! |
|
|
|
|
|
|
|
|
@delete <num> |
|
|
(You can speak to the bot using @logbot or logbot:) |
|
|
Delete posts from the log. |
|
|
|
|
|
For example: @logbot @delete 5 |
|
|
<image>: Your image is added to the log. |
|
|
|
|
|
|
|
|
|
|
|
logbot @help: Print this message |
|
|
|
|
|
|
|
|
@bots |
|
|
logbot @add <message>: Add a message to the log. |
|
|
To see who is around :) |
|
|
|
|
|
|
|
|
|
|
|
@uptime |
|
|
logbot @delete <num>: Delete posts from the log. For example: @logbot @delete 5 |
|
|
To check how long @logbot has been around |
|
|
|
|
|
|
|
|
|
|
|
@help |
|
|
logbot @title <string>: Set the title of your log. [future-feature] |
|
|
Print this message |
|
|
|
|
|
|
|
|
logbot @style <element> <css-rule>: Edit the css of your log. For example: logbot @style body background-color: pink; [future-feature] |
|
|
|
|
|
|
|
|
|
|
|
logbot @uptime: To check how long @logbot has been around |
|
|
|
|
|
|
|
|
|
|
|
@bots: To see who is around :) |
|
|
''' |
|
|
''' |
|
|
|
|
|
|
|
|
def group(self, message): |
|
|
def group(self, message): |
|
|
print(message.content) |
|
|
|
|
|
messages = readdb() |
|
|
if not message.room in self.db.keys(): |
|
|
|
|
|
self.db[message.room] = {} |
|
|
|
|
|
|
|
|
|
|
|
# to debug in the terminal |
|
|
|
|
|
print('------------------') |
|
|
|
|
|
print('message: ', message.text) |
|
|
|
|
|
print('room: ', message.room) |
|
|
|
|
|
print('sender: ', message.sender) |
|
|
|
|
|
|
|
|
if message.url: |
|
|
if message.url: |
|
|
messages = writedb(f'<img src="{ message.url }">') |
|
|
# messages = writedb(f'<img src="{ message.url }">') |
|
|
reply = 'Thanks for that image!' |
|
|
reply = 'Thanks for that image!' |
|
|
|
|
|
|
|
|
|
|
|
elif '@add' in message.text: |
|
|
|
|
|
add_to_db(self, message) |
|
|
|
|
|
reply = 'Added, thanks!' |
|
|
|
|
|
|
|
|
elif '@delete' in message.text: |
|
|
elif '@delete' in message.text: |
|
|
match = re.findall("@delete \d*", message.content)[0] |
|
|
match = re.findall("@delete \d*", message.content)[0] |
|
|
id = match.replace('@delete ','') |
|
|
key = str(match.replace('@delete ','')) |
|
|
if id in messages: |
|
|
|
|
|
print('To be deleted:', messages[str(id)]) |
|
|
if key in self.db[message.room]: |
|
|
reply = f'This message is deleted: { messages[str(id)] }' |
|
|
print('To be deleted:', self.db[message.room][key]) |
|
|
messages = deletefromdb(id) |
|
|
reply = f'This message is deleted: { self.db[message.room][key] }' |
|
|
|
|
|
del_from_db(self, message, key) |
|
|
else: |
|
|
else: |
|
|
reply = 'This message is already gone!' |
|
|
reply = 'This message is already gone!' |
|
|
|
|
|
|
|
|
elif '@help' in message.text: |
|
|
elif '@help' in message.text: |
|
|
print('HELP') |
|
|
print('HELP') |
|
|
|
|
|
|
|
|
|
|
|
elif '@title' in message.text: |
|
|
|
|
|
reply = 'This is a future-feature ...' |
|
|
|
|
|
|
|
|
|
|
|
elif '@style' in message.text: |
|
|
|
|
|
reply = 'This is a future-feature ...' |
|
|
|
|
|
|
|
|
else: |
|
|
else: |
|
|
messages = writedb(message.text) |
|
|
reply = 'Hmm ... not sure what you want to do?' |
|
|
reply = 'Added, thanks!' |
|
|
|
|
|
|
|
|
|
|
|
writelog(messages) |
|
|
write_log(self, message) |
|
|
return self.reply(reply, room=message.room) |
|
|
return self.reply(reply, room=message.room) |
|
|
|
|
|
|
|
|
logbot() |
|
|
logbot() |
|
|