ctucx.git: ctucxbot

[nimlang] A telegram bot

commit a3bd8fdb8c01254b9acdd4b1c1e8fb67499414e2
parent c0963dc53aa6d43a4af8865072961934a2891e06
Author: ctucx <c@ctu.cx>
Date: Thu, 23 Jan 2020 10:36:42 +0100

now has penisstats
2 files changed, 34 insertions(+), 12 deletions(-)
M
src/cmd/stats.nim
|
38
++++++++++++++++++++++++++++----------
M
src/ctucxbot.nim
|
8
++++++--
diff --git a/src/cmd/stats.nim b/src/cmd/stats.nim
@@ -9,9 +9,12 @@ proc myCmp(x, y: User): int =
   if x.todayMsgs < y.todayMsgs: -1 else: 1
 
 proc statsCommand(bot: Telebot, command: Command) {.async.} =
-  let filePath = getCurrentDir() & "/data/stats/" & $(command.message.chat.id) & ".json"
+  var filePath = getCurrentDir() & "/data/stats/" & $(command.message.chat.id) & ".json"
   var text     = ""
 
+  if command.params == "nsfw":
+    filePath = getCurrentDir() & "/data/stats/penis/" & $(command.message.chat.id) & ".json"
+
   if not fileExists(filePath):
     text = "No statistics yet."
   else:

@@ -23,7 +26,11 @@ proc statsCommand(bot: Telebot, command: Command) {.async.} =
 
     users.sort(myCmp, SortOrder.Descending)
 
-    text  = "Toplist (today/total):\n"
+    if command.params != "nsfw":
+      text  = "Toplist (today/total):\n"
+    else:
+      text  = "Penislist (today/total):\n"
+    
     text &= "======================\n"
      
     var id = 1

@@ -40,10 +47,14 @@ proc statsCommand(bot: Telebot, command: Command) {.async.} =
   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"
+proc statsHandler(message: Message, penis: bool) {.async.} =
+  let user     = message.fromUser.get
+  var filePath = getCurrentDir() & "/data/stats/" & $(message.chat.id) & ".json"
+
+  if penis:
+    discard existsOrCreateDir(getCurrentDir() & "/data/stats/penis")
+
+    filePath = getCurrentDir() & "/data/stats/penis/" & $(message.chat.id) & ".json"
 
   var data = %* {
     "lastReset": getDateStr(now()),

@@ -74,10 +85,15 @@ proc statsHandler(message: Message) {.async.} =
   else:
     data["users"][$(user.id)]["username"] = %user.username
 
-  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)
+  var num = 1
+  if penis:
+    if message.text.isSome:
+      num = message.text.get.normalize.count("penis")
+
+  data["users"][$(user.id)]["todayMessagesCount"] = newJInt(data["users"][$(user.id)]["todayMessagesCount"].getInt()+num)
+  data["users"][$(user.id)]["totalMessagesCount"] = newJInt(data["users"][$(user.id)]["totalMessagesCount"].getInt()+num)
+  data["messagesCount"]["today"] = newJInt(data["messagesCount"]["today"].getInt()+num)
+  data["messagesCount"]["total"] = newJInt(data["messagesCount"]["total"].getInt()+num)
 
   if getDateStr(now()) > data["lastReset"].getStr:
     data["messagesCount"]["today"] = %0

@@ -85,4 +101,6 @@ proc statsHandler(message: Message) {.async.} =
     for id in keys(data["users"]):
       data["users"][id]["todayMessagesCount"] = %0
 
+    data["lastReset"] = %getDateStr(now())
+
   writeFile(filePath, $data)
diff --git a/src/ctucxbot.nim b/src/ctucxbot.nim
@@ -1,4 +1,4 @@
-import asyncdispatch, telebot, options, strutils, random, os, re, unicode, logging
+import asyncdispatch, telebot, options, strutils, random, os, re, unicode
 include cmd/[animalpics, daysuntilcongress, debuginfo, help, stats, unixtime, utc, uptime, yesorno, whoami, invitelinks]
 
 proc ctrlcHandler() {.noconv.} =

@@ -7,7 +7,7 @@ proc ctrlcHandler() {.noconv.} =
 proc updateHandler(bot: Telebot, update: Update) {.async.} =
   if not update.message.isNone:
     var message = update.message.get
-    discard statsHandler(message)
+    discard statsHandler(message, false)
 
     if message.text.isSome:
       let text = message.text.get

@@ -31,12 +31,16 @@ proc updateHandler(bot: Telebot, update: Update) {.async.} =
 
       # if much penis send a pic
       elif penisCount > 4:
+        discard statsHandler(message, true)
+
         var response = newPhoto(message.chat.id, "file://" & getCurrentDir() & "/data/pics/penis.jpg")
         response.replyToMessageId = message.messageId
         discard await bot.send(response)
 
       #if not so much penis send a random lewd message
       elif penisCount > 0:
+        discard statsHandler(message, true)
+
         var response = newMessage(message.chat.id, sample(answers))
         response.replyToMessageId = message.messageId
         discard await bot.send(response)