ctucx.git: ctucxbot

[nimlang] A telegram bot

commit f51601b0655f3d1b92a20f96c4837b620c3cfa5c
parent ba7d3b64b417d13ffe38b2629f32df32836ce847
Author: ctucx <c@ctu.cx>
Date: Wed, 15 Jan 2020 15:16:45 +0100

new command: /stats
5 files changed, 90 insertions(+), 18 deletions(-)
M
.gitignore
|
2
++
A
data/stats/empty
|
0
A
foo
|
2
++
A
src/cmd/stats.nim
|
57
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M
src/ctucxbot.nim
|
47
+++++++++++++++++++++++++++++------------------
diff --git a/.gitignore b/.gitignore
@@ -1,2 +1,3 @@
 ctucxbot
 src/secret.key
+data/stats/*.json+
\ No newline at end of file
diff --git a/data/stats/empty b/data/stats/empty
diff --git a/foo b/foo
@@ -0,0 +1 @@
+lol+
\ No newline at end of file
diff --git a/src/cmd/stats.nim b/src/cmd/stats.nim
@@ -0,0 +1,57 @@
+import asyncdispatch, telebot, strutils, options, os, json
+
+proc statsCmd(bot: Telebot, command: Command) {.async.} =
+  let filePath = getCurrentDir() & "/data/stats/" & $(command.message.chat.id) & ".json"
+  var text     = ""
+
+  if not fileExists(filePath):
+    text = "No statistics yet."
+  else:
+    var data = parseFile(filePath)
+    text  = "Toplist (today/total):\n"
+    text &= "======================\n"
+     
+    var id = 1
+    var users = data["users"]
+    for key, value in mpairs(users):
+      text &= $(id) & ": " & $(value["username"].getStr()) & " " & $(value["todayMessagesCount"]) & "/" & $(value["totalMessagesCount"]) & "\n"
+      inc(id)
+
+    text &= "\n"
+    text &= "today sent: " & $(data["messagesCount"]["today"]) & "\n"
+    text &= "total sent: " & $(data["messagesCount"]["total"]) & "\n"
+
+
+  var message = newMessage(command.message.chat.id, text)
+  message.replyToMessageId = command.message.messageId
+  discard bot.send(message)
+
+proc statsHandler(message: Message) {.async.} =
+  let user = message.fromUser.get
+  
+  let filePath = getCurrentDir() & "/data/stats/" & $(message.chat.id) & ".json"
+
+  var data = %* {
+    "messagesCount":{
+      "today": 0,
+      "total": 0
+    },
+    "users": {}
+  }
+
+  if fileExists(filePath):
+    data = parseFile(filePath)
+
+  if not data["users"].hasKey($(user.id)):
+    data["users"].add($(user.id), %* {
+      "username": user.username,
+      "todayMessagesCount": 0,
+      "totalMessagesCount": 0
+    })
+
+  data["users"][$(user.id)]["todayMessagesCount"] = newJInt(data["users"][$(user.id)]["todayMessagesCount"].getInt()+1)
+  data["users"][$(user.id)]["totalMessagesCount"] = newJInt(data["users"][$(user.id)]["totalMessagesCount"].getInt()+1)
+  data["messagesCount"]["today"] = newJInt(data["messagesCount"]["today"].getInt()+1)
+  data["messagesCount"]["total"] = newJInt(data["messagesCount"]["total"].getInt()+1)
+
+  writeFile(filePath, $data)
diff --git a/src/ctucxbot.nim b/src/ctucxbot.nim
@@ -1,13 +1,15 @@
-import asyncdispatch, telebot, options, strutils, random, os
-include cmd/[uptime, yesorno]
+import asyncdispatch, telebot, options, strutils, random, os, re, unicode
+include cmd/[uptime, yesorno, stats]
 
 const API_KEY = slurp("secret.key")
 
 proc updateHandler(bot: Telebot, update: Update) {.async.} =
   if not update.message.isNone:
-    var response = update.message.get
-    if response.text.isSome:
-      var text = response.text.get
+    var message = update.message.get
+    discard statsHandler(message)
+
+    if message.text.isSome:
+      var text = message.text.get
 
       var answers = [
         "Höhö! Penis!", 

@@ -18,31 +20,40 @@ proc updateHandler(bot: Telebot, update: Update) {.async.} =
         "8===D" 
       ]
 
+      #react to sarcasm to make it more obvious 
+      if text.contains(re"\/s"):
+        var response = newSticker(message.chat.id, "file://" & getCurrentDir() & "/data/pics/sarcasm_sign.webp")
+        response.replyToMessageId = message.messageId
+        discard await bot.send(response)
+
+
       let penisCount = text.normalize.count("penis")
-      
+
       if penisCount > 0:
         if penisCount > 4:
-          var message = newPhoto(response.chat.id, "file://" & getCurrentDir() & "/data/pics/penis.jpg")
-
-          message.replyToMessageId = response.messageId
-          discard await bot.send(message)
+          var response = newPhoto(message.chat.id, "file://" & getCurrentDir() & "/data/pics/penis.jpg")
+          response.replyToMessageId = message.messageId
+          discard await bot.send(response)
 
         else:
-          var message = newMessage(response.chat.id, sample(answers))
+          var response = newMessage(message.chat.id, sample(answers))
+          response.replyToMessageId = message.messageId
+          discard await bot.send(response)
 
-          message.replyToMessageId = response.messageId
-          discard await bot.send(message)
+      if text.normalize.contains("sinep"):
+        var response = newMessage(message.chat.id, reversed(sample(answers)))
+        response.replyToMessageId = message.messageId
+        discard await bot.send(response)        
 
       if text == "egg_irl":
-        var message = newPhoto(response.chat.id, "file://" & getCurrentDir() & "/data/pics/egg.jpg")
-
-        message.replyToMessageId = response.messageId
-        discard await bot.send(message)
-       
+        var response = newPhoto(message.chat.id, "file://" & getCurrentDir() & "/data/pics/egg.jpg")
+        response.replyToMessageId = message.messageId
+        discard await bot.send(response)
 
 let bot = newTeleBot(API_KEY)
 
 bot.onUpdate(updateHandler)
 bot.onCommand("uptime", uptimeCmd)
 bot.onCommand("yesorno", yesornoCmd)
+bot.onCommand("stats", statsCmd)
 bot.poll(timeout=300) 
\ No newline at end of file