Skip to content

Commit

Permalink
Merge pull request #618 from MiniAppleTheApple/master
Browse files Browse the repository at this point in the history
Support for new select menu components
  • Loading branch information
jchristgit authored Aug 14, 2024
2 parents 02d4267 + 98a8f6a commit e6e0b12
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/nostrum/struct/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ defmodule Nostrum.Struct.Component do
"""
@moduledoc since: "0.5.0"

alias Nostrum.Struct.Channel

defmacro __using__(_opts) do
quote do
alias Nostrum.Struct.Component.{ActionRow, Button, Option, SelectMenu, TextInput}
Expand Down Expand Up @@ -167,7 +169,9 @@ defmodule Nostrum.Struct.Component do
:max_length,
:required,
:value,
:components
:components,
:channel_types,
:default_values
]

@typedoc """
Expand Down Expand Up @@ -308,6 +312,10 @@ defmodule Nostrum.Struct.Component do
"""
@type components :: [SelectMenu.t() | Button.t() | nil]

@type channel_types :: [Channel.type()]

@type default_values :: [DefaultValue.t()]

@spec to_struct(map()) :: struct
def to_struct(%{} = map) do
new =
Expand Down
2 changes: 1 addition & 1 deletion lib/nostrum/struct/component/action_row.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Nostrum.Struct.Component.ActionRow do
"""
def action_row(opts \\ [])

def action_row(%Component{type: type} = component) when type in [3, 4] do
def action_row(%Component{type: type} = component) when type in [3, 4, 5, 6, 7, 8] do
[
{:components, [component]}
]
Expand Down
46 changes: 46 additions & 0 deletions lib/nostrum/struct/component/channel_select.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule Nostrum.Struct.Component.ChannelSelect do
@moduledoc """
Channel Select
"""
alias Nostrum.Constants.ComponentType

@defaults %{
custom_id: nil,
min_values: 1,
disabled: false,
max_values: 1,
channel_types: [],
type: ComponentType.channel_select()
}

use Nostrum.Struct.Component

@type t :: %Component{
custom_id: Component.custom_id(),
disabled: Component.disabled(),
min_values: Component.min_values(),
max_values: Component.max_values(),
channel_types: Component.channel_types(),
type: Component.type()
}

@type opt ::
{:custom_id, Component.custom_id()}
| {:min_values, Component.min_values()}
| {:max_values, Component.max_values()}
| {:disabled, Component.disabled()}
| {:channel_types, Component.channel_types()}

@type opts :: [opt]

def channel_select(custom_id, opts \\ []) when is_binary(custom_id) do
[
custom_id: custom_id,
disabled: opts[:disabled],
min_values: opts[:min_values],
max_values: opts[:max_values],
channel_types: opts[:channel_types]
]
|> new()
end
end
19 changes: 19 additions & 0 deletions lib/nostrum/struct/component/default_value.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Nostrum.Struct.Component.DefaultValue do
@moduledoc """
Default Value
"""

@derive Jason.Encoder
defstruct [
:id,
:type
]

@type t :: %__MODULE__{
id: id(),
type: type()
}

@type id :: Nostrum.Snowflake.t()
@type type :: :user | :role | :channel
end
40 changes: 40 additions & 0 deletions lib/nostrum/struct/component/mentionable_select.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Nostrum.Struct.Component.MentionableSelect do
@moduledoc """
Mentionable Select
"""
alias Nostrum.Constants.ComponentType
alias Nostrum.Struct.Component.SelectMenu

@defaults %{
custom_id: nil,
placeholder: "",
min_values: 1,
disabled: false,
max_values: 1,
type: ComponentType.mentionable_select()
}

use Nostrum.Struct.Component

@type t :: SelectMenu.t()

@type opt ::
{:custom_id, Component.custom_id()}
| {:placeholder, Component.placeholder()}
| {:min_values, Component.min_values()}
| {:max_values, Component.max_values()}
| {:disabled, Component.disabled()}

@type opts :: [opt]

def mentionable_select(custom_id, opts \\ []) when is_binary(custom_id) do
[
custom_id: custom_id,
disabled: opts[:disabled],
placeholder: opts[:placeholder],
min_values: opts[:min_values],
max_values: opts[:max_values]
]
|> new()
end
end
40 changes: 40 additions & 0 deletions lib/nostrum/struct/component/role_select.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Nostrum.Struct.Component.RoleSelect do
@moduledoc """
Role Select
"""
alias Nostrum.Constants.ComponentType
alias Nostrum.Struct.Component.SelectMenu

@defaults %{
custom_id: nil,
placeholder: "",
min_values: 1,
disabled: false,
max_values: 1,
type: ComponentType.role_select()
}

use Nostrum.Struct.Component

@type t :: SelectMenu.t()

@type opt ::
{:custom_id, Component.custom_id()}
| {:placeholder, Component.placeholder()}
| {:min_values, Component.min_values()}
| {:max_values, Component.max_values()}
| {:disabled, Component.disabled()}

@type opts :: [opt]

def mentionable_select(custom_id, opts \\ []) when is_binary(custom_id) do
[
custom_id: custom_id,
disabled: opts[:disabled],
placeholder: opts[:placeholder],
min_values: opts[:min_values],
max_values: opts[:max_values]
]
|> new()
end
end
40 changes: 40 additions & 0 deletions lib/nostrum/struct/component/user_select.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Nostrum.Struct.Component.UserSelect do
@moduledoc """
User Select
"""
alias Nostrum.Constants.ComponentType
alias Nostrum.Struct.Component.SelectMenu

@defaults %{
custom_id: nil,
placeholder: "",
min_values: 1,
disabled: false,
max_values: 1,
type: ComponentType.user_select()
}

use Nostrum.Struct.Component

@type t :: SelectMenu.t()

@type opt ::
{:custom_id, Component.custom_id()}
| {:placeholder, Component.placeholder()}
| {:min_values, Component.min_values()}
| {:max_values, Component.max_values()}
| {:disabled, Component.disabled()}

@type opts :: [opt]

def mentionable_select(custom_id, opts \\ []) when is_binary(custom_id) do
[
custom_id: custom_id,
disable: opts[:disabled],
placeholder: opts[:placeholder],
min_values: opts[:min_values],
max_values: opts[:max_values]
]
|> new()
end
end

0 comments on commit e6e0b12

Please sign in to comment.