aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar jesopo2019-07-11 13:31:35 +0100
committerGravatar jesopo2019-07-11 13:31:35 +0100
commite7e32c097899d1557f29fd7c8e9de7765d6a0885 (patch)
tree9db38be9dbb4c09af98ae6c8b9067677255838f9
parentMerge pull request #93 from fosslinux/todo-list (diff)
signature
Show warning for private-only commands in channels and visa versa
closes #89
-rw-r--r--modules/commands/__init__.py32
1 files changed, 28 insertions, 4 deletions
diff --git a/modules/commands/__init__.py b/modules/commands/__init__.py
index d24813b8..3dfa9890 100644
--- a/modules/commands/__init__.py
+++ b/modules/commands/__init__.py
@@ -20,6 +20,11 @@ def _command_method_validate(s):
if s.upper() in COMMAND_METHODS:
return s.upper()
+class BadContextException(Exception):
+ def __init__(self, required_context):
+ self.required_context = required_context
+ Exception.__init__(self)
+
class CommandMethodSetting(utils.Setting):
example = "NOTICE"
def parse(self, value: str) -> typing.Any:
@@ -103,6 +108,8 @@ class Module(ModuleManager.BaseModule):
return None, None
hook = None
+ channel_skip = False
+ private_skip = False
if self.has_command(command):
for potential_hook in self.get_hooks(command):
alias_of = self._get_alias_of(potential_hook)
@@ -116,14 +123,19 @@ class Module(ModuleManager.BaseModule):
if not is_channel and potential_hook.get_kwarg("channel_only",
False):
+ channel_skip = True
continue
if is_channel and potential_hook.get_kwarg("private_only",
False):
+ private_skip = True
continue
hook = potential_hook
break
+ if not hook and (private_skip or channel_skip):
+ raise BadContextException("channel" if channel_skip else "private")
+
return hook, args_split
def _check(self, context, kwargs, requests=[]):
@@ -280,8 +292,15 @@ class Module(ModuleManager.BaseModule):
if event["action"]:
return
- hook, args_split = self._find_command_hook(event["server"], command,
- True, args_split)
+ try:
+ hook, args_split = self._find_command_hook(event["server"],
+ command, True, args_split)
+ except BadContextException:
+ event["channel"].send_message(
+ "%s: That command is not valid in a channel" %
+ event["user"].nickname)
+ return
+
if hook:
self.command(event["server"], event["channel"],
event["target_str"], True, event["user"], command,
@@ -325,8 +344,13 @@ class Module(ModuleManager.BaseModule):
args_split = event["message_split"][1:]
- hook, args_split = self._find_command_hook(event["server"], command,
- False, args_split)
+ try:
+ hook, args_split = self._find_command_hook(event["server"],
+ command, False, args_split)
+ except BadContextException:
+ event["user"].send_message(
+ "That command is not valid in a PM")
+ return
if hook:
self.command(event["server"], event["user"],