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
(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 :sonarr/quality-profile string?)

View File

@@ -6,7 +6,8 @@
[taoensso.timbre :refer [fatal]]
[doplarr.utils :as utils]
[fmnoise.flow :as flow :refer [else]]
[discljord.messaging :as m]))
[discljord.messaging :as m]
[clojure.set :as set]))
(defn request-command [media-types]
{:name "request"
@@ -64,6 +65,13 @@
:custom_id (str "request:" uuid ":" 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]
{:label (or (:title result) (:name result))
:description (:year result)
@@ -83,18 +91,19 @@
(str "result-select:" uuid)
(map-indexed select-menu-option results))))
(defn option-dropdown [option options uuid]
(let [ddown (dropdown (str "Which " (utils/canonical-option-name option) "?")
(defn option-dropdown [option options uuid page]
(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))
(take MAX-OPTIONS (map #(hash-map :label (:name %) :value (:id %)) options)))]
(if (> (count options) MAX-OPTIONS)
(update-in ddown [:components] conj {:type 1
:components [{:type 2
:style 1
:custom_id (str "option-page:" uuid ":")
:disabled false
:label "More Options"}]})
ddown)))
(nth chunked page))]
(cond-> ddown
; Create the action row if we have more than 1 chunk
(> (count chunked) 1) (update-in [:components] conj {:type 1 :components []})
; More chunks exist
(< page (dec (count chunked))) (update-in [:components 1 :components] conj (page-button uuid (name option) (inc page) "More"))
; Past chunk 1
(> page 0) (update-in [:components 1 :components] conj (page-button uuid (name option) (dec page) "Less")))))
(defn dropdown-result [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))
(else #(fatal % "Error in sending request embed"))))
(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"))))))))
(defmulti process-event! (fn [event _ _ _] event))
@@ -75,6 +75,16 @@
(swap! state/cache update-in [uuid :payload] merge ready-opts)
(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]
(let [selection (discord/dropdown-result interaction)
cache-val (swap! state/cache update-in [uuid :pending-opts] #(dissoc % (keyword option)))]