-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalias.py
98 lines (78 loc) · 2.59 KB
/
alias.py
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
from errbot import BotPlugin, botcmd
class Alias(BotPlugin):
"""
Use shortcuts for long commands.
"""
def callback_message(self, mess):
"""Re-inject defined aliases to process_message"""
command = mess.body.split(' ', 1)
if len(command) > 0:
alias = command[0].strip(self._bot.prefix)
args = ''
if len(command) > 1:
args = command[1]
if alias in self:
mess.body = u'{prefix}{command} {args}'.format(
prefix=self._bot.prefix,
command=self[alias],
args=args
)
self._bot.process_message(mess)
@botcmd
def alias(self, mess, args):
"""List all aliases."""
if len(args) < 2:
return self.alias_list(mess, args)
@botcmd(split_args_with=None)
def alias_add(self, mess, args):
"""Define a new alias.
Usage:
!alias add <name> <command> [<args>]
Examples
!alias add s status
!alias add pr plugin reload
"""
if len(args) < 2:
return u'usage: {prefix}alias add <name> <command>'.format(
prefix=self._bot.prefix
)
name = args.pop(0)
command = ' '.join(args)
if name in self:
return u'An alias with that name already exists.'
self[name] = command
return u'Alias created.'
@botcmd
def alias_remove(self, mess, args):
"""Remove an alias.
Usage:
!alias remove <name>
"""
name = args
if not name:
return u'usage: {prefix}alias remove <name>'.format(
prefix=self._bot.prefix
)
try:
del self[name]
return u'Alias removed.'
except KeyError:
return u'That alias does not exist. ' \
'Use {prefix}alias list to see all aliases.'.format(
prefix=self._bot.prefix
)
@botcmd
def alias_list(self, mess, args):
"""List all aliases."""
if len(self) > 0:
return u'All Aliases:\n\n' + u'\n'.join(
['- {prefix}{alias} = {prefix}{command}'.format(
prefix=self._bot.prefix,
alias=alias,
command=self[alias]
) for alias in self])
else:
return u'No aliases found. ' \
'Use {prefix}alias add to define one.'.format(
prefix=self._bot.prefix
)