1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#--depends-on commands
from src import EventManager, ModuleManager, utils
SETTING_PREFIX = "command-alias-"
class VariableKeyError(KeyError):
pass
class Module(ModuleManager.BaseModule):
def _arg_replace(self, s, args_split, kwargs):
vars = {}
for i in range(len(args_split)):
vars[str(i)] = args_split[i]
vars["%d-" % i] = " ".join(args_split[i:])
vars["-"] = " ".join(args_split)
vars.update(kwargs)
not_found, new_s = utils.parse.format_token_replace(s, vars)
if not_found:
raise VariableKeyError(f"not found: {not_found!r}")
return new_s
def _get_alias(self, server, target, command):
setting = "%s%s" % (SETTING_PREFIX, command)
command = self.bot.get_setting(setting,
server.get_setting(setting,
target.get_setting(setting, None)))
if not command == None:
command, _, args = command.partition(" ")
return command, args
return None
def _get_aliases(self, targets):
alias_list = []
for target in targets:
alias_list += target.find_settings(prefix=SETTING_PREFIX)
aliases = {}
for alias, command in alias_list:
alias = alias.replace(SETTING_PREFIX, "", 1)
if not alias in aliases:
aliases[alias] = command
return aliases
@utils.hook("get.command")
def get_command(self, event):
alias = self._get_alias(event["server"], event["target"],
event["command"].command)
if not alias == None:
alias, alias_args = alias
given_args = []
if event["command"].args:
given_args = event["command"].args.split(" ")
try:
event["command"].args = self._arg_replace(alias_args,
given_args, event["kwargs"])
except VariableKeyError:
pass
else:
event["command"].command = alias
@utils.hook("received.command.alias",
permission="alias")
@utils.hook("received.command.balias",
permission="balias")
@utils.hook("received.command.calias",
require_mode="o", require_access="high,alias")
@utils.spec("!'list ?<alias>wordlower")
@utils.spec("!'add !<alias>wordlower !<command>wordlower ?<args>string")
@utils.spec("!'remove !<alias>wordlower")
@utils.kwarg("remove_empty", False)
def alias(self, event):
target = event["server"]
if event["command"] == "calias":
if not event["is_channel"]:
raise utils.EventError("%scalias can only be used in-channel"
% event["command_prefix"])
target = event["target"]
elif event["command"] == "balias":
target = self.bot
subcommand = event["spec"][0]
alias = event["spec"][1]
if subcommand == "list":
if alias:
setting = target.get_setting(f"{SETTING_PREFIX}{alias}", None)
if setting == None:
raise utils.EventError("I don't have an '%s' alias" % alias)
prefix = event["command_prefix"]
event["stdout"].write(f"{prefix}{alias}: {prefix}{setting}")
else:
aliases = self._get_aliases([target])
event["stdout"].write("Available aliases: %s" %
", ".join(sorted(aliases.keys())))
elif subcommand == "add":
command = event["spec"][2].lower()
args = event["spec"][3]
if args:
command = f"{command} {args}"
target.set_setting("%s%s" % (SETTING_PREFIX, alias), command)
event["stdout"].write("Added '%s' alias" % alias)
elif subcommand == "remove":
setting = f"{SETTING_PREFIX}{alias}"
if target.get_setting(setting, None) == None:
raise utils.EventError("I don't have an '%s' alias" % alias)
target.del_setting(setting)
event["stdout"].write("Removed '%s' alias" % alias)
else:
raise utils.EventError("Unknown subcommand '%s'" % subcommand)
|