mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-23 04:26:57 +00:00
remove id application command
This commit is contained in:
@@ -1,169 +0,0 @@
|
||||
defmodule PlantIdDiscordBot.Cog.PlantNet do
|
||||
require Logger
|
||||
use Nostrum.Consumer
|
||||
alias Nostrum.Api
|
||||
alias Nostrum.Cache.GuildCache
|
||||
alias PlantIdDiscordBot.RateLimiter
|
||||
alias PlantIdDiscordBot.PlantNet.Parser
|
||||
alias PlantIdDiscordBot.FileServer.File
|
||||
alias PlantIdDiscordBot.Metrics
|
||||
|
||||
@api Application.compile_env(:plantid_discord_bot, :api)
|
||||
@plantnet_api_base_url Application.compile_env(:plantid_discord_bot, :plantnet_api_base_url)
|
||||
@max_results Application.compile_env(:plantid_discord_bot, :max_results)
|
||||
|
||||
@doc """
|
||||
Process /id application command.
|
||||
"""
|
||||
def id(interaction) do
|
||||
case RateLimiter.check_limit(interaction.guild_id) do
|
||||
{:limit_exceeded, _} ->
|
||||
@api.create_interaction_response(interaction, %{
|
||||
type: 4,
|
||||
data: %{
|
||||
content:
|
||||
"This server has exceeded its allowed requests in 24 hours. Please try again tomorrow."
|
||||
}
|
||||
})
|
||||
|
||||
{:ok, _} ->
|
||||
do_identification(interaction)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_identification(interaction) do
|
||||
@api.create_interaction_response(interaction, %{
|
||||
type: 5,
|
||||
data: %{
|
||||
content: "Processing..."
|
||||
}
|
||||
})
|
||||
|
||||
attachment_urls = get_attachment_urls(interaction)
|
||||
original_images = get_original_images(attachment_urls)
|
||||
|
||||
saved_images =
|
||||
try do
|
||||
File.download_and_save_files!(attachment_urls)
|
||||
rescue
|
||||
e in ArgumentError ->
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: e.message
|
||||
})
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
if saved_images do
|
||||
try do
|
||||
prepare_images(saved_images)
|
||||
|> build_query_uri()
|
||||
|> get_response(interaction, original_images)
|
||||
rescue
|
||||
e ->
|
||||
Logger.error(Exception.format(:error, e, __STACKTRACE__))
|
||||
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: "An error occurred. This error has been logged."
|
||||
})
|
||||
after
|
||||
cleanup_saved_images(saved_images)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp get_attachment_urls(interaction) do
|
||||
interaction.data.resolved.attachments
|
||||
|> Map.values()
|
||||
|> Enum.map(& &1.url)
|
||||
end
|
||||
|
||||
@spec get_original_images([String.t()]) :: String.t()
|
||||
defp get_original_images(attachment_urls), do: Enum.join(attachment_urls, "\n")
|
||||
|
||||
defp prepare_images(saved_images) do
|
||||
case Application.get_env(:plantid_discord_bot, :environment) do
|
||||
:test ->
|
||||
PlantIdDiscordBotTest.Mocks.PlantNet.Images.images()
|
||||
|
||||
:dev ->
|
||||
PlantIdDiscordBotTest.Mocks.PlantNet.Images.images()
|
||||
|
||||
_ ->
|
||||
Enum.map(saved_images, fn {:ok, filename} ->
|
||||
"#{Application.get_env(:plantid_discord_bot, :fileserver_url)}/#{filename}"
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
@spec build_query_uri([String.t()]) :: String.t()
|
||||
defp build_query_uri(image_filenames) do
|
||||
URI.parse(
|
||||
"#{@plantnet_api_base_url}/identify/all?api-key=#{Application.get_env(:plantid_discord_bot, :plantnet_api_key)}"
|
||||
)
|
||||
|> URI.append_query("images=#{Enum.join(image_filenames, "&images=")}")
|
||||
|> URI.append_query("nb-results=#{@max_results}")
|
||||
|> URI.append_query("type=kt")
|
||||
|> URI.to_string()
|
||||
end
|
||||
|
||||
@spec get_response(String.t(), Nostrum.Interaction.t(), String.t()) :: :ok
|
||||
defp get_response(query_uri, interaction, original_images) do
|
||||
case HTTPoison.get(query_uri) do
|
||||
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
|
||||
response_message = Parser.parse(body)
|
||||
|
||||
guild_id = interaction.guild_id
|
||||
{:ok, %{name: guild_name}} = GuildCache.get(guild_id)
|
||||
|
||||
RateLimiter.increase_counter(guild_id)
|
||||
Metrics.increase_request_count(guild_id, guild_name)
|
||||
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: response_message <> "\n#{original_images}"
|
||||
})
|
||||
|
||||
{:ok, %HTTPoison.Response{status_code: 400, body: body}} ->
|
||||
Logger.error("""
|
||||
Malformed request sent to the PlantNet Api from PlantIdDiscordBot.Cog.PlantNet.do_identification
|
||||
Query URI: #{query_uri}
|
||||
Status Code: 400
|
||||
Response Body: #{String.slice(body, 0..500)}... (truncated)
|
||||
""")
|
||||
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: "Bad Request. This error has been logged."
|
||||
})
|
||||
|
||||
{:ok, %HTTPoison.Response{status_code: 404}} ->
|
||||
guild_id = interaction.guild_id
|
||||
{:ok, %{name: guild_name}} = GuildCache.get(guild_id)
|
||||
|
||||
RateLimiter.increase_counter(guild_id)
|
||||
Metrics.increase_request_count(guild_id, guild_name)
|
||||
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: "Species Not Found"
|
||||
})
|
||||
|
||||
{:ok, %HTTPoison.Response{status_code: 429}} ->
|
||||
Logger.warning("Request limit exceeded for the PlantNet API")
|
||||
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: "Too Many Requests"
|
||||
})
|
||||
|
||||
{_, _} ->
|
||||
Logger.error("Internal server error when contacting the PlantNet API")
|
||||
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: "Internal Server Error"
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
defp cleanup_saved_images(saved_images) do
|
||||
Enum.map(saved_images, fn {:ok, filename} -> filename end)
|
||||
|> File.delete_files!()
|
||||
end
|
||||
end
|
||||
@@ -3,10 +3,11 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
||||
|
||||
use Nostrum.Consumer
|
||||
alias Nostrum.Api
|
||||
alias PlantIdDiscordBot.{Guild, RateLimiter, Metrics, FileServer.File, PlantNet.Parser}
|
||||
alias PlantIdDiscordBot.{Guild, RateLimiter, Metrics}
|
||||
alias PlantIdDiscordBot.PlantNet.Parser
|
||||
alias PlantIdDiscordBot.FileServer.File
|
||||
|
||||
# TODO reintroduce mocks
|
||||
# @api Application.compile_env(:plantid_discord_bot, :api)
|
||||
# @guild Application.compile_env(:plantid_discord_bot, :guild)
|
||||
@plantnet_api_base_url Application.compile_env(:plantid_discord_bot, :plantnet_api_base_url)
|
||||
@max_results Application.compile_env(:plantid_discord_bot, :max_results)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user