Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ config :fleet_bot, FleetBot.Discord,
discord_allowed_langs:
~w(ar da vi hu uk lt ja hi tr cs th zh-TW el pl zh-CN ko de en-GB fi id ru sv-SE bg es-ES it pt-BR nl ro fr no hr he en-US)

config :fleet_bot, FleetBot.Fleetyards,
api_url: "https://api.fleetyards.net",
client: FleetBot.Fleetyards.Client

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
2 changes: 1 addition & 1 deletion lib/fleet_bot/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule FleetBot.Application do
def start(_type, _args) do
children = [
FleetBot.Repo,
FleetBot.Discord.Commands.RegisterManager,
FleetBot.Discord.Commands,
FleetBot.Discord
]

Expand Down
39 changes: 38 additions & 1 deletion lib/fleet_bot/discord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ defmodule FleetBot.Discord do
import Ecto.Changeset

@spec chat_command_allowed_regex() :: Regex.t()
@moduledoc """
@doc """
Regex matcher for the discord allowed command string.

Converted from discords `^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$` Regex.

See: https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-naming
"""
alias FleetBot.Discord.Commands.RegisterManager
alias Nostrum.Consumer
def chat_command_allowed_regex, do: ~R/^[-_\p{L}\p{N}\p{Devanagari}\p{Thai}]{1,32}$/u

Expand Down Expand Up @@ -44,7 +45,25 @@ defmodule FleetBot.Discord do
else: changeset
end

@spec get_subcommand_name(
%Nostrum.Struct.Interaction{}
| %Nostrum.Struct.ApplicationCommandInteractionData{}
| [%Nostrum.Struct.ApplicationCommandInteractionDataOption{}]
| %Nostrum.Struct.ApplicationCommandInteractionDataOption{}
) :: String.t()
def get_subcommand_name(%Nostrum.Struct.Interaction{data: data}), do: get_subcommand_name(data)

def get_subcommand_name(%Nostrum.Struct.ApplicationCommandInteractionData{options: options}),
do: get_subcommand_name(options)

def get_subcommand_name([option | _]), do: get_subcommand_name(option)

def get_subcommand_name(%Nostrum.Struct.ApplicationCommandInteractionDataOption{name: name}),
do: name

## Consumer impl
use Nostrum.Consumer
use FleetBot.Gettext

def start_link, do: Consumer.start_link(__MODULE__)

Expand All @@ -54,6 +73,24 @@ defmodule FleetBot.Discord do
:noop
end

@impl Nostrum.Consumer
def handle_event(
{:INTERACTION_CREATE,
%Nostrum.Struct.Interaction{
data: %Nostrum.Struct.ApplicationCommandInteractionData{name: name}
} = interaction, _ws_state}
)
when is_binary(name) do
RegisterManager.get_module(name)
|> case do
module when is_atom(module) ->
apply(module, :command, [name, interaction])

nil ->
LGettext.error("No module found for command `%{command}`", command: name)
end
end

@impl true
def handle_event(_event) do
# event
Expand Down
67 changes: 67 additions & 0 deletions lib/fleet_bot/discord/Fleetyards.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule FleetBot.Discord.Fleetyards do
alias Nostrum.Api

use FleetBot.Discord.Command
use FleetBot.Gettext
alias FleetBot.Repo.Discord.FleetyardsAccount

@impl Command
def command("fleetyards", interaction) do
Api.create_interaction_response(
interaction,
create_interaction_response(
:deferred_channel_message_with_source,
create_interaction_response_data(flags: :ephemeral)
)
)

FleetBot.Discord.get_subcommand_name(interaction)
|> fleetyards_command(interaction)
end

def fleetyards_command("link", interaction) do
Api.edit_interaction_response(
interaction,
create_interaction_response_data(content: "todo", flags: :ephemeral)
)
end

def fleetyards_command("unlink", %Nostrum.Struct.Interaction{user: user} = interaction) do
FleetyardsAccount.get_account(user)
|> int_fleetyards_unlink(interaction)
end

@impl Command
def global_commands do
[
# create_command("fleetyards", "Fleetyards account management",
# member_permission: :SEND_MESSAGES,
# dm_permission: true,
# options: [
# create_option("link", "Link Fleetyards account",
# type: :sub_command,
# channel_types: [:guild_text, :dm, :guild_directory],
# options: [
# create_option("token", "Fleetyards access token", type: :string),
# create_option("username", "Fleetyards username or email address", type: :string),
# create_option("password", "Fleetyards password", type: :string)
# ]
# ),
# create_option("unlink", "Unlink Fleetyards accounts",
# type: :sub_command,
# channel_types: [:guild_text, :dm, :guild_directory]
# )
# ]
# )
]
end

# Internal helpers
defp int_fleetyards_unlink(nil, interaction) do
content = LGettext.dgettext("discord_fleetyards", "Account not linked, cannot unlink.")
Api.edit_interaction_response(interaction, create_interaction_response_data(content: content))
end

defp int_fleetyards_unlink(account, interaction) do
end
end
102 changes: 102 additions & 0 deletions lib/fleet_bot/discord/command.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
defmodule FleetBot.Discord.Command do
@callback command(String.t(), %Nostrum.Struct.Interaction{}) :: term()

@callback global_commands() :: [Nostrum.Struct.ApplicationCommand.application_command_map()]
@callback remove_global_commands() :: [String.t()]

@optional_callbacks global_commands: 0, remove_global_commands: 0

# Macros
defmacro __using__(_opts) do
quote do
@behaviour unquote(__MODULE__)
alias unquote(__MODULE__)
require unquote(__MODULE__)

import unquote(__MODULE__),
only: [
create_interaction_response: 1,
create_interaction_response: 2,
create_interaction_response_data: 1
]

require FleetBot.Discord.Commands.Register
import FleetBot.Discord.Commands.Register

use FleetBot.Gettext
end
end

defmacro create_interaction_response(type, data \\ nil) do
type = interaction_response_type(type)

quote do
%{
type: unquote(type),
data: unquote(data)
}
end
end

def create_interaction_response_data(opts) do
tts = Keyword.get(opts, :tts)
content = Keyword.get(opts, :content)
embeds = Keyword.get(opts, :embeds)
allowed_mentions = Keyword.get(opts, :allowed_mentions)

flags =
Keyword.get(opts, :flags)
|> interaction_response_data_flags()

components = Keyword.get(opts, :components)
attachements = Keyword.get(opts, :attachements)

# quote do
# %{
# tts: unquote(tts),
# content: unquote(
# content
# ),
# embeds: unquote(embeds),
# allowed_mentions: unquote(allowed_mentions),
# flags: unquote(flags),
# components: unquote(components),
# attachements: unquote(attachements)
# }
# end
%{
tts: tts,
content: content,
embeds: embeds,
allowed_mentions: allowed_mentions,
flags: flags,
components: components,
attachements: attachements
}
end

## Macro helpers
use Bitwise, only_operators: true

def interaction_response_type(:pong), do: 1
def interaction_response_type(:channel_message_with_source), do: 4
def interaction_response_type(:deferred_channel_message_with_source), do: 5
def interaction_response_type(:deferred_update_channel), do: 6
def interaction_response_type(:update_message), do: 7
def interaction_response_type(:application_command_autocomplete_result), do: 8
def interaction_response_type(:modal), do: 9
def interaction_response_type(v) when is_number(v), do: v

def interaction_response_data_flags(flags) when is_list(flags) do
flags
|> Enum.map(&interaction_response_data_flags/1)
|> Enum.reduce(0, fn flag, acc ->
flag ||| acc
end)
end

def interaction_response_data_flags(:suppress_embeds), do: 1 <<< 3
def interaction_response_data_flags(:ephemeral), do: 1 <<< 6
def interaction_response_data_flags(v) when is_number(v), do: v
def interaction_response_data_flags(nil), do: nil
end
33 changes: 24 additions & 9 deletions lib/fleet_bot/discord/commands.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
defmodule FleetBot.Discord.Commands do
use FleetBot.Discord.Commands.Register

@impl Register
def global_commands() do
[
create_command("login", "Login with fleetyards account",
member_permission: :SEND_MESSAGES,
dm_permission: true
)
use Supervisor

def get_commands,
do: [
FleetBot.Discord.Fleetyards
]

def start_link(opts) do
Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
end

@impl Supervisor
def init(_opts) do
children =
[
FleetBot.Discord.Commands.RegisterManager
] ++ get_supervised_commands()

Supervisor.init(children, strategy: :one_for_one)
end

def get_supervised_commands(commands \\ get_commands()) do
commands
|> Enum.filter(&Kernel.function_exported?(&1, :start_link, 1))
|> Enum.into([])
end
end
Loading