updating logbot to the latest version of xbotlib .... woosh

This commit is contained in:
manetta 2021-01-30 17:36:59 +01:00
parent 00805bc7e7
commit 80f9ef4232
5 changed files with 82 additions and 74 deletions

BIN
LogBot/avatar.1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -6,12 +6,12 @@
<link rel="stylesheet" type="text/css" href="stylesheet.css"> <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head> </head>
<body> <body>
<h1>OMG</h1> <h1>{{ room }}</h1>
{% for num, msg in messages.items() %} {% for num, msg in db.items() %}
<div class="post"> <div class="post">
<p class="id">({{ num }})</p> <p class="key">({{ num }})</p>
<p class="message">{{ msg }}</p> <p class="message">{{ msg }}</p>
</div> </div>

View File

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

View File

@ -1,12 +1,11 @@
body{ body{
background-color: pink;
margin: 1em; margin: 1em;
} }
.post{ .post{
margin: 1em 0; margin: 1em 0;
clear: both; clear: both;
} }
.post p.id{ .post p.key{
float: left; float: left;
margin: 0 1em 1em; margin: 0 1em 1em;
} }