From bfd7a7f53a4b9914eab429afa9bc129e85e91e47 Mon Sep 17 00:00:00 2001 From: alichz Date: Sun, 23 Jul 2023 17:40:37 +0330 Subject: [PATCH 1/3] add command_schema_options for slash commands --- lib/nosedrum/application_command.ex | 31 +++++++++++++++++++++++++++++ lib/nosedrum/storage/dispatcher.ex | 8 ++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/nosedrum/application_command.ex b/lib/nosedrum/application_command.ex index 478f96d..00cad7a 100644 --- a/lib/nosedrum/application_command.ex +++ b/lib/nosedrum/application_command.ex @@ -289,5 +289,36 @@ defmodule Nosedrum.ApplicationCommand do """ @callback command(interaction :: Interaction.t()) :: response + + @typedoc """ + Application command optional properties. + See [official Discord documentation](https://discord.com/developers/docs/interactions/application-commands#application-command-object) + """ + @type application_command :: %{ + optional(:description_localizations) => map(), + optional(:default_member_permissions) => non_neg_integer(), + optional(:dm_permission) => boolean(), # only for globally-scoped commands + optional(:default_permission) => boolean(), #Not recommended for use as field will soon be deprecated. + optional(:nsfw) => boolean() + } + + @doc """ + An optional callback that returns a map contains optioanl properties of slash application command. + Used when registering the command with Discord. Only valid for + CHAT_INPUT application commands, aka slash commands. + + Read more in the official + [Application Command documentation](https://discord.com/developers/docs/interactions/application-commands#application-command-object). + + ## Example + ```elixir + def command_schema_options(), do: + %{ + default_member_permissions: 8 + } + ``` + """ + @callback command_schema_options() :: application_command() + @optional_callbacks [options: 0] end diff --git a/lib/nosedrum/storage/dispatcher.ex b/lib/nosedrum/storage/dispatcher.ex index 13c15f7..8251795 100644 --- a/lib/nosedrum/storage/dispatcher.ex +++ b/lib/nosedrum/storage/dispatcher.ex @@ -170,11 +170,19 @@ defmodule Nosedrum.Storage.Dispatcher do [] end + command_schema_options = + if function_exported?(command, :command_schema_options, 0) do + command.command_schema_options() + else + %{} + end + %{ type: parse_type(command.type()), name: name } |> put_type_specific_fields(command, options) + |> Map.merge(command_schema_options) end # This seems like a hacky way to unwrap the outer list... From a5873d9b14a748a1cf987fc4b9bd2d847ddf085a Mon Sep 17 00:00:00 2001 From: alichz Date: Sun, 23 Jul 2023 17:52:19 +0330 Subject: [PATCH 2/3] support for add list of permissions for default_member_permissions: [:administrator] --- lib/nosedrum/application_command.ex | 2 +- lib/nosedrum/storage/dispatcher.ex | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/nosedrum/application_command.ex b/lib/nosedrum/application_command.ex index 00cad7a..4b6912f 100644 --- a/lib/nosedrum/application_command.ex +++ b/lib/nosedrum/application_command.ex @@ -296,7 +296,7 @@ defmodule Nosedrum.ApplicationCommand do """ @type application_command :: %{ optional(:description_localizations) => map(), - optional(:default_member_permissions) => non_neg_integer(), + optional(:default_member_permissions) => non_neg_integer() | [Nostrum.Permission.t], optional(:dm_permission) => boolean(), # only for globally-scoped commands optional(:default_permission) => boolean(), #Not recommended for use as field will soon be deprecated. optional(:nsfw) => boolean() diff --git a/lib/nosedrum/storage/dispatcher.ex b/lib/nosedrum/storage/dispatcher.ex index 8251795..682d499 100644 --- a/lib/nosedrum/storage/dispatcher.ex +++ b/lib/nosedrum/storage/dispatcher.ex @@ -173,6 +173,7 @@ defmodule Nosedrum.Storage.Dispatcher do command_schema_options = if function_exported?(command, :command_schema_options, 0) do command.command_schema_options() + |> handle_default_member_permissions() else %{} end @@ -185,6 +186,15 @@ defmodule Nosedrum.Storage.Dispatcher do |> Map.merge(command_schema_options) end + defp handle_default_member_permissions(%{default_member_permissions: default_member_permissions} = data) + when is_list(default_member_permissions), do: + %{data | default_member_permissions: Nostrum.Permission.to_bitset(default_member_permissions)} + + defp handle_default_member_permissions(%{default_member_permissions: default_member_permissions} = data) + when is_integer(default_member_permissions), do: data + + defp handle_default_member_permissions(data), do: data + # This seems like a hacky way to unwrap the outer list... defp build_payload(path, command) do build_payload({path, command}) From 673a9c6c0464628ae3db20047937451efa31e61e Mon Sep 17 00:00:00 2001 From: alichz Date: Wed, 26 Jul 2023 16:27:18 +0330 Subject: [PATCH 3/3] add recursive type translate --- lib/nosedrum/storage/dispatcher.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/nosedrum/storage/dispatcher.ex b/lib/nosedrum/storage/dispatcher.ex index 682d499..a36332c 100644 --- a/lib/nosedrum/storage/dispatcher.ex +++ b/lib/nosedrum/storage/dispatcher.ex @@ -242,6 +242,7 @@ defmodule Nosedrum.Storage.Dispatcher do Enum.map(options, fn map when is_map_key(map, :type) -> Map.update!(map, :type, &Map.fetch!(@option_type_map, &1)) + |> Map.update(:options, [], fn opts -> parse_option_types(opts) end) map -> map