Skip to content

Commit 5694e93

Browse files
paolobarboliniPietro Albini
authored andcommitted
Add ability to reorder commands in /help
This commit adds the optional `order` argument to the @Bot.command decorator, allowing you to reorder commands in `/help`. @bot.command("test", order=5) def test_commad(chat, message, args): """I'm the fifth!""" chat.send("I'm the fifth!")
1 parent 3ec2a02 commit 5694e93

File tree

9 files changed

+39
-19
lines changed

9 files changed

+39
-19
lines changed

botogram/bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ def message_edited(self, func):
142142
self._main_component.add_message_edited_hook(func)
143143
return func
144144

145-
def command(self, name, hidden=False):
145+
def command(self, name, hidden=False, order=0):
146146
"""Register a new command"""
147147
def __(func):
148148
self._main_component.add_command(name, func, hidden,
149-
_from_main=True)
149+
order=order, _from_main=True)
150150
return func
151151
return __
152152

botogram/commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self, hook, _bot=None):
1414
# Get some parameters from the hook
1515
self.name = hook._name
1616
self.hidden = hook._hidden
17+
self.order = hook._order
1718

1819
self._hook = hook
1920
self._component_id = hook.component_id

botogram/components.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def add_message_matches_hook(self, regex, func, flags=0, multiple=False):
9797
})
9898
self.__processors.append(hook)
9999

100-
def add_command(self, name, func, hidden=False, _from_main=False):
100+
def add_command(self, name, func, hidden=False, order=0, _from_main=False):
101101
"""Register a new command"""
102102
if name in self.__commands:
103103
raise NameError("The command /%s already exists" % name)
@@ -113,6 +113,7 @@ def add_command(self, name, func, hidden=False, _from_main=False):
113113
hook = hooks.CommandHook(func, self, {
114114
"name": name,
115115
"hidden": hidden,
116+
"order": order,
116117
})
117118
command = commands.Command(hook)
118119
self.__commands[name] = command

botogram/defaults.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ def _start_command_help(bot):
4545
# /help command
4646

4747
def help_command(self, bot, chat, args):
48-
commands = {cmd.name: cmd for cmd in bot.available_commands()}
48+
commands = list(bot.available_commands())
4949
if len(args) > 1:
5050
message = [bot._("<b>Error!</b> The <code>/help</code> command "
5151
"allows up to one argument.")]
5252
elif len(args) == 1:
53-
if args[0] in commands:
53+
if any(cmd.name == args[0] for cmd in commands):
5454
message = self._help_command_message(bot, commands, args[0])
5555
else:
5656
message = [bot._("<b>Unknown command:</b> "
@@ -78,11 +78,12 @@ def _help_generic_message(self, bot, commands):
7878
# Show help on commands
7979
if len(commands) > 0:
8080
message.append(bot._("<b>This bot supports those commands:</b>"))
81-
for name in sorted(commands.keys()):
82-
summary = escape_html(commands[name].summary)
81+
for command in commands:
82+
summary = escape_html(command.summary)
8383
if summary is None:
8484
summary = "<i>%s</i>" % bot._("No description available.")
85-
message.append("/%s <code>-</code> %s" % (name, summary))
85+
message.append("/%s <code>-</code> %s" %
86+
(command.name, summary))
8687
message.append("")
8788
message.append(bot._("You can also use <code>/help &lt;command&gt;"
8889
"</code> to get help about a specific "
@@ -107,7 +108,8 @@ def _help_command_message(self, bot, commands, command):
107108
"""Generate a command's help message"""
108109
message = []
109110

110-
docstring = escape_html(commands[command].docstring)
111+
docstring = escape_html(next((cmd for cmd in commands if
112+
cmd.name == command), None).docstring)
111113
if docstring is None:
112114
docstring = "<i>%s</i>" % bot._("No description available.")
113115
message.append("/%s <code>-</code> %s" % (command, docstring))

botogram/frozenbot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ def _(self, message, **args):
226226

227227
def available_commands(self, all=False):
228228
"""Get a list of the commands this bot implements"""
229-
for command in self._commands.values():
229+
s = sorted(self._commands.values(), key=lambda command: command.name)
230+
c = sorted(s, key=lambda command: command.order)
231+
232+
for command in c:
230233
# Remove `or command.name in self.hide_commands` in botogram 1.0
231234
is_hidden = command.hidden or command.name in self._hide_commands
232235
if all or not is_hidden:

botogram/hooks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ def _after_init(self, args):
174174
self._hidden = False
175175
if "hidden" in args:
176176
self._hidden = args["hidden"]
177+
self._order = 0
178+
if "order" in args:
179+
self._order = args["order"]
177180

178181
def _call(self, bot, update):
179182
message = update.message

docs/api/bot.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ components.
188188
189189
.. versionadded:: 0.3
190190

191-
.. py:decoratormethod:: command(name, [hidden=False])
191+
.. py:decoratormethod:: command(name, [hidden=False, order=0])
192192
193193
This decorator register a new command, and calls the decorated function
194194
when someone issues the command in a chat. The command will also be added
@@ -208,6 +208,11 @@ components.
208208

209209
:param str name: The name of the command.
210210
:param bool hidden: If the command should be hidden from ``/help``
211+
:param int order: The order in which the commands are shown in ``/help``
212+
213+
.. versionchanged:: 0.4
214+
215+
Added the ``order`` argument.
211216

212217
.. versionchanged:: 0.3
213218

docs/api/components.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ about how to create them in the ":ref:`custom-components`" chapter.
158158

159159
.. versionadded:: 0.3
160160

161-
.. py:method:: add_command(name, func, [hidden=False])
161+
.. py:method:: add_command(name, func, [hidden=False, order=0])
162162
163163
This function registers a new command, and calls the provided function
164164
when someone issues the command in a chat. The command will also be added
@@ -185,6 +185,11 @@ about how to create them in the ":ref:`custom-components`" chapter.
185185
:param str name: The name of the command.
186186
:param callable func: The function you want to use.
187187
:param bool hidden: If the command should be hidden from ``/help``
188+
:param int order: The order in which the commands are shown in ``/help``
189+
190+
.. versionchanged:: 0.4
191+
192+
Added the ``order`` argument.
188193

189194
.. versionchanged:: 0.3
190195

tests/test_frozenbot.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def more(bot, a, c):
5252

5353
def test_available_commands(bot):
5454
# Create a bunch of dummy commands
55-
@bot.command("test1")
55+
@bot.command("test1", order=10)
5656
def test1():
5757
pass
5858

@@ -64,19 +64,19 @@ def test2():
6464
def test3():
6565
pass
6666

67-
assert {cmd.name for cmd in bot.available_commands()} == {
67+
assert [cmd.name for cmd in bot.available_commands()] == [
6868
"help",
69-
"test1",
7069
"test2",
71-
}
70+
"test1",
71+
]
7272

73-
assert {cmd.name for cmd in bot.available_commands(all=True)} == {
73+
assert [cmd.name for cmd in bot.available_commands(all=True)] == [
7474
"help",
7575
"start",
76-
"test1",
7776
"test2",
7877
"test3",
79-
}
78+
"test1",
79+
]
8080

8181

8282
def test_pickle_frozenbot(frozenbot):

0 commit comments

Comments
 (0)