[PATCH r16] Implement IRC-bot message integration
Export this patch
From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>
This patch allow IRCs users (hello #tech_chat!) to use r16.
Garnish the config with:
> "irc_bridge_bots": ["992244650243014696"],
and enjoy!
---
frontends/discord.rkt | 25 ++++++++++++++++++++ -----
1 file changed, 20 insertions(+), 5 deletions(-)
diff --git a/frontends/discord.rkt b/frontends/discord.rkt
index 4c1df54..7e635d2 100644
--- a/frontends/discord.rkt
+++ b/frontends/discord.rkt
@@ -87,6 +87,7 @@
(init-field bot-prefix)
(init-field trick-prefix)
(init-field delete-time-sec)
+ (init-field irc-bridge-bots)
(define with-typing-indicator ;; (_ proc)
(let ()
@@ -205,10 +206,22 @@
(when (> purged 0)
(log-r16-debug "Purged ~a recent messages" purged)))))
+ (define (strip-irc-header contents)
+ (define match (regexp-match-positions #px"^<[][\\\\`_^{|}a-zA-Z0-9]{1,9}>\\s+" contents))
Explain the regex here? why the extra [] at the front and also {1,9} seems
kinda arbitrary? why not + for the character set.
+ (and match (substring contents (cdar match))))
+
+ (define (get-message-contents message)
+ (define base-content (string-trim (hash-ref message 'content)))
+ (cond
+ [(set-member? irc-bridge-bots (message-author-id message))
+ (strip-irc-header base-content)]
+ [(message-from-bot? message) #f]
+ [else base-content]))
+
(define/public (get-enrich-context)
(define deleted-box (current-deleted-box))
(define message (current-message))
- (define message-contents (hash-ref message 'content))
+ (define message-contents (get-message-contents message))
(define message-attachments (or (hash-ref message 'attachments #f) null))
(define reply-message-attachments (or (and~> message (hash-ref 'referenced_message #f) (hash-ref 'attachments #f)) null))
(define message-author (message-author-id message))
@@ -394,11 +407,11 @@
[current-message message]
[current-deleted-box (box #f)]
[current-context-id (context-id message)])
- (define content (string-trim (hash-ref message 'content)))
- (define channel (hash-ref message 'channel_id))
- (unless (message-from-bot? message)
+ (define content (get-message-contents message))
+ (when content
(match-define (cons func func-args) (parse-command content))
(when func
+ (define channel (hash-ref message 'channel_id))
(define contents
(with-handlers
([exn?
@@ -675,6 +688,7 @@
(define bot-prefix (hash-ref config 'bot_prefix "!rkt "))
(define trick-prefix (hash-ref config 'trick_prefix "!!"))
(define delete-time-sec (hash-ref config 'delete_time 300))
+ (define irc-bridge-bots (list->set (hash-ref config 'irc_bridge_bots '())))
(define client
(rc:make-client
token
@@ -684,4 +698,5 @@
[client client]
[bot-prefix bot-prefix]
[trick-prefix trick-prefix]
- [delete-time-sec delete-time-sec]))
+ [delete-time-sec delete-time-sec]
+ [irc-bridge-bots irc-bridge-bots]))
--
2.34.1
r16/patches/linux_buildtest.yml: SUCCESS in 50s
[Implement IRC-bot message integration][0] from [][1]
[0]: https://lists.sr.ht/~williewillus/public-inbox/patches/54488
[1]: mailto:wenming.yi@gmail.com
✓ #1304794 SUCCESS r16/patches/linux_buildtest.yml https://builds.sr.ht/~williewillus/job/1304794
See inline for a question.
Maybe to generalize, you might want to name this "allowed_bots" and let
the prefix-stripping regex to be customizable.
Also, please update the scribble docs.
wenming.yi@gmail.com writes: