Show warning for private-only commands in channels and visa versa

closes #89
This commit is contained in:
jesopo 2019-07-11 13:31:35 +01:00
parent a2265dbfd4
commit e7e32c0978

View file

@ -20,6 +20,11 @@ def _command_method_validate(s):
if s.upper() in COMMAND_METHODS: if s.upper() in COMMAND_METHODS:
return s.upper() return s.upper()
class BadContextException(Exception):
def __init__(self, required_context):
self.required_context = required_context
Exception.__init__(self)
class CommandMethodSetting(utils.Setting): class CommandMethodSetting(utils.Setting):
example = "NOTICE" example = "NOTICE"
def parse(self, value: str) -> typing.Any: def parse(self, value: str) -> typing.Any:
@ -103,6 +108,8 @@ class Module(ModuleManager.BaseModule):
return None, None return None, None
hook = None hook = None
channel_skip = False
private_skip = False
if self.has_command(command): if self.has_command(command):
for potential_hook in self.get_hooks(command): for potential_hook in self.get_hooks(command):
alias_of = self._get_alias_of(potential_hook) 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", if not is_channel and potential_hook.get_kwarg("channel_only",
False): False):
channel_skip = True
continue continue
if is_channel and potential_hook.get_kwarg("private_only", if is_channel and potential_hook.get_kwarg("private_only",
False): False):
private_skip = True
continue continue
hook = potential_hook hook = potential_hook
break break
if not hook and (private_skip or channel_skip):
raise BadContextException("channel" if channel_skip else "private")
return hook, args_split return hook, args_split
def _check(self, context, kwargs, requests=[]): def _check(self, context, kwargs, requests=[]):
@ -280,8 +292,15 @@ class Module(ModuleManager.BaseModule):
if event["action"]: if event["action"]:
return return
hook, args_split = self._find_command_hook(event["server"], command, try:
True, args_split) 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: if hook:
self.command(event["server"], event["channel"], self.command(event["server"], event["channel"],
event["target_str"], True, event["user"], command, event["target_str"], True, event["user"], command,
@ -325,8 +344,13 @@ class Module(ModuleManager.BaseModule):
args_split = event["message_split"][1:] args_split = event["message_split"][1:]
hook, args_split = self._find_command_hook(event["server"], command, try:
False, args_split) 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: if hook:
self.command(event["server"], event["user"], self.command(event["server"], event["user"],