hemppa/modules/cron.py

63 lines
2.2 KiB
Python
Raw Normal View History

2019-12-10 19:03:19 +02:00
import shlex
from datetime import datetime
2020-02-08 23:16:19 +02:00
2020-02-02 23:08:15 +02:00
from .common.module import BotModule
2019-12-10 19:03:19 +02:00
2020-01-02 14:27:29 +02:00
2020-02-02 23:08:15 +02:00
class MatrixModule(BotModule):
2020-01-02 14:27:29 +02:00
daily_commands = dict() # room_id -> command json
2019-12-10 19:03:19 +02:00
last_hour = datetime.now().hour
async def matrix_message(self, bot, room, event):
bot.must_be_admin(room, event)
2020-01-02 14:27:29 +02:00
2019-12-10 19:03:19 +02:00
args = shlex.split(event.body)
args.pop(0)
if len(args) == 3:
if args[0] == 'daily':
dailytime = int(args[1])
dailycmd = args[2]
if not self.daily_commands.get(room.room_id):
self.daily_commands[room.room_id] = []
2020-01-02 14:27:29 +02:00
self.daily_commands[room.room_id].append(
{'time': dailytime, 'command': dailycmd})
2019-12-10 19:03:19 +02:00
bot.save_settings()
await bot.send_text(room, 'Daily command added.')
elif len(args) == 1:
if args[0] == 'list':
await bot.send_text(room, 'Daily commands on this room: ' + str(self.daily_commands.get(room.room_id)))
elif args[0] == 'clear':
self.daily_commands.pop(room.room_id, None)
bot.save_settings()
await bot.send_text(room, 'Cleared commands on this room.')
def help(self):
2020-02-02 23:08:15 +02:00
return ('Runs scheduled commands')
2019-12-10 19:03:19 +02:00
def get_settings(self):
data = super().get_settings()
data['daily_commands'] = self.daily_commands
return data
2019-12-10 19:03:19 +02:00
def set_settings(self, data):
super().set_settings(data)
2019-12-10 19:03:19 +02:00
if data.get('daily_commands'):
self.daily_commands = data['daily_commands']
async def matrix_poll(self, bot, pollcount):
delete_rooms = []
2019-12-10 19:03:19 +02:00
if self.last_hour != datetime.now().hour:
self.last_hour = datetime.now().hour
for room_id in self.daily_commands:
if room_id in bot.client.rooms:
commands = self.daily_commands[room_id]
for command in commands:
if int(command['time']) == self.last_hour:
await bot.send_text(bot.get_room_by_id(room_id), command['command'])
else:
delete_rooms.append(room_id)
for roomid in delete_rooms:
2020-02-02 23:08:15 +02:00
self.daily_commands.pop(roomid, None)