Allowed debug usage of commands
This commit is contained in:
parent
6990185736
commit
9cd325869e
18
main.py
18
main.py
|
|
@ -63,6 +63,9 @@ class ClientWrapper(object):
|
|||
else:
|
||||
self.slack = SlackClient(SLACK_API)
|
||||
|
||||
# For overriding output channel
|
||||
self.debug_slack = slack_util.SlackDebugCondom(self.slack)
|
||||
|
||||
# Hooks go regex -> callback on (slack, msg, match)
|
||||
self.hooks = []
|
||||
|
||||
|
|
@ -83,15 +86,24 @@ class ClientWrapper(object):
|
|||
continue
|
||||
|
||||
# Handle Message
|
||||
text = msg['text'].strip()
|
||||
msg['text'] = msg['text'].strip()
|
||||
|
||||
# If first few letters DEBUG, use debug slack
|
||||
if msg['text'][:6] == "DEBUG ":
|
||||
slack_to_use = self.debug_slack
|
||||
msg['text'] = msg['text'][6:]
|
||||
print("Debug handling \"{}\"".format(msg['text']))
|
||||
else:
|
||||
slack_to_use = self.slack
|
||||
|
||||
success = False
|
||||
for hook in self.hooks:
|
||||
if hook.check(self.slack, msg):
|
||||
if hook.check(slack_to_use, msg):
|
||||
success = True
|
||||
break
|
||||
|
||||
if not success:
|
||||
print("No hit on {}".format(text))
|
||||
print("No hit on {}".format(msg['text']))
|
||||
|
||||
|
||||
# run main
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
from time import sleep
|
||||
import re
|
||||
import channel_util
|
||||
from typing import Any
|
||||
|
||||
"""
|
||||
Slack helpers. Separated for compartmentalization
|
||||
|
|
@ -23,6 +25,28 @@ def reply(slack, msg, text, in_thread=True, to_channel=None):
|
|||
slack.rtm_send_message(channel=to_channel, message=text)
|
||||
|
||||
|
||||
class SlackDebugCondom(object):
|
||||
def __init__(self, actual_slack):
|
||||
self.actual_slack = actual_slack
|
||||
|
||||
def __getattribute__(self, name: str) -> Any:
|
||||
# Specialized behaviour
|
||||
if name == "rtm_send_message":
|
||||
# Flub some args
|
||||
def override_send_message(*args, **kwargs):
|
||||
print("Overriding: {} {}".format(args, kwargs))
|
||||
kwargs["channel"] = channel_util.BOTZONE
|
||||
kwargs["thread"] = None
|
||||
self.actual_slack.rtm_send_message(*args, **kwargs)
|
||||
return override_send_message
|
||||
else:
|
||||
# Default behaviour. Try to give the self first, elsewise give the child
|
||||
try:
|
||||
return super(SlackDebugCondom, self).__getattribute__(name)
|
||||
except AttributeError:
|
||||
return self.actual_slack.__getattribute__(name)
|
||||
|
||||
|
||||
def message_stream(slack):
|
||||
"""
|
||||
Generator that yields messages from slack.
|
||||
|
|
|
|||
Loading…
Reference in New Issue