added logs.py and changed EventHook objects to know their name.
This commit is contained in:
parent
e604a8de31
commit
369b784a0d
2 changed files with 23 additions and 4 deletions
|
@ -1,7 +1,8 @@
|
||||||
|
|
||||||
class Event(object):
|
class Event(object):
|
||||||
def __init__(self, bot, **kwargs):
|
def __init__(self, bot, subevent, **kwargs):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
self.subevent = subevent
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.eaten = False
|
self.eaten = False
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
|
@ -34,8 +35,9 @@ class MultipleEventHook(object):
|
||||||
event_hook.call(max, **kwargs)
|
event_hook.call(max, **kwargs)
|
||||||
|
|
||||||
class EventHook(object):
|
class EventHook(object):
|
||||||
def __init__(self, bot):
|
def __init__(self, bot, name=None):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
self.name = name
|
||||||
self._children = {}
|
self._children = {}
|
||||||
self._hooks = []
|
self._hooks = []
|
||||||
self._hook_notify = None
|
self._hook_notify = None
|
||||||
|
@ -56,7 +58,7 @@ class EventHook(object):
|
||||||
return multiple_event_hook
|
return multiple_event_hook
|
||||||
return self.get_child(subevent)
|
return self.get_child(subevent)
|
||||||
def call(self, max=None, **kwargs):
|
def call(self, max=None, **kwargs):
|
||||||
event = Event(self.bot, **kwargs)
|
event = Event(self.bot, self.name, **kwargs)
|
||||||
if self._call_notify:
|
if self._call_notify:
|
||||||
self._call_notify(self, event)
|
self._call_notify(self, event)
|
||||||
called = 0
|
called = 0
|
||||||
|
@ -72,7 +74,8 @@ class EventHook(object):
|
||||||
def get_child(self, child_name):
|
def get_child(self, child_name):
|
||||||
child_name_lower = child_name.lower()
|
child_name_lower = child_name.lower()
|
||||||
if not child_name_lower in self._children:
|
if not child_name_lower in self._children:
|
||||||
self._children[child_name_lower] = EventHook(self.bot)
|
self._children[child_name_lower] = EventHook(self.bot,
|
||||||
|
child_name)
|
||||||
if self._child_notify:
|
if self._child_notify:
|
||||||
self._child_notify(self, self._children[
|
self._child_notify(self, self._children[
|
||||||
child_name_lower])
|
child_name_lower])
|
||||||
|
|
16
modules/logs.py
Normal file
16
modules/logs.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
class Module(object):
|
||||||
|
def __init__(self, bot):
|
||||||
|
bot.events.on("log").on("info", "warn", "error").hook(self.log)
|
||||||
|
|
||||||
|
def timestamp(self):
|
||||||
|
return datetime.datetime.utcnow().isoformat()+"Z"
|
||||||
|
|
||||||
|
def log(self, event):
|
||||||
|
log_level = event.name
|
||||||
|
timestamp = self.timestamp()
|
||||||
|
message = event["message"]
|
||||||
|
with open("bot.log", "a") as log_file:
|
||||||
|
log_file.write("%s [%s] %s" % (timestamp, log_level,
|
||||||
|
message))
|
Loading…
Reference in a new issue