~williewillus/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 3

[PATCH r16] Add methods to lookup and fetch emotes

Details
Message ID
<20210226174205.604059-1-wenming.yi@gmail.com>
DKIM signature
pass
Download raw message
Patch: +33 -0
From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>

---
 r16.rkt | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/r16.rkt b/r16.rkt
index dd94f89..5cd74a5 100755
--- a/r16.rkt
+++ b/r16.rkt
@@ -7,6 +7,7 @@
  (prefix-in db: "trick-db.rkt")

  (only-in racket/serialize serializable-struct/versions)
  (only-in net/url get-pure-port string->url)
  (only-in "evaluator.rkt" (run ev:run)))

; williewillus#8490, maintainer, & Vazkii#0999, whose server we hope to run this in
@@ -204,6 +205,8 @@
       "-  message-contents => Full text of the invoking command, as a string"
       "-  string-args => Message contents after the bot command, as a string"
       "-  read-args => Thunk that returns message contents after the bot command, as split by the Racket reader, or #f if there was a split failure"
       "-  emote-lookup => Function that takes an emote name and returns its snowflake (or #f if none exists)"
       "-  emote-body => Function that takes an emote snowflake and returns its body (or #f if no such emote exists)"
       "-  delete-caller => Thunk that removes the call or eval command that ran this code"
       "-  parent-context => Mapping of the above symbols for the trick calling this one, or #f if this trick is top-level")
     "\n")
@@ -233,6 +236,30 @@
        (apply values vals))
      (raise (make-exn:fail:contract (~a "Trick " name " doesn't exist!"))))))

(define emote-lookup-cache (make-hash))
(define emote-whitelist-cache (make-hash))
(define emote-data-cache (make-hash))
(define emote-lookup-thread
  (thread
   (thunk
    (let loop ()
      (let ([message (thread-receive)])
        (channel-put (cdr message) (port->bytes (get-pure-port (string->url (~a "https://cdn.discordapp.com/emojis/" (car message) ".png?v=1"))))))
      (loop)))))
(define/contract ((lookup-emote client) id)
  (-> rc:client? (-> string? (or/c bytes? #f)))
  (hash-ref! emote-data-cache id
   (thunk
    (and 
      ; Is this an emote that this bot has encountered?
      (set-member? (hash-ref! emote-whitelist-cache client
                    (thunk
                     (list->set (hash-values (hash-ref emote-lookup-cache client)))))
                   id)
      (let ([ch (make-channel)])
        (thread-send emote-lookup-thread (cons id ch))
        (channel-get ch))))))

(define (evaluation-ctx client message trick-ctx args parent-ctx)
  (let* ([placeholder (make-placeholder #f)]
         [ctx
@@ -243,6 +270,12 @@
                                    (let loop ([data (open-input-string args)])
                                      (let ([val (read data)])
                                        (if (eof-object? val) null (cons val (loop data))))))))
            (emote-lookup     . ,(curry hash-ref
                                  (hash-ref! emote-lookup-cache client
                                   (thunk (for*/hash ([(_ guild) (rc:client-guilds client)]
                                                      [emoji     (rc:guild-emojis guild)])
                                            (values (rc:emoji-name emoji) (rc:emoji-id emoji)))))))
            (emote-body       . ,(lookup-emote client))
            (delete-caller    . ,(thunk (thread-send deleter-thread (cons client message))))
            (make-attachment  . ,make-attachment)
            (call-trick       . ,(call-subtrick client trick-ctx message placeholder))
-- 
2.25.1

[r16/patches/linux_buildtest.yml] build failed

builds.sr.ht
Details
Message ID
<C9JNCJDVEC45.1AM2PDYPKCEQ9@cirno>
In-Reply-To
<20210226174205.604059-1-wenming.yi@gmail.com> (view parent)
DKIM signature
missing
Download raw message
r16/patches/linux_buildtest.yml: FAILED in 49s

[Add methods to lookup and fetch emotes][0] from [][1]

[0]: https://lists.sr.ht/~williewillus/public-inbox/patches/20621
[1]: mailto:wenming.yi@gmail.com

✗ #440902 FAILED r16/patches/linux_buildtest.yml https://builds.sr.ht/~williewillus/job/440902
Details
Message ID
<87mtvouce2.fsf@vincent-lee.net>
In-Reply-To
<20210226174205.604059-1-wenming.yi@gmail.com> (view parent)
DKIM signature
pass
Download raw message
wenming.yi@gmail.com writes:

> From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>
>
> ---
>  r16.rkt | 33 +++++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+)
>
> diff --git a/r16.rkt b/r16.rkt
> index dd94f89..5cd74a5 100755
> --- a/r16.rkt
> +++ b/r16.rkt
> @@ -7,6 +7,7 @@
>    (prefix-in db: "trick-db.rkt")
>  
>    (only-in racket/serialize serializable-struct/versions)
> +  (only-in net/url get-pure-port string->url)
>    (only-in "evaluator.rkt" (run ev:run)))
>  
>  ; williewillus#8490, maintainer, & Vazkii#0999, whose server we hope to run this in
> @@ -204,6 +205,8 @@
>         "-  message-contents => Full text of the invoking command, as a string"
>         "-  string-args => Message contents after the bot command, as a string"
>         "-  read-args => Thunk that returns message contents after the bot command, as split by the Racket reader, or #f if there was a split failure"
> +       "-  emote-lookup => Function that takes an emote name and returns its snowflake (or #f if none exists)"
> +       "-  emote-body => Function that takes an emote snowflake and returns its body (or #f if no such emote exists)"
what does body mean, the raw png data? Clarify here and maybe rename the
variable. Also, how are animated emotes handled?
also, I'd either briefly explain what snowflakes are or just call them "ID's"
>         "-  delete-caller => Thunk that removes the call or eval command that ran this code"
>         "-  parent-context => Mapping of the above symbols for the trick calling this one, or #f if this trick is top-level")
>       "\n")
> @@ -233,6 +236,30 @@
>          (apply values vals))
>        (raise (make-exn:fail:contract (~a "Trick " name " doesn't exist!"))))))
>  
> +(define emote-lookup-cache (make-hash))
> +(define emote-whitelist-cache (make-hash))
> +(define emote-data-cache (make-hash))
reading these names doesn't really give me any idea of what
they're. Maybe more verbose names?
> +(define emote-lookup-thread
> +  (thread
> +   (thunk
> +    (let loop ()
> +      (let ([message (thread-receive)])
> +        (channel-put (cdr message) (port->bytes (get-pure-port (string->url (~a "https://cdn.discordapp.com/emojis/" (car message) ".png?v=1"))))))
> +      (loop)))))
needs error handling

> +(define/contract ((lookup-emote client) id)
> +  (-> rc:client? (-> string? (or/c bytes? #f)))
> +  (hash-ref! emote-data-cache id
> +   (thunk
> +    (and 
> +      ; Is this an emote that this bot has encountered?
> +      (set-member? (hash-ref! emote-whitelist-cache client
> +                    (thunk
> +                     (list->set (hash-values (hash-ref emote-lookup-cache client)))))
this logic is really dense and hard to read and I'm not really sure why this check/the
whitelist cache is necessary?
> +                   id)
> +      (let ([ch (make-channel)])
> +        (thread-send emote-lookup-thread (cons id ch))
> +        (channel-get ch))))))
> +
>  (define (evaluation-ctx client message trick-ctx args parent-ctx)
>    (let* ([placeholder (make-placeholder #f)]
>           [ctx
> @@ -243,6 +270,12 @@
>                                      (let loop ([data (open-input-string args)])
>                                        (let ([val (read data)])
>                                          (if (eof-object? val) null (cons val (loop data))))))))
> +            (emote-lookup     . ,(curry hash-ref
> +                                  (hash-ref! emote-lookup-cache client
> +                                   (thunk (for*/hash ([(_ guild) (rc:client-guilds client)]
> +                                                      [emoji     (rc:guild-emojis guild)])
> +                                            (values (rc:emoji-name emoji) (rc:emoji-id emoji)))))))
> +            (emote-body       . ,(lookup-emote client))
>              (delete-caller    . ,(thunk (thread-send deleter-thread (cons client message))))
>              (make-attachment  . ,make-attachment)
>              (call-trick       . ,(call-subtrick client trick-ctx message placeholder))

[PATCH] Patch stuff

Details
Message ID
<20210228041406.695524-1-wenming.yi@gmail.com>
In-Reply-To
<20210226174205.604059-1-wenming.yi@gmail.com> (view parent)
DKIM signature
pass
Download raw message
Patch: +22 -7
From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>

---

> Also, how are animated emotes handled?
Unhandled right now due to a racketcord limitation.

> I'm not really sure why this check/the whitelist cache is necessary?
Not necessary, but it saves on net accesses in case some asshole decides
to go all `(for ([i in-naturals]) (emote-image (~a i)))`.

For everything else, see below:

 r16.rkt | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/r16.rkt b/r16.rkt
index bb7f9d6..92427fe 100755
--- a/r16.rkt
+++ b/r16.rkt
@@ -211,8 +211,8 @@
       "-  message-contents => Full text of the invoking command, as a string"
       "-  string-args => Message contents after the bot command, as a string"
       "-  read-args => Thunk that returns message contents after the bot command, as split by the Racket reader, or #f if there was a split failure"
       "-  emote-lookup => Function that takes an emote name and returns its snowflake (or #f if none exists)"
       "-  emote-body => Function that takes an emote snowflake and returns its body (or #f if no such emote exists)"
       "-  emote-lookup => Function that takes an emote name and returns its ID (or #f if none exists)"
       "-  emote-image => Function that takes an emote snowflake and returns its PNG data (or #f if no such emote exists)"
       "-  delete-caller => Thunk that removes the call or eval command that ran this code"
       "-  parent-context => Mapping of the above symbols for the trick calling this one, or #f if this trick is top-level")
     "\n")
@@ -242,15 +242,24 @@
        (apply values vals))
      (raise (make-exn:fail:contract (~a "Trick " name " doesn't exist!"))))))

; Cache for (emote-lookup string)
(define emote-lookup-cache (make-hash))
; Cache for checking whether an emote is valid
(define emote-whitelist-cache (make-hash))
; Cache for (emote-image id)
(define emote-data-cache (make-hash))
(define emote-lookup-thread
  (thread
   (thunk
    (let loop ()
      (let ([message (thread-receive)])
        (channel-put (cdr message) (port->bytes (get-pure-port (string->url (~a "https://cdn.discordapp.com/emojis/" (car message) ".png?v=1"))))))
        ; TODO this only uses PNG, racket-cord needs to expose an animated field on emoji
        (channel-put (cdr message)
                     (with-handlers ([exn:fail? (const #f)])
                       (~> (~a "https://cdn.discordapp.com/emojis/" (car message) ".png?v=1")
                           string->url
                           get-pure-port
                           port->bytes))))
      (loop)))))
(define/contract ((lookup-emote client) id)
  (-> rc:client? (-> string? (or/c bytes? #f)))
@@ -258,13 +267,19 @@
   (thunk
    (and 
      ; Is this an emote that this bot has encountered?
      ; If not, don't bother requesting it and just return #f
      (set-member? (hash-ref! emote-whitelist-cache client
                    (thunk
                     (list->set (hash-values (hash-ref emote-lookup-cache client)))))
                     ; COFU a set of all emotes in the lookup table
                     (thunk (~> emote-lookup-cache
                              (hash-ref client)
                              hash-values
                              list->set))
                   id)
      (let ([ch (make-channel)])
        (thread-send emote-lookup-thread (cons id ch))
        (channel-get ch))))))
        (let ([data (channel-get ch)])
          ; If empty byte string returned, return #f
          (and data (positive? (bytes-length data)) data)))))))

(define (evaluation-ctx client message trick-ctx args parent-ctx)
  (let* ([placeholder (make-placeholder #f)]
@@ -281,7 +296,7 @@
                                   (thunk (for*/hash ([(_ guild) (rc:client-guilds client)]
                                                      [emoji     (rc:guild-emojis guild)])
                                            (values (rc:emoji-name emoji) (rc:emoji-id emoji)))))))
            (emote-body       . ,(lookup-emote client))
            (emote-image      . ,(lookup-emote client))
            (delete-caller    . ,(thunk (thread-send deleter-thread (cons client message))))
            (make-attachment  . ,make-attachment)
            (call-trick       . ,(call-subtrick client trick-ctx message placeholder))
-- 
2.25.1
Reply to thread Export thread (mbox)