From 6249e0a8c5254e45cf79e3e53824e63e54e18233 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Sat, 18 Oct 2025 10:47:50 +0300 Subject: Make config be a global special variable --- src/main.lisp | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'src/main.lisp') diff --git a/src/main.lisp b/src/main.lisp index 5d3cf76..be17168 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -2,15 +2,16 @@ ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/main (:nicknames :ukkoclot) - (:use :c2cl :iterate :ukkoclot/config :ukkoclot/inline-bots :ukkoclot/tg) + (:use :c2cl :iterate :ukkoclot/inline-bots :ukkoclot/tg) (:import-from :alexandria :when-let) (:import-from :com.dieggsy.f-string :enable-f-strings) + (:import-from :conf) (:import-from :log) (:import-from :serapeum :drop) (:import-from :str) (:import-from :ukkoclot/db :with-db) (:import-from :ukkoclot/serializing :fixup-value) - (:import-from :ukkoclot/state :make-bot :bot-config :bot-power-on) + (:import-from :ukkoclot/state :make-bot :bot-power-on) (:import-from :ukkoclot/strings :escape-xml :is-tg-whitespace :is-tg-whitespace-str) (:local-nicknames (:jzon :com.inuoe.jzon)) @@ -31,25 +32,26 @@ (defun main () (log:config :debug) (unwind-protect - (let ((config (config-load #P"config.default.lisp"))) - (config-merge config #P"config.lisp") - (log:info "Starting up ~A" (config-bot-name config)) - (main-with-config config) + (progn + (conf:print-default #P"config.default.lisp") + (conf:load-config #P"config.lisp") + (log:info "Starting up ~A" (conf:bot-name)) + (main-with-config) nil) (log:info "Quitting!"))) -(defun main-with-config (config) +(defun main-with-config () (unwind-protect - (with-db (db (config-db-path config)) - (let ((bot (make-bot config db))) + (with-db (db (conf:db-path)) + (let ((bot (make-bot db))) ;; TODO: Catch fatal errors & report them - (wrapped-main bot config))) + (wrapped-main bot))) (log:info "We're done!"))) -(defun wrapped-main (bot config) +(defun wrapped-main (bot) (when *in-prod* - (send-message bot :chat-id (config-dev-group config) :text "Initializing...")) - (set-my-name bot :name (config-bot-name config)) + (send-message bot :chat-id (conf:dev-group) :text "Initializing...")) + (set-my-name bot :name (conf:bot-name)) (let ((gup-offset 0)) (loop while (bot-power-on bot) do (let ((updates (get-updates bot :timeout 60 :offset gup-offset))) @@ -65,14 +67,14 @@ (setf gup-offset (1+ (update-update-id update))))))) ;; One last getUpdates to make sure offset is stored on server (get-updates bot :timeout 0 :limit 1 :offset gup-offset)) - (send-message bot :chat-id (config-dev-group config) :text "Shutting down...")) + (send-message bot :chat-id (conf:dev-group) :text "Shutting down...")) (defun on-callback-query (bot cb) (let ((data (callback-query-data cb))) (cond ((and data (str:starts-with-p "bbl:" data :ignore-case nil) (= (user-id (callback-query-from cb)) - (config-owner (bot-config bot)))) + (conf:owner))) (let ((bot-id (read-from-string data t nil :start 4))) (blacklist-inline-bot bot bot-id)) (when-let (msg (callback-query-message cb)) @@ -85,7 +87,7 @@ ((and data (str:starts-with-p "bwl:" data :ignore-case nil) (= (user-id (callback-query-from cb)) - (config-owner (bot-config bot)))) + (conf:owner))) (let ((bot-id (read-from-string data t nil :start 4))) (whitelist-inline-bot bot bot-id)) (when-let (msg (callback-query-message cb)) @@ -269,7 +271,7 @@ ((and (equal simple-cmd "shutdown") (message-from msg) - (= (user-id (message-from msg)) (config-owner (bot-config bot)))) + (= (user-id (message-from msg)) (conf:owner))) (setf (bot-power-on bot) nil) (reply-message bot msg "Initialising shutdown..." :allow-sending-without-reply t))))) @@ -280,6 +282,6 @@ (log:error "While handling ~A: ~A" evt err) (let ((msg #f"{(escape-xml-obj err)} while handling{;~%}
{(escape-xml-obj evt)}
")) (send-message bot - :chat-id (config-dev-group (bot-config bot)) + :chat-id (conf:dev-group) :text msg :parse-mode html))) -- cgit v1.2.3