-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the hidden=True argument on @bot.command to hide commands
Before this commit, the only way to hide existing commands from the /help command was to manage the bot.hide_commands list, but the approach had some pitfalls: it was easy to override previous entries, and there was no way for components to insert commands in it. This new commit deprecates that old behavior, and refactors the whole commands hiding system: now it's possible to pass hidden=True to @bot.command (main component) or self.add_command (custom components) and have the command hidden from the message. Fixes: GH-19
- Loading branch information
Pietro Albini
committed
May 14, 2016
1 parent
b004e16
commit 70d88e9
Showing
14 changed files
with
342 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
botogram.commands | ||
Core logic for commands | ||
Copyright (c) 2016 Pietro Albini <[email protected]> | ||
Released under the MIT license | ||
""" | ||
|
||
|
||
class Command: | ||
"""Representation of a single command""" | ||
|
||
def __init__(self, hook, _bot=None): | ||
# Get some parameters from the hook | ||
self.name = hook._name | ||
self.hidden = hook._hidden | ||
|
||
self._hook = hook | ||
self._component_id = hook.component_id | ||
|
||
self._bot = _bot | ||
|
||
def __reduce__(self): | ||
return rebuild_command, (self._hook,) | ||
|
||
def for_bot(self, bot): | ||
"""Get the command instance for a specific bot""" | ||
return self.__class__(self._hook, _bot=bot) | ||
|
||
@property | ||
def raw_docstring(self): | ||
"""Get the raw docstring of this command""" | ||
func = self._hook.func | ||
|
||
if hasattr(func, "_botogram_help_message"): | ||
if self._bot is not None: | ||
return self._bot._call(func._botogram_help_message, | ||
self._component_id) | ||
else: | ||
return func._botogram_help_message() | ||
elif func.__doc__: | ||
return func.__doc__ | ||
|
||
return | ||
|
||
@property | ||
def docstring(self): | ||
"""Get the docstring of this command""" | ||
docstring = self.raw_docstring | ||
if docstring is None: | ||
return | ||
|
||
result = [] | ||
for line in self.raw_docstring.split("\n"): | ||
# Remove leading whitespaces | ||
line = line.strip() | ||
|
||
# Allow only a single blackline | ||
if line == "" and len(result) and result[-1] == "": | ||
continue | ||
|
||
result.append(line) | ||
|
||
# Remove empty lines at the end or at the start of the docstring | ||
for pos in 0, -1: | ||
if result[pos] == "": | ||
result.pop(pos) | ||
|
||
return "\n".join(result) | ||
|
||
@property | ||
def summary(self): | ||
"""Get a summary of the command""" | ||
docstring = self.docstring | ||
if docstring is None: | ||
return | ||
|
||
return docstring.split("\n", 1)[0] | ||
|
||
|
||
def rebuild_command(hook): | ||
"""Rebuild a Command after being pickled""" | ||
return Command(hook) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.