ctucx.git: ctucxbot

[nimlang] A telegram bot

commit 8b9e8db64ea944a4cf5a49cb5496b63a43d09f2a
parent 53ee52d5b3d82c6d97dce4ad24505790fdc50c56
Author: Sebastian Walz <sivizius@ohai.su>
Date: Wed, 15 Jan 2020 19:34:23 +0100

/help
2 files changed, 43 insertions(+), 2 deletions(-)
A
src/cmd/help.nim
|
40
++++++++++++++++++++++++++++++++++++++++
M
src/ctucxbot.nim
|
5
+++--
diff --git a/src/cmd/help.nim b/src/cmd/help.nim
@@ -0,0 +1,40 @@
+import asyncdispatch, strformat, telebot
+
+const COMMANDS = [
+  # command               arguments     description
+  [ "daysuntilcongress",  "",           "prints the number of days till congress."      ],
+  [ "help",               "[[command]]",  "prints the description of the given command."  ],
+  [ "stats",              "",           "print some stats…dunno?"                       ],
+  [ "uptime",             "",           "prints the uptime of this bot."                ],
+  [ "yesorno",            "",           "helps with decisions."                         ],
+]
+
+proc helpCommand(bot: Telebot, command: Command) {.async.} =
+  var answer                            =   ""
+  if command.params.len() > 0:
+    let command                         =   command.params
+    answer                              =   fmt"Could not find Command ›/{command}‹"
+    for index                           in  0..( COMMANDS.len()  - 1 ):
+      let entry                         =   COMMANDS[ index ]
+      if entry[ 0 ] == command:
+        let arguments                   =   entry[ 1 ]
+        let description                 =   entry[ 2 ]
+        answer                          =   fmt"/{command} {arguments}{'\n'}  {description}"
+        break
+  else:
+    answer                              =   "This Bot reacts to messages…\n"
+    answer                              &=  "…starting with ›/‹ (see below),\n"
+    answer                              &=  "…containing ›penis‹,\n"
+    answer                              &=  "…containing ›/s‹ somewhere.\n"
+    answer                              &=  "Available Commands:"
+    for index                           in  0..( COMMANDS.len()  - 1 ):
+      let entry                         =   COMMANDS[ index ]
+      let command                       =   entry[ 0 ]
+      let arguments                     =   entry[ 1 ]
+      let description                   =   entry[ 2 ]
+      answer                            &=  fmt"{'\n'}/{command} {arguments} – {description}"
+
+  var message                           =   newMessage(command.message.chat.id, answer)
+  message.replyToMessageId              =   command.message.messageId
+  message.parseMode                     =   "markdown"
+  discard bot.send(message)
diff --git a/src/ctucxbot.nim b/src/ctucxbot.nim
@@ -1,5 +1,5 @@
 import asyncdispatch, telebot, options, strutils, random, os, re, unicode
-include cmd/[daysuntilcongress, stats, uptime, yesorno]
+include cmd/[daysuntilcongress, help, stats, uptime, yesorno]
 
 const API_KEY = slurp("secret.key")
 

@@ -17,7 +17,7 @@ proc updateHandler(bot: Telebot, update: Update) {.async.} =
         "Gihihihi, Pimmel!",
         "Höhö, Döödel!",
         "Glied",
-        "8===D"
+        "8===D",
       ]
 
       let penisCount = text.normalize.count("penis")

@@ -52,6 +52,7 @@ let bot = newTeleBot(API_KEY)
 
 bot.onUpdate(updateHandler)
 bot.onCommand("daysuntilcongress", daysUntilCongressCmd)
+bot.onCommand("help", helpCommand)
 bot.onCommand("stats", statsCmd)
 bot.onCommand("uptime", uptimeCmd)
 bot.onCommand("yesorno", yesornoCmd)