Skip to content

Commit

Permalink
Add help for module this
Browse files Browse the repository at this point in the history
  • Loading branch information
nonamenix committed Feb 23, 2019
1 parent f3418b3 commit dc3322a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 0.5.8

* Add help for this module


### 0.5.7

* Update python version
Expand Down
43 changes: 26 additions & 17 deletions bot/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
_(Inspired by "The Zen of Python, by Tim Peters")_
"""


rule_not_found = """
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name '{}'
"""


rules = [
[
["hi", "hello", "intro"], # from this import hi
Expand All @@ -34,24 +36,31 @@
"Unless it is started by a link to Habrahabr.",
],
],
[[], ["Politeness counts."]], # from this import politeness
[
["politeness"], # from this import politeness
["Politeness counts."]
],
[
["bad_mood"], # from this import bad_mood
["Bad mood is not a good reason to break the rules."]
],
[
["ask"], # from this import ask
["Don't ask to ask just ask."]
],
[
["text", "voice"], # from this import voice
["Text message is better than voice message.", "Unless it is voice conference."]
[], # from this import bad_mood
["Bad mood is not a good reason to break the rules."],
],
[["ask"], ["Don't ask to ask just ask."]], # from this import ask
[
[],
["Git repos are one honking great idea — let's do more of those!"]
[], # from this import voice
[
"Text message is better than voice message.",
"Unless it is voice conference.",
],
],
]
[[], ["Git repos are one honking great idea — let's do more of those!"]],
]

help_test = """
Help on module this:
*NAME*
this
*EXAMPLES*
```
{examples}
```
"""
32 changes: 24 additions & 8 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ def make_zen_md(rules, wrap=False):
rules.insert(0, "...")
rules.append("...")

return "\n".join([
content.chat_rules_header,
*rules
])
return "\n".join([content.chat_rules_header, *rules])


def get_moderators():
Expand Down Expand Up @@ -101,6 +98,23 @@ async def hello(chat: Chat, message):
await chat.reply("Hello world")


@bot.command("/?help\(this\)")
async def rule_help(chat: Chat, message):
body = []
for keys, rules in content.rules:
if len(keys) > 0:
row = ">>> from this import {}".format(keys[0])
if len(keys) > 1:
row += " # {}".format(", ".join(keys[1:]))
body.append(row)
body.extend(rules)
body.append("")

await chat.reply(
content.help_test.format(examples="\n".join(body)), parse_mode="Markdown"
)


@bot.command("/?from this import (?P<key>.+)")
async def rule_of_zen(chat: Chat, matched):
key = matched.group("key")
Expand All @@ -112,15 +126,17 @@ async def rule_of_zen(chat: Chat, matched):
await chat.reply(content.rule_not_found.format(key), parse_mode="Markdown")


@bot.command("/rules")
@bot.command("/?rules")
@bot.command("/?zen")
@bot.command("/?import this")
@bot.command("/?import __this__")
@bot.command("\`import __this__\`")
@no_more_than_once_every(interval=timedelta(minutes=5), key="zen_of_chat")
async def zen(chat: Chat, message):
# TODO: fetch it from chat_zen_url = "https://raw.githubusercontent.com/spbpython/orgs-wiki/master/chat/this.md"

await chat.reply(make_zen_md(flatten([rules for _, rules in content.rules])), parse_mode="Markdown")
await chat.reply(
make_zen_md(flatten([rules for _, rules in content.rules])),
parse_mode="Markdown",
)


def reply_with_let_me_search_for_you(chat: Chat, search_url: str, query: str):
Expand Down

0 comments on commit dc3322a

Please sign in to comment.