Skip to content

Commit

Permalink
Merge pull request #106 from LanceMaverick/make-paginated-help
Browse files Browse the repository at this point in the history
Make paginated help
  • Loading branch information
LanceMaverick committed Feb 24, 2017
2 parents 6a3c258 + 3231022 commit 12e4473
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
62 changes: 53 additions & 9 deletions skybeard/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# from skybeard.beards import BeardAsyncChatHandlerMixin
from skybeard.beards import BeardChatHandler, Beard, SlashCommand
from skybeard.mixins import PaginatorMixin
from skybeard.utils import embolden, italisize

import config
Expand All @@ -27,21 +28,44 @@ async def fetch_user_help():
return retdict


async def format_user_help(userhelps):
async def format_user_help(name, userhelp):
"""Takes a dict of user help messages and formats them."""

retstr = ""
if userhelp:
retstr += "{name}:\n{userhelp}\n\n".format(name=embolden(name), userhelp=userhelp)
else:
retstr += "{name}:\nNo documentation found.\n\n".format(name=embolden(name))

return retstr


async def format_user_helps(userhelps):
"""Takes a dict of user help messages and formats them."""

retstr = italisize("List of beard documentation:\n\n")
for name, userhelp in sorted(userhelps.items(), key=lambda x: x[0]):
if userhelp:
retstr += "{name}:\n{userhelp}\n\n".format(name=embolden(name), userhelp=userhelp)
else:
retstr += "{name}:\nNo documentation found.\n\n".format(name=embolden(name))
retstr += await format_user_help(name, userhelp)

retstr += italisize("End of beard documentation.")

return retstr


async def format_user_helps_paginated(userhelps):
"""Takes a dict of user help messages and formats them."""

retlist = []
for name, userhelp in sorted(userhelps.items(), key=lambda x: x[0]):
retstr = italisize("Beard documentation:\n\n")
retstr += await format_user_help(name, userhelp)
# retstr += italisize("End of beard documentation.")

retlist.append(retstr)

return retlist


def get_all_cmd_helps():
"""Retrieves the help messages in the tuples of each plug-in's
__commands__ list"""
Expand All @@ -65,11 +89,30 @@ async def send_help(self, msg):
retstr += ("My help message is unconfigured. To display "
"something here, add a docstring to my config.py.")

userhelp = await fetch_user_help()
userhelp = await format_user_help(userhelp)
retstr += "\n\n{}".format(userhelp)
userhelps = await fetch_user_help()
userhelps = await format_user_helps(userhelps)
retstr += "\n\n{}".format(userhelps)
await self.sender.sendMessage(retstr, parse_mode='html')

async def send_paginated_help(self, msg):
"""sends the user a combined help message for all plug-ins"""
retstr = ""
try:
retstr += config.__userhelp__
except AttributeError:
retstr += ("My help message is unconfigured. To display "
"something here, add a docstring to my config.py.")

userhelps = await fetch_user_help()
userhelps = await format_user_helps_paginated(userhelps)

async def identity(x):
return x

await self.send_paginated_message(userhelps, identity)
# retstr += "\n\n{}".format(userhelps)
# await self.sender.sendMessage(retstr, parse_mode='html')

async def cmd_helps(self, msg):
"""sends the user a formatted list of commands for easy registering with
botfather"""
Expand All @@ -81,11 +124,12 @@ async def cmd_helps(self, msg):

def create_help(config):

class BeardedHelp(Help, BeardChatHandler):
class BeardedHelp(Help, PaginatorMixin, BeardChatHandler):
"""Beard for interfacing help functionality with telegram"""
_timeout = 2
__commands__ = [
('help', 'send_help', "Shows verbose help message."),
('helppaged', 'send_paginated_help', "Shows verbose help message, paginated."),
('cmdhelps', 'cmd_helps', "Lists all commands available."),
]

Expand Down
2 changes: 1 addition & 1 deletion skybeard/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def command_predicate(cmd):
"""Returns a predicate coroutine which returns True if command is sent."""
async def retcoro(beard_chat_handler, msg):
bot_username = await beard_chat_handler.get_username()
pattern = r"^/{}(?:@{}|[^@]|$)".format(
pattern = r"^/{}(?:@{}| |$)".format(
cmd,
bot_username,
)
Expand Down

0 comments on commit 12e4473

Please sign in to comment.