summaryrefslogtreecommitdiff
path: root/src/main.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.lisp')
-rw-r--r--src/main.lisp60
1 files changed, 31 insertions, 29 deletions
diff --git a/src/main.lisp b/src/main.lisp
index cd9e755..94148a7 100644
--- a/src/main.lisp
+++ b/src/main.lisp
@@ -2,8 +2,8 @@
2;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> 2;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com>
3(defpackage :ukkoclot/main 3(defpackage :ukkoclot/main
4 (:nicknames :ukkoclot) 4 (:nicknames :ukkoclot)
5 (:use :c2cl :ukkoclot/config :ukkoclot/inline-bots :ukkoclot/tg) 5 (:use :c2cl :iterate :ukkoclot/config :ukkoclot/inline-bots :ukkoclot/tg)
6 (:import-from :anaphora :acond :awhen :it) 6 (:import-from :alexandria :when-let)
7 (:import-from :com.dieggsy.f-string :enable-f-strings) 7 (:import-from :com.dieggsy.f-string :enable-f-strings)
8 (:import-from :log) 8 (:import-from :log)
9 (:import-from :ukkoclot/db :with-db) 9 (:import-from :ukkoclot/db :with-db)
@@ -55,12 +55,12 @@
55 (loop for update across updates do 55 (loop for update across updates do
56 (unwind-protect 56 (unwind-protect
57 (progn 57 (progn
58 (awhen (update-message update) 58 (when-let (msg (update-message update))
59 (reporty (it) 59 (reporty (msg)
60 (on-message bot it))) 60 (on-message bot msg)))
61 (awhen (update-callback-query update) 61 (when-let (cbq (update-callback-query update))
62 (reporty (it) 62 (reporty (cbq)
63 (on-callback-query bot it)))) 63 (on-callback-query bot cbq))))
64 (setf gup-offset (1+ (update-update-id update))))))) 64 (setf gup-offset (1+ (update-update-id update)))))))
65 ;; One last getUpdates to make sure offset is stored on server 65 ;; One last getUpdates to make sure offset is stored on server
66 (get-updates bot :timeout 0 :limit 1 :offset gup-offset)) 66 (get-updates bot :timeout 0 :limit 1 :offset gup-offset))
@@ -74,10 +74,10 @@
74 (config-owner (bot-config bot)))) 74 (config-owner (bot-config bot))))
75 (let ((bot-id (read-from-string data t nil :start 4))) 75 (let ((bot-id (read-from-string data t nil :start 4)))
76 (blacklist-inline-bot bot bot-id)) 76 (blacklist-inline-bot bot bot-id))
77 (awhen (callback-query-message cb) 77 (when-let (msg (callback-query-message cb))
78 (delete-message bot 78 (delete-message bot
79 :chat-id (message-chat-id it) 79 :chat-id (message-chat-id msg)
80 :message-id (message-id it))) 80 :message-id (message-id msg)))
81 (answer-callback-query bot 81 (answer-callback-query bot
82 :callback-query-id (callback-query-id cb) 82 :callback-query-id (callback-query-id cb)
83 :text "OK")) 83 :text "OK"))
@@ -87,10 +87,10 @@
87 (config-owner (bot-config bot)))) 87 (config-owner (bot-config bot))))
88 (let ((bot-id (read-from-string data t nil :start 4))) 88 (let ((bot-id (read-from-string data t nil :start 4)))
89 (whitelist-inline-bot bot bot-id)) 89 (whitelist-inline-bot bot bot-id))
90 (awhen (callback-query-message cb) 90 (when-let (msg (callback-query-message cb))
91 (delete-message bot 91 (delete-message bot
92 :chat-id (message-chat-id it) 92 :chat-id (message-chat-id msg)
93 :message-id (message-id it))) 93 :message-id (message-id msg)))
94 (answer-callback-query bot 94 (answer-callback-query bot
95 :callback-query-id (callback-query-id cb) 95 :callback-query-id (callback-query-id cb)
96 :text "OK")) 96 :text "OK"))
@@ -104,15 +104,16 @@
104 104
105(defun on-message (bot msg) 105(defun on-message (bot msg)
106 (block nil 106 (block nil
107 (awhen (message-via-bot msg) 107 (when-let (inline-bot (message-via-bot msg))
108 (unless (on-inline-bot bot msg it) 108 (unless (on-inline-bot bot msg inline-bot)
109 (return))) 109 (return)))
110 110
111 (awhen (message-text msg) 111 (when-let (text (message-text msg))
112 (on-text-message bot msg it)) 112 (on-text-message bot msg text))
113 113
114 (awhen (message-new-chat-members msg) 114 (when-let (new-chat-members (message-new-chat-members msg))
115 (loop for new-chat-member across it do 115 (iter
116 (for new-chat-member in-vector new-chat-members)
116 (on-new-member bot msg new-chat-member))))) 117 (on-new-member bot msg new-chat-member)))))
117 118
118(defun on-new-member (bot msg new-member) 119(defun on-new-member (bot msg new-member)
@@ -145,11 +146,12 @@
145 ;; warn gets removed after a month of no warns 146 ;; warn gets removed after a month of no warns
146 (return)) 147 (return))
147 148
148 (awhen (message-entities msg) 149 (when-let (entities (message-entities msg))
149 (loop for entity across it 150 (iter
150 when (and (equal (message-entity-type entity) bot-command) 151 (for entity in-vector entities)
151 (= (message-entity-offset entity) 0)) 152 (when (and (eql (message-entity-type entity) bot-command)
152 do (on-text-command bot msg text (message-entity-extract entity text)))) 153 (zerop (message-entity-offset entity)))
154 (on-text-command bot msg text (message-entity-extract entity text)))))
153 155
154 ;; TODO: Replace this cond with a nicer dispatch. Something like string-case? 156 ;; TODO: Replace this cond with a nicer dispatch. Something like string-case?
155 (cond ((equal text ":3") 157 (cond ((equal text ":3")
@@ -241,15 +243,15 @@
241 (let ((simple-cmd (simplify-cmd bot cmd))) 243 (let ((simple-cmd (simplify-cmd bot cmd)))
242 (log:debug "text-command: ~A AKA ~A" cmd simple-cmd) 244 (log:debug "text-command: ~A AKA ~A" cmd simple-cmd)
243 ;; TODO: Replace this cond with a nicer dispatch. Something like string-case? 245 ;; TODO: Replace this cond with a nicer dispatch. Something like string-case?
244 (acond 246 (cond
245 ((equal simple-cmd "chatid") 247 ((equal simple-cmd "chatid")
246 (reply-message bot msg 248 (reply-message bot msg
247 #f"<code>{(message-chat-id msg)}</code>" 249 #f"<code>{(message-chat-id msg)}</code>"
248 :parse-mode html)) 250 :parse-mode html))
249 251
250 ((and (equal simple-cmd "msginfo") 252 ((equal simple-cmd "msginfo")
251 (message-reply-to-message msg)) 253 (when-let (replied (message-reply-to-message msg))
252 (reply-message bot it (let ((*print-pretty* t)) (fixup-value it)))) 254 (reply-message bot replied (let ((*print-pretty* t)) (fixup-value replied)))))
253 255
254 ((equal simple-cmd "ping") 256 ((equal simple-cmd "ping")
255 (let* ((start-time (get-internal-real-time)) 257 (let* ((start-time (get-internal-real-time))