Place MessageTreater in context
This commit is contained in:
parent
4af108265b
commit
60f7c6eea7
@ -25,7 +25,6 @@ from nemubot import __version__
|
|||||||
from nemubot.consumer import Consumer, EventConsumer, MessageConsumer
|
from nemubot.consumer import Consumer, EventConsumer, MessageConsumer
|
||||||
from nemubot import datastore
|
from nemubot import datastore
|
||||||
import nemubot.hooks
|
import nemubot.hooks
|
||||||
from nemubot.modulecontext import ModuleContext
|
|
||||||
|
|
||||||
logger = logging.getLogger("nemubot")
|
logger = logging.getLogger("nemubot")
|
||||||
|
|
||||||
@ -70,19 +69,19 @@ class Bot(threading.Thread):
|
|||||||
self.event_timer = None
|
self.event_timer = None
|
||||||
|
|
||||||
# Own hooks
|
# Own hooks
|
||||||
from nemubot.hooks.manager import HooksManager
|
from nemubot.treatment import MessageTreater
|
||||||
self.hooks = HooksManager()
|
self.treater = MessageTreater()
|
||||||
|
|
||||||
import re
|
import re
|
||||||
def in_ping(msg):
|
def in_ping(msg):
|
||||||
if re.match("^ *(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)", msg.message, re.I) is not None:
|
if re.match("^ *(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)", msg.message, re.I) is not None:
|
||||||
return msg.respond("pong")
|
return msg.respond("pong")
|
||||||
self.hooks.add_hook(nemubot.hooks.Message(in_ping), "in", "DirectAsk")
|
self.treater.hm.add_hook(nemubot.hooks.Message(in_ping), "in", "DirectAsk")
|
||||||
|
|
||||||
def in_echo(msg):
|
def in_echo(msg):
|
||||||
from nemubot.message import Text
|
from nemubot.message import Text
|
||||||
return Text(msg.nick + ": " + " ".join(msg.args), to=msg.to_response)
|
return Text(msg.nick + ": " + " ".join(msg.args), to=msg.to_response)
|
||||||
self.hooks.add_hook(nemubot.hooks.Message(in_echo, "echo"), "in", "Command")
|
self.treater.hm.add_hook(nemubot.hooks.Message(in_echo, "echo"), "in", "Command")
|
||||||
|
|
||||||
def _help_msg(msg):
|
def _help_msg(msg):
|
||||||
"""Parse and response to help messages"""
|
"""Parse and response to help messages"""
|
||||||
@ -129,7 +128,7 @@ class Bot(threading.Thread):
|
|||||||
" de tous les modules disponibles localement",
|
" de tous les modules disponibles localement",
|
||||||
message=["\x03\x02%s\x03\x02 (%s)" % (im, self.modules[im].__doc__) for im in self.modules if self.modules[im].__doc__])
|
message=["\x03\x02%s\x03\x02 (%s)" % (im, self.modules[im].__doc__) for im in self.modules if self.modules[im].__doc__])
|
||||||
return res
|
return res
|
||||||
self.hooks.add_hook(nemubot.hooks.Message(_help_msg, "help"), "in", "Command")
|
self.treater.hm.add_hook(nemubot.hooks.Message(_help_msg, "help"), "in", "Command")
|
||||||
|
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
# Messages to be treated
|
# Messages to be treated
|
||||||
@ -400,6 +399,7 @@ class Bot(threading.Thread):
|
|||||||
module.print = prnt
|
module.print = prnt
|
||||||
|
|
||||||
# Create module context
|
# Create module context
|
||||||
|
from nemubot.modulecontext import ModuleContext
|
||||||
module.__nemubot_context__ = ModuleContext(self, module)
|
module.__nemubot_context__ = ModuleContext(self, module)
|
||||||
|
|
||||||
if not hasattr(module, "logger"):
|
if not hasattr(module, "logger"):
|
||||||
|
@ -58,10 +58,8 @@ class MessageConsumer:
|
|||||||
msg.frm_owner = (not hasattr(self.srv, "owner") or self.srv.owner == msg.frm)
|
msg.frm_owner = (not hasattr(self.srv, "owner") or self.srv.owner == msg.frm)
|
||||||
|
|
||||||
# Treat the message
|
# Treat the message
|
||||||
from nemubot.treatment import MessageTreater
|
|
||||||
mt = MessageTreater(context.hooks) # Should be in context, this is static
|
|
||||||
for msg in msgs:
|
for msg in msgs:
|
||||||
for res in mt.treat_msg(msg):
|
for res in context.treater.treat_msg(msg):
|
||||||
# Identify the destination
|
# Identify the destination
|
||||||
to_server = None
|
to_server = None
|
||||||
if isinstance(res, str):
|
if isinstance(res, str):
|
||||||
|
@ -62,13 +62,13 @@ class ModuleContext:
|
|||||||
def add_hook(store, hook):
|
def add_hook(store, hook):
|
||||||
store = convert_legacy_store(store)
|
store = convert_legacy_store(store)
|
||||||
self.hooks.append((store, hook))
|
self.hooks.append((store, hook))
|
||||||
return context.hooks.add_hook(hook, store)
|
return context.treater.hm.add_hook(hook, store)
|
||||||
def del_hook(store, hook):
|
def del_hook(store, hook):
|
||||||
store = convert_legacy_store(store)
|
store = convert_legacy_store(store)
|
||||||
self.hooks.remove((store, hook))
|
self.hooks.remove((store, hook))
|
||||||
return context.hooks.del_hook(hook, store)
|
return context.treater.hm.del_hook(hook, store)
|
||||||
def call_hook(store, msg):
|
def call_hook(store, msg):
|
||||||
for h in context.hooks.get_hooks(store):
|
for h in context.treater.hm.get_hooks(store):
|
||||||
if h.match(msg):
|
if h.match(msg):
|
||||||
res = h.run(msg)
|
res = h.run(msg)
|
||||||
if isinstance(res, list):
|
if isinstance(res, list):
|
||||||
|
@ -23,8 +23,9 @@ class MessageTreater:
|
|||||||
|
|
||||||
"""Treat a message"""
|
"""Treat a message"""
|
||||||
|
|
||||||
def __init__(self, hm):
|
def __init__(self):
|
||||||
self.hm = hm # Pointer to the HookManager
|
from nemubot.hooks.manager import HooksManager
|
||||||
|
self.hm = HooksManager()
|
||||||
|
|
||||||
|
|
||||||
def treat_msg(self, msg):
|
def treat_msg(self, msg):
|
||||||
|
Loading…
Reference in New Issue
Block a user