From 1d31d9b677ff6b8e6d17208461f0a1df36a0eb03 Mon Sep 17 00:00:00 2001 From: Idris TAKERBOUST Date: Wed, 18 Oct 2023 15:10:14 +0200 Subject: [PATCH 1/2] feat: chose a specific type in channel_selection --- examples/select_menus.rb | 2 +- lib/discordrb/webhooks/view.rb | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/examples/select_menus.rb b/examples/select_menus.rb index 9ced90a3b..946914dba 100644 --- a/examples/select_menus.rb +++ b/examples/select_menus.rb @@ -68,7 +68,7 @@ 'channel_select', false, nil, nil, nil, nil, Discordrb::Components::View.new do |builder| builder.row do |r| - r.channel_select(custom_id: 'channel_select', placeholder: 'Test of ChannelSelect', max_values: 3) + r.channel_select(custom_id: 'channel_select', placeholder: 'Test of ChannelSelect', max_values: 3, channel_types: %i[guild_text guild_voice]) end end ) diff --git a/lib/discordrb/webhooks/view.rb b/lib/discordrb/webhooks/view.rb index e773628fe..292a3c16c 100644 --- a/lib/discordrb/webhooks/view.rb +++ b/lib/discordrb/webhooks/view.rb @@ -24,6 +24,24 @@ class Discordrb::Webhooks::View channel_select: 8 }.freeze + # Channel types. + # @see https://discord.com/developers/docs/resources/channel#channel-object-channel-types + CHANNEL_TYPES = { + guild_text: 0, + dm: 1, + guild_voice: 2, + group_dm: 3, + guild_category: 4, + guild_announcement: 5, + announcement_thread: 10, + public_thread: 11, + private_thread: 12, + guild_stage_voice: 13, + guild_directory: 14, + guild_forum: 15, + guild_media: 16 + }.freeze + # This builder is used when constructing an ActionRow. All current components must be within an action row, but this can # change in the future. A message can have 5 action rows, each action row can hold a weight of 5. Buttons have a weight of 1, # and dropdowns have a weight of 5. @@ -114,8 +132,16 @@ def mentionable_select(custom_id:, placeholder: nil, min_values: nil, max_values # @param min_values [Integer, nil] The minimum amount of values a user must select. # @param max_values [Integer, nil] The maximum amount of values a user can select. # @param disabled [true, false, nil] Grey out the component to make it unusable. - def channel_select(custom_id:, placeholder: nil, min_values: nil, max_values: nil, disabled: nil) - @components << SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, disabled, select_type: :channel_select).to_h + # @param channel_types [Array, nil] Display only the specific channel type(s). + def channel_select(custom_id:, placeholder: nil, min_values: nil, max_values: nil, disabled: nil, channel_types: nil) + builder = SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, disabled, select_type: :channel_select).to_h + + if channel_types + types_selected = CHANNEL_TYPES.values_at(*channel_types) + builder[:channel_types] = types_selected + end + + @components << builder end # @!visibility private From 626e120c9b1390d457b6c90e103f3f56b1251baf Mon Sep 17 00:00:00 2001 From: Idris TAKERBOUST Date: Wed, 18 Oct 2023 16:51:29 +0200 Subject: [PATCH 2/2] reuse the existing constant --- examples/select_menus.rb | 2 +- lib/discordrb/data/channel.rb | 4 +++- lib/discordrb/webhooks/view.rb | 22 ++-------------------- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/examples/select_menus.rb b/examples/select_menus.rb index 946914dba..a1892053c 100644 --- a/examples/select_menus.rb +++ b/examples/select_menus.rb @@ -68,7 +68,7 @@ 'channel_select', false, nil, nil, nil, nil, Discordrb::Components::View.new do |builder| builder.row do |r| - r.channel_select(custom_id: 'channel_select', placeholder: 'Test of ChannelSelect', max_values: 3, channel_types: %i[guild_text guild_voice]) + r.channel_select(custom_id: 'channel_select', placeholder: 'Test of ChannelSelect', max_values: 3, channel_types: %i[text voice]) end end ) diff --git a/lib/discordrb/data/channel.rb b/lib/discordrb/data/channel.rb index 4f442b7c8..c995956e1 100644 --- a/lib/discordrb/data/channel.rb +++ b/lib/discordrb/data/channel.rb @@ -9,6 +9,7 @@ class Channel include IDObject # Map of channel types + # @see https://discord.com/developers/docs/resources/channel#channel-object-channel-types TYPES = { text: 0, dm: 1, @@ -22,7 +23,8 @@ class Channel private_thread: 12, stage_voice: 13, directory: 14, - forum: 15 + forum: 15, + media: 16 }.freeze # @return [String] this channel's name. diff --git a/lib/discordrb/webhooks/view.rb b/lib/discordrb/webhooks/view.rb index 292a3c16c..1450a9348 100644 --- a/lib/discordrb/webhooks/view.rb +++ b/lib/discordrb/webhooks/view.rb @@ -24,24 +24,6 @@ class Discordrb::Webhooks::View channel_select: 8 }.freeze - # Channel types. - # @see https://discord.com/developers/docs/resources/channel#channel-object-channel-types - CHANNEL_TYPES = { - guild_text: 0, - dm: 1, - guild_voice: 2, - group_dm: 3, - guild_category: 4, - guild_announcement: 5, - announcement_thread: 10, - public_thread: 11, - private_thread: 12, - guild_stage_voice: 13, - guild_directory: 14, - guild_forum: 15, - guild_media: 16 - }.freeze - # This builder is used when constructing an ActionRow. All current components must be within an action row, but this can # change in the future. A message can have 5 action rows, each action row can hold a weight of 5. Buttons have a weight of 1, # and dropdowns have a weight of 5. @@ -132,12 +114,12 @@ def mentionable_select(custom_id:, placeholder: nil, min_values: nil, max_values # @param min_values [Integer, nil] The minimum amount of values a user must select. # @param max_values [Integer, nil] The maximum amount of values a user can select. # @param disabled [true, false, nil] Grey out the component to make it unusable. - # @param channel_types [Array, nil] Display only the specific channel type(s). + # @param channel_types [Array, nil] Display only the specific channel type(s). def channel_select(custom_id:, placeholder: nil, min_values: nil, max_values: nil, disabled: nil, channel_types: nil) builder = SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, disabled, select_type: :channel_select).to_h if channel_types - types_selected = CHANNEL_TYPES.values_at(*channel_types) + types_selected = Discordrb::Channel::TYPES.values_at(*channel_types) builder[:channel_types] = types_selected end