Paged dropdowns works!

This commit is contained in:
Kiran Shila
2022-01-10 13:18:55 -08:00
parent b4b15a4ce5
commit 93f8e0f9fb
3 changed files with 34 additions and 14 deletions

View File

@@ -18,7 +18,8 @@
; Optional settings ; Optional settings
(spec/def :discord/role-id string?) (spec/def :discord/role-id string?)
(spec/def :discord/max-results pos-int?) (spec/def :discord/max-results #(and (pos-int? %)
(<= % 25)))
(spec/def :radarr/quality-profile string?) (spec/def :radarr/quality-profile string?)
(spec/def :sonarr/quality-profile string?) (spec/def :sonarr/quality-profile string?)

View File

@@ -6,7 +6,8 @@
[taoensso.timbre :refer [fatal]] [taoensso.timbre :refer [fatal]]
[doplarr.utils :as utils] [doplarr.utils :as utils]
[fmnoise.flow :as flow :refer [else]] [fmnoise.flow :as flow :refer [else]]
[discljord.messaging :as m])) [discljord.messaging :as m]
[clojure.set :as set]))
(defn request-command [media-types] (defn request-command [media-types]
{:name "request" {:name "request"
@@ -64,6 +65,13 @@
:custom_id (str "request:" uuid ":" format) :custom_id (str "request:" uuid ":" format)
:label (str/trim (str "Request " format))}) :label (str/trim (str "Request " format))})
(defn page-button [uuid option page label]
{:type 2
:style 1
:custom_id (str "option-page:" uuid ":" option "-" page)
:disabled false
:label label})
(defn select-menu-option [index result] (defn select-menu-option [index result]
{:label (or (:title result) (:name result)) {:label (or (:title result) (:name result))
:description (:year result) :description (:year result)
@@ -83,18 +91,19 @@
(str "result-select:" uuid) (str "result-select:" uuid)
(map-indexed select-menu-option results)))) (map-indexed select-menu-option results))))
(defn option-dropdown [option options uuid] (defn option-dropdown [option options uuid page]
(let [ddown (dropdown (str "Which " (utils/canonical-option-name option) "?") (let [all-options (map #(set/rename-keys % {:name :label :id :value}) options)
chunked (partition-all MAX-OPTIONS all-options)
ddown (dropdown (str "Which " (utils/canonical-option-name option) "?")
(str "option-select:" uuid ":" (name option)) (str "option-select:" uuid ":" (name option))
(take MAX-OPTIONS (map #(hash-map :label (:name %) :value (:id %)) options)))] (nth chunked page))]
(if (> (count options) MAX-OPTIONS) (cond-> ddown
(update-in ddown [:components] conj {:type 1 ; Create the action row if we have more than 1 chunk
:components [{:type 2 (> (count chunked) 1) (update-in [:components] conj {:type 1 :components []})
:style 1 ; More chunks exist
:custom_id (str "option-page:" uuid ":") (< page (dec (count chunked))) (update-in [:components 1 :components] conj (page-button uuid (name option) (inc page) "More"))
:disabled false ; Past chunk 1
:label "More Options"}]}) (> page 0) (update-in [:components 1 :components] conj (page-button uuid (name option) (dec page) "Less")))))
ddown)))
(defn dropdown-result [interaction] (defn dropdown-result [interaction]
(Integer/parseInt (s/select-one [:payload :values 0] interaction))) (Integer/parseInt (s/select-one [:payload :values 0] interaction)))

View File

@@ -52,7 +52,7 @@
(->> @(m/edit-original-interaction-response! messaging bot-id token (discord/request embed uuid)) (->> @(m/edit-original-interaction-response! messaging bot-id token (discord/request embed uuid))
(else #(fatal % "Error in sending request embed")))) (else #(fatal % "Error in sending request embed"))))
(let [[op options] (first pending-opts)] (let [[op options] (first pending-opts)]
(->> @(m/edit-original-interaction-response! messaging bot-id token (discord/option-dropdown op options uuid)) (->> @(m/edit-original-interaction-response! messaging bot-id token (discord/option-dropdown op options uuid 0))
(else #(fatal % "Error in creating option dropdown")))))))) (else #(fatal % "Error in creating option dropdown"))))))))
(defmulti process-event! (fn [event _ _ _] event)) (defmulti process-event! (fn [event _ _ _] event))
@@ -75,6 +75,16 @@
(swap! state/cache update-in [uuid :payload] merge ready-opts) (swap! state/cache update-in [uuid :payload] merge ready-opts)
(query-for-option-or-request pending-opts uuid)))) (query-for-option-or-request pending-opts uuid))))
(defmethod process-event! "option-page" [_ _ uuid option]
(let [{:keys [messaging bot-id]} @state/discord
{:keys [pending-opts token]} (get @state/cache uuid)
[opt page] (str/split option #"-")
op (keyword opt)
page (Long/parseLong page)
options (op pending-opts)]
(->> @(m/edit-original-interaction-response! messaging bot-id token (discord/option-dropdown op options uuid page))
(else #(fatal % "Error in updating option dropdown")))))
(defmethod process-event! "option-select" [_ interaction uuid option] (defmethod process-event! "option-select" [_ interaction uuid option]
(let [selection (discord/dropdown-result interaction) (let [selection (discord/dropdown-result interaction)
cache-val (swap! state/cache update-in [uuid :pending-opts] #(dissoc % (keyword option)))] cache-val (swap! state/cache update-in [uuid :pending-opts] #(dissoc % (keyword option)))]