hackbot/plugins/uptime.py

85 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/python
# -*- coding: utf-8 -*-
# file: uptime.py
# date: 25.07.2020
# desc: Returns bots uptime
import time
import datetime
import logging
logging = logging.getLogger()
from manager import Plugin
from constants import Const
class Plugin(Plugin):
'''
Returns the hackbots uptime. The variable birth from the globalvar
module is used to become the starttime.
'''
__module = __name__
__command = 'uptime'
__description = 'Returns hackbots uptime'
@staticmethod
def get_module():
return Plugin.__module
@staticmethod
def get_command():
return Plugin.__command
@staticmethod
def get_description():
return Plugin.__description
def __init__(self, callback=None):
self.callback = callback
def help(self):
return ('!uptime returns hackbots uptime. There are no options. '
'\nSyntax: !uptime')
def run(self, stanza):
'''
Starts the plugin.
'''
const = Const()
birth = const.birth
uptime = self.calculate_uptime(birth)
if uptime == False:
logging.warning('Error while calculating uptime')
self.callback('Error while calculating uptime')
else:
self.callback('My uptime is {}'.format(uptime))
def calculate_uptime(self, birth):
logging.debug('Calculate uptime since {} UTC'.format(birth))
start = self.birth2time(birth)
now = datetime.datetime.utcnow()
print('*** Start: {} :: Now: {}'.format(start, now))
try:
uptime = now - start
print('Uptime: {}'.format(uptime))
return uptime
except Exception as exc:
logging.warning('ERROR: {}'.format(exc))
return False
def birth2time(self, birth):
time_object = None
datum, zeit = birth.split('T')
jahr, monat, tag = datum.split('-')
stunde, minute, sekunde = zeit.split(':')
time_object = datetime.datetime(int(jahr), int(monat), int(tag), \
int(stunde), int(minute), int(sekunde), 0, None)
return time_object