!LOwFQWBsILWVcszfWb:matrix.org

ClojureScript

196 Members
ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.9.22923 Servers

Load older messages


SenderMessageTime
23 Aug 2022
@marv:marv.sexy@marv:marv.sexy *

Hey guys, porting some discord bot from js to clojurescript right now and I think I am doing something wrong "style"-wise, my code feels like it could be improved

Trying to handle commands right now, to make it easier, there only is one command at the moment, which can have multiple subcommands.
I want to parse the subcommand and run the proper handler for it
currently doing that like this:

(defn- handle-subcommands [^Discord/Interaction interaction]
  (case (get-subcommand-name interaction)
    "rename" (handle-rename interaction)
    (print-unknown-command-error interaction)))

(defn- handle-chat-input-command [^Discord/Interaction interaction]
  (if (= "channel" (.-commandName interaction))
    (handle-subcommands interaction)
    (print-unknown-command-error interaction)))

(defn handle-interaction-create+ [^Discord/Interaction interaction]
  (cond (.isChatInputCommand interaction)
        (handle-chat-input-command interaction)))

This totally works, but I don't like finishing every line with interaction. I have been thinking about using partial on the handler functions but do have the feeling that it still would get cluttered a lot
can someone give me a hint into the correct direction here? :)

18:08:30
@savodj:matrix.org@savodj:matrix.org left the room.18:08:54
@kakuhen:kotoura.moekakuhen
In reply to @marv:marv.sexy

Hey guys, porting some discord bot from js to clojurescript right now and I think I am doing something wrong "style"-wise, my code feels like it could be improved

Trying to handle commands right now, to make it easier, there only is one command at the moment, which can have multiple subcommands.
I want to parse the subcommand and run the proper handler for it
currently doing that like this:

(defn- handle-subcommands [^Discord/Interaction interaction]
  (case (get-subcommand-name interaction)
    "rename" (handle-rename interaction)
    (print-unknown-command-error interaction)))

(defn- handle-chat-input-command [^Discord/Interaction interaction]
  (if (= "channel" (.-commandName interaction))
    (handle-subcommands interaction)
    (print-unknown-command-error interaction)))

(defn handle-interaction-create+ [^Discord/Interaction interaction]
  (cond (.isChatInputCommand interaction)
        (handle-chat-input-command interaction)))

This totally works, but I don't like finishing every line with interaction. I have been thinking about using partial on the handler functions but do have the feeling that it still would get cluttered a lot
can someone give me a hint into the correct direction here? :)

I don't think there's anything wrong with that. The only criticism I do have about the code is using a CASE where you intend to only handle a single subcommand anyway
23:16:59
@kakuhen:kotoura.moekakuhen

If you only ever intend for "rename" to be a valid case, then I would personally write

(when-not (= "rename" (get-subcomamnd-name interaction))
  (print-unknown-command-error interaction)
  (handle-rename interaction))
23:17:17
@kakuhen:kotoura.moekakuhen *

If you only ever intend for "rename" to be a valid case, then I would personally write

(if-not (= "rename" (get-subcomamnd-name interaction))
  (print-unknown-command-error interaction)
  (handle-rename interaction))
23:19:02
28 Aug 2022
@tomasz90:matrix.org@tomasz90:matrix.org changed their display name from Tomasz to Thomas.15:55:47
@marv:marv.sexy@marv:marv.sexyThere is going to be more subcommands, sorry for nor making that more clear 😄 Good to hear that this looks good, I felt like I am repeating myself too much16:00:41
10 Sep 2022
@tim:dertev.de@tim:dertev.de joined the room.12:40:53
@tim:dertev.de@tim:dertev.de left the room.12:41:25
@cyber9unk:matrix.orgPythonista joined the room.21:15:22
11 Sep 2022
@halontka89:matrix.org@halontka89:matrix.org joined the room.21:33:10
15 Sep 2022
@kakuhen:kotoura.moekakuhen
In reply to @marv:marv.sexy
There is going to be more subcommands, sorry for nor making that more clear 😄
Good to hear that this looks good, I felt like I am repeating myself too much
If a certain chunk of code repeats itself often and starts feeling like boilerplate, then it may be a good idea to encapsulate that into a function or a macro
19:52:58
@kakuhen:kotoura.moekakuhen with that said, I have no idea what Discord/Interaction looks like, but if it's a record, then you can always use associative destructuring to get values of the record directly, and I think it's perfectly fine to be using interaction many times, since it's just a function argument 19:54:06
17 Sep 2022
@shalinikumaran:matrix.orgShalini Kumaran joined the room.17:49:26
24 Sep 2022
@astmir:matrix.orgX bit joined the room.19:43:32
8 Oct 2022
@john-1:matrix.org@john-1:matrix.org joined the room.04:45:01
@john-1:matrix.org@john-1:matrix.org changed their profile picture.11:37:35
9 Oct 2022
@vistrcm:matrix.orgvistrcm joined the room.07:19:20
11 Oct 2022
@john-1:matrix.org@john-1:matrix.org left the room.16:23:27
16 Oct 2022
@ilmu:matrix.orgilmu joined the room.22:52:21
17 Oct 2022
@dinomug:matrix.org@dinomug:matrix.org left the room.17:27:39
21 Oct 2022
@Seylerius:matrix.orgPirates changed their profile picture.13:52:45
25 Oct 2022
@aeftink:matrix.orgaeftink joined the room.01:11:55
27 Oct 2022
@jak.wolf:matrix.orgjak.wolf joined the room.22:33:38
28 Oct 2022
@jak.wolf:matrix.orgjak.wolf set a profile picture.01:51:13
30 Oct 2022
@jak.wolf:matrix.orgjak.wolf changed their display name from ghostarchist to jak.wolf.07:35:05
9 Nov 2022
@robertlj:matrix.orgRobert Johnson joined the room.00:59:25
10 Nov 2022
@o5s6:matrix.orgo5s6 joined the room.06:50:12
26 Nov 2022
@halontka89:matrix.org@halontka89:matrix.org left the room.21:44:31
28 Nov 2022
@yuu:matrix.org@yuu:matrix.org left the room.11:57:02

Show newer messages


Back to Room ListRoom Version: