From: Janneke Nieuwenhuizen Date: Thu, 17 Aug 2023 07:49:22 +0000 (+0200) Subject: demos: irc-bot: Refactor message handling using new handle-message. X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=0d10b58e73e1ef034b2f1b47fdd31b4ff45d3ce3;p=8sync.git demos: irc-bot: Refactor message handling using new handle-message. * demos/ircbot.scm (handle-line): Rename to... (handle-message): ...this, and refactor. * doc/8sync.texi (An intermission on live hacking): Update accordingly. --- diff --git a/demos/ircbot.scm b/demos/ircbot.scm index 32b0d1f..3506111 100755 --- a/demos/ircbot.scm +++ b/demos/ircbot.scm @@ -21,6 +21,7 @@ ;;; License along with 8sync. If not, see . (use-modules (8sync) + (8sync contrib irc) (8sync systems irc) (8sync repl) (oop goops) @@ -30,38 +31,48 @@ (define-class ()) -(define-method (handle-line (irc-bot ) message - speaker channel line emote?) +(define-method (handle-message (irc-bot ) message) (define my-name (irc-bot-username irc-bot)) (define (looks-like-me? str) (or (equal? str my-name) (equal? str (string-concatenate (list my-name ":"))))) - (match (string-split line #\space) - (((? looks-like-me? _) action action-args ...) - (match action - ;; The classic botsnack! - ("botsnack" - (<- (actor-id irc-bot) 'send-line channel - "Yippie! *does a dance!*")) - ;; Return greeting - ((or "hello" "hello!" "hello." "greetings" "greetings." "greetings!" - "hei" "hei." "hei!" "hi" "hi!") - (<- (actor-id irc-bot) 'send-line channel - (format #f "Oh hi ~a!" speaker))) - ;; ---> Add yours here <--- + (match message + ((and ($ ) + (= irc:message-line line) + (= irc:message-command 'PRIVMSG) + (= irc:message-speaker speaker) + (= irc:message-channel channel) + (= irc:message-message message) + (= irc:message-emote? emote?)) - ;; Default + (match (string-split message #\space) + (((? looks-like-me? _) action action-args ...) + (match action + ;; The classic botsnack! + ("botsnack" + (<- (actor-id irc-bot) 'send-line channel + "Yippie! *does a dance!*")) + ;; Return greeting + ((or "hello" "hello!" "hello." "greetings" "greetings." "greetings!" + "hei" "hei." "hei!" "hi" "hi!") + (<- (actor-id irc-bot) 'send-line channel + (format #f "Oh hi ~a!" speaker))) + + ;; ---> Add yours here <--- + + ;; Default + (_ + (<- (actor-id irc-bot) 'send-line channel + "*stupid puppy look*")))) + ;; Otherwise... just spit the output to current-output-port or whatever (_ - (<- (actor-id irc-bot) 'send-line channel - "*stupid puppy look*")))) - ;; Otherwise... just spit the output to current-output-port or whatever - (_ - (if emote? - (format #t "~a emoted ~s in channel ~a\n" - speaker line channel) - (format #t "~a said ~s in channel ~a\n" - speaker line channel))))) + (if emote? + (format #t "~a emoted ~s in channel ~a\n" + speaker message channel) + (format #t "~a said ~s in channel ~a\n" + speaker message channel))))) + (_ #f))) (define (display-help scriptname) diff --git a/doc/8sync.texi b/doc/8sync.texi index 7412ea1..8dea9f3 100644 --- a/doc/8sync.texi +++ b/doc/8sync.texi @@ -1018,8 +1018,7 @@ Edit the respond section to see what channel it's really sending things to: @example -(define-method (handle-line (irc-bot ) message - speaker channel line emote?) +(define-method (handle-message (irc-bot ) message) ;; [... snip ...] (define (respond respond-line) (<- (actor-id irc-bot) 'send-line (pk 'channel channel) @@ -1048,8 +1047,7 @@ to looks like our own username that we respond back to the sender. (We can remove the pk now that we know what's going on.) @example -(define-method (handle-line (irc-bot ) message - speaker channel line emote?) +(define-method (handle-line (irc-bot ) message) ;; [... snip ...] (define (respond respond-line) (<- (actor-id irc-bot) 'send-line