-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,16 @@ | |
|
||
import textwrap | ||
import collections | ||
import json | ||
|
||
import requests | ||
|
||
from sopel.formatting import bold | ||
from sopel.logger import get_logger | ||
from sopel.module import commands, rule, example, priority | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
@rule('$nick' '(?i)(help|doc) +([A-Za-z]+)(?:\?+)?$') | ||
@example('.help tell') | ||
|
@@ -42,23 +48,55 @@ def help(bot, trigger): | |
if bot.doc[name][1]: | ||
msgfun('e.g. ' + bot.doc[name][1]) | ||
else: | ||
if not trigger.is_privmsg: | ||
bot.reply("I'm sending you a list of my commands in a private message!") | ||
bot.say( | ||
'You can see more info about any of these commands by doing .help ' | ||
'<command> (e.g. .help time)', | ||
trigger.nick | ||
) | ||
bot.say("Hang on, I'm creating a list.") | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dgw
Member
|
||
msgs = [] | ||
|
||
name_length = max(6, max(len(k) for k in bot.command_groups.keys())) | ||
for category, cmds in collections.OrderedDict(sorted(bot.command_groups.items())).items(): | ||
category = category.upper().ljust(name_length) | ||
cmds = ' '.join(cmds) | ||
msg = bold(category) + ' ' + cmds | ||
msg = category + ' ' + cmds | ||
indent = ' ' * (name_length + 2) | ||
msg = textwrap.wrap(msg, subsequent_indent=indent) | ||
for line in msg: | ||
bot.say(line, trigger.nick) | ||
# Honestly not sure why this is a list here | ||
msgs.append('\n'.join(textwrap.wrap(msg, subsequent_indent=indent))) | ||
|
||
url = create_gist(bot, '\n\n'.join(msgs)) | ||
if not url: | ||
return | ||
bot.say("I've posted a list of my commands at {} - You can see " | ||
"more info about any of these commands by doing .help " | ||
"<command> (e.g. .help time)".format(url)) | ||
|
||
|
||
def create_gist(bot, msg): | ||
payload = { | ||
'description': 'Command listing for {}@{}'.format(bot.nick, bot.config.core.host), | ||
'public': 'true', | ||
'files': { | ||
'commands.txt': { | ||
"content": msg, | ||
}, | ||
}, | ||
} | ||
print(json.dumps(payload)) | ||
try: | ||
result = requests.post('https://api.github.com/gists', | ||
data=json.dumps(payload)) | ||
except requests.RequestException: | ||
bot.say("Sorry! Something went wrong.") | ||
logger.exception("Error posting commands gist") | ||
return | ||
if not result.status_code != '201': | ||
bot.say("Sorry! Something went wrong.") | ||
logger.error("Error %s posting commands gist: %s", | ||
result.status_code, result.text) | ||
return | ||
result = result.json() | ||
if 'html_url' not in result: | ||
bot.say("Sorry! Something went wrong.") | ||
logger.error("Invalid result %s", result) | ||
return | ||
return result['html_url'] | ||
|
||
|
||
@rule('$nick' r'(?i)help(?:[?!]+)?$') | ||
|
Since commands don't change often, wouldn't it be better to cache the list link and send it to the user instead of creating a new gist every time? Otherwise the bot will upload a lot of identical gists to github when spammed with the .help command