Skip to content

Commit

Permalink
Create a gist with the command list
Browse files Browse the repository at this point in the history
  • Loading branch information
embolalia committed Oct 15, 2016
1 parent 9d13dc9 commit fa89eb7
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions sopel/modules/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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.")
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(?:[?!]+)?$')
Expand Down

0 comments on commit fa89eb7

Please sign in to comment.