/source application command with test

This commit is contained in:
Owen
2024-12-13 23:02:23 +01:00
parent 1c4d0fb8af
commit 5975458407
12 changed files with 233 additions and 162 deletions
+15
View File
@@ -0,0 +1,15 @@
defmodule PlantIdDiscordBot.Application do
use Application
@port Application.compile_env(:plantid_discord_bot, :port)
@impl true
def start(_type, _args) do
children = [
PlantIdDiscordBot
]
opts = [strategy: :one_for_one, name: PlantIdDiscordBot.Supervisor]
Supervisor.start_link(children, opts)
end
end
+7
View File
@@ -0,0 +1,7 @@
defmodule PlantIdDiscordBotTest.Mocks.Nostrum.Api do
@moduledoc """
Mocks the Nostrum.Api module.
"""
def create_interaction_response(_interaction, response), do: {:ok, response}
end
+28 -9
View File
@@ -1,18 +1,37 @@
defmodule PlantIdDiscordBot do
@moduledoc """
Documentation for `PlantidDiscordBot`.
A Discord bot that identifies plants from photos of their organs.
"""
@doc """
Hello world.
use Nostrum.Consumer
alias Nostrum.Api
alias PlantIdDiscordBot.Cog
## Examples
IO.inspect("Starting PlantIdDiscordBot")
iex> PlantidDiscordBot.hello()
:world
@global_application_commands [
{"servers", "All servers that this bot belongs to, []"},
{"invite", "Invite link for this bot, []"},
{"source", "Link to the source code for this bot, []"},
{"info", "Information about this bot, []"},
{"stats", "Statistics about this bot, []"},
{"help", "Help information for this bot, []"},
{"status", "API Status, []"},
{"id", "ID a plant from up to 5 images, []"}
]
"""
def hello do
:world
# Starts the bot
def handle_event({:READY}) do
# mock function depending on the environment
Api.create_global_application_command(@global_application_commands)
# TODO fix, not working
Api.update_status(:online, "Guess the Plant|/help")
# TODO remove in prod
Api.create_guild_application_command(1_002_507_312_159_797_318, @global_application_commands)
end
# Handle /source command
def handle_event({:INTERACTION_CREATE, %{data: %{name: "source"}} = interaction, _ws_state}) do
Cog.Info.source(interaction)
end
end
+13
View File
@@ -0,0 +1,13 @@
defmodule PlantIdDiscordBot.Cog.Info do
use Nostrum.Consumer
@api Application.compile_env(:plantid_discord_bot, :api)
@source Application.compile_env(:plantid_discord_bot, :source)
@doc """
Sends a link to the source code for this bot.
"""
def source(interaction) do
@api.create_interaction_response(interaction, %{type: 4, data: %{content: @source}})
end
end