From 7cf2e79c3e2cea0ebfea489d19842430a11f75c5 Mon Sep 17 00:00:00 2001 From: Owen Date: Sun, 15 Dec 2024 16:10:37 +0100 Subject: [PATCH] status command --- config/config.exs | 3 +++ config/dev.exs | 1 - config/prod.exs | 1 - config/runtime.exs | 1 - config/test.exs | 1 - lib/mocks/nostrum_api.ex | 5 ++++- lib/plantid_discord_bot.ex | 22 +++++++++++++++++++--- lib/plantid_discord_bot/cogs/info.ex | 20 ++++++++++++++++++++ lib/rate_limiter.ex | 0 test/commands/info_test.exs | 9 +++++++++ 10 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 lib/rate_limiter.ex create mode 100644 test/commands/info_test.exs diff --git a/config/config.exs b/config/config.exs index d1186fe..bdcf328 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,3 +1,6 @@ import Config +config :plantid_discord_bot, + start_time: DateTime.utc_now() + import_config "#{config_env()}.exs" diff --git a/config/dev.exs b/config/dev.exs index 3dc5939..7c4a24f 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -2,7 +2,6 @@ import Config config :plantid_discord_bot, guild_ids: [1_002_507_312_159_797_318], - start_time: DateTime.utc_now(), plantnet_api_key: System.get_env("PLANTNET_API_KEY"), plantnet_api_base_url: "https://my-api.plantnet.org/v2", image_folder: "priv/static", diff --git a/config/prod.exs b/config/prod.exs index 9c045c2..3839f17 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -2,7 +2,6 @@ import Config config :plantid_discord_bot, guild_ids: [], - start_time: DateTime.utc_now(), plantnet_api_key: System.get_env("PLANTNET_API_KEY"), plantnet_api_base_url: "https://my-api.plantnet.org/v2", image_folder: "priv/static", diff --git a/config/runtime.exs b/config/runtime.exs index 4643bb4..36fad54 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -2,7 +2,6 @@ import Config config :plantid_discord_bot, guild_ids: [1_002_507_312_159_797_318], - start_time: DateTime.utc_now(), plantnet_api_key: System.get_env("PLANTNET_API_KEY"), plantnet_api_base_url: "https://my-api.plantnet.org/v2", image_folder: "priv/static", diff --git a/config/test.exs b/config/test.exs index 027988a..aa05f16 100644 --- a/config/test.exs +++ b/config/test.exs @@ -2,7 +2,6 @@ import Config config :plantid_discord_bot, guild_ids: [1_002_507_312_159_797_318], - start_time: DateTime.utc_now(), plantnet_api_key: System.get_env("PLANTNET_API_KEY"), plantnet_api_base_url: "https://my-api.plantnet.org/v2", image_folder: "priv/static", diff --git a/lib/mocks/nostrum_api.ex b/lib/mocks/nostrum_api.ex index 9f2ac05..4ad4f0e 100644 --- a/lib/mocks/nostrum_api.ex +++ b/lib/mocks/nostrum_api.ex @@ -4,5 +4,8 @@ defmodule PlantIdDiscordBotTest.Mocks.Nostrum.Api do """ def create_interaction_response(_interaction, response), do: {:ok, response} - def get_application_info(), do: {:ok, %{name: "Test Bot", owner: %{username: "Test User"}}} + + def get_application_information() do + {:ok, %{name: "Test Bot", approximate_guild_count: 1, owner: %{username: "Test User"}}} + end end diff --git a/lib/plantid_discord_bot.ex b/lib/plantid_discord_bot.ex index 9c645d5..d594a20 100644 --- a/lib/plantid_discord_bot.ex +++ b/lib/plantid_discord_bot.ex @@ -10,13 +10,13 @@ defmodule PlantIdDiscordBot do IO.inspect("Starting PlantIdDiscordBot") @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, []"}, + {"invite", "Invite link for this bot, []"}, + {"help", "Help information for this bot, []"}, {"info", "Information about this bot, []"}, {"stats", "Statistics about this bot, []"}, - {"help", "Help information for this bot, []"}, {"status", "API Status, []"}, + {"servers", "All servers that this bot belongs to, []"}, {"id", "ID a plant from up to 5 images, []"} ] @@ -49,4 +49,20 @@ defmodule PlantIdDiscordBot do def handle_event({:INTERACTION_CREATE, %{data: %{name: "info"}} = interaction, _ws_state}) do Cog.Info.info(interaction) end + + # def handle_event({:INTERACTION_CREATE, %{data: %{name: "stats"}} = interaction, _ws_state}) do + # Cog.Info.info(interaction) + # end + + def handle_event({:INTERACTION_CREATE, %{data: %{name: "status"}} = interaction, _ws_state}) do + Cog.Info.status(interaction) + end + + # def handle_event({:INTERACTION_CREATE, %{data: %{name: "servers"}} = interaction, _ws_state}) do + # Cog.Info.info(interaction) + # end + + # def handle_event({:INTERACTION_CREATE, %{data: %{name: "id"}} = interaction, _ws_state}) do + # Cog.Info.info(interaction) + # end end diff --git a/lib/plantid_discord_bot/cogs/info.ex b/lib/plantid_discord_bot/cogs/info.ex index 607bc29..8f99002 100644 --- a/lib/plantid_discord_bot/cogs/info.ex +++ b/lib/plantid_discord_bot/cogs/info.ex @@ -3,9 +3,13 @@ defmodule PlantIdDiscordBot.Cog.Info do import Nostrum.Struct.Embed alias PlantIdDiscordBot.Utils + # reference to Nostrum.Api in non-test environments, reference to mock in test @api Application.compile_env(:plantid_discord_bot, :api) + + # urls @source Application.compile_env(:plantid_discord_bot, :source) @invite Application.compile_env(:plantid_discord_bot, :invite) + @plantnet_api_base_url Application.compile_env(:plantid_discord_bot, :plantnet_api_base_url) @doc """ Sends a link to the source code for this bot. @@ -69,4 +73,20 @@ defmodule PlantIdDiscordBot.Cog.Info do response = %{type: 4, data: %{embeds: [embed]}} @api.create_interaction_response(interaction, response) end + + @doc """ + Check the status of all relevant APIs. + """ + def status(interaction) do + message = + case HTTPoison.get("#{@plantnet_api_base_url}/_status") do + {:ok, %{status_code: 200}} -> + "PlantNet API is up and running" + + _ -> + "PlantNet API is down" + end + + @api.create_interaction_response(interaction, %{type: 4, data: %{content: message}}) + end end diff --git a/lib/rate_limiter.ex b/lib/rate_limiter.ex new file mode 100644 index 0000000..e69de29 diff --git a/test/commands/info_test.exs b/test/commands/info_test.exs new file mode 100644 index 0000000..469b152 --- /dev/null +++ b/test/commands/info_test.exs @@ -0,0 +1,9 @@ +defmodule PlantIdDiscordBotTest.Cog.Info do + use ExUnit.Case + + test "/info" do + {:ok, response} = PlantIdDiscordBot.Cog.Info.info(%{data: %{name: "info"}}) + [embed] = response[:data][:embeds] + assert match?(%Nostrum.Struct.Embed{}, embed) + end +end