mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-23 04:26:57 +00:00
Feature/diseases (#19)
* add disease photo options * basic disease application command, no real parsing other than map to string
This commit is contained in:
@@ -3,14 +3,34 @@ defmodule PlantIdDiscordBot.Cog.Diseases do
|
||||
Functions for using the /diseases application command.
|
||||
"""
|
||||
|
||||
alias PlantIdDiscordBot.PlantNet.Projects
|
||||
alias PlantIdDiscordBot.Cog.PlantNetMessage
|
||||
|
||||
@api Application.compile_env(:plantid_discord_bot, :api)
|
||||
|
||||
@doc """
|
||||
Sends an invite link for this bot.
|
||||
"""
|
||||
def diseases(interaction) do
|
||||
message = "Diseases coming soon."
|
||||
@api.create_interaction_response(interaction, %{type: 4, data: %{content: message}})
|
||||
# 1. Acknowledge immediately so the token doesn't expire
|
||||
@api.create_interaction_response(interaction, %{
|
||||
type: 4,
|
||||
data: %{content: "Analyzing plant health from photos..."}
|
||||
})
|
||||
|
||||
resolved = Map.get(interaction.data, :resolved)
|
||||
attachments_map = (resolved && Map.get(resolved, :attachments)) || %{}
|
||||
options = interaction.data.options || []
|
||||
|
||||
images =
|
||||
options
|
||||
|> Enum.filter(fn opt -> String.starts_with?(opt.name, "image") end)
|
||||
|> Enum.map(fn opt -> Map.get(attachments_map, opt.value) end)
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|
||||
message = %{
|
||||
guild_id: interaction.guild_id,
|
||||
channel_id: interaction.channel_id,
|
||||
id: nil,
|
||||
attachments: images
|
||||
}
|
||||
|
||||
PlantNetMessage.id(message, "diseases")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -88,13 +88,17 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
||||
end
|
||||
end
|
||||
|
||||
defp get_response(query_uri, message) do
|
||||
defp get_response(query_uri, message, interaction_type \\ nil) do
|
||||
guild_id = message.guild_id
|
||||
guild_name = Guild.get_guild_name!(guild_id)
|
||||
|
||||
case HTTPoison.get(query_uri) do
|
||||
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
|
||||
response_message = Parser.parse(body)
|
||||
response_message =
|
||||
case interaction_type do
|
||||
"diseases" -> Parser.parse_disease_response(body)
|
||||
_ -> Parser.parse(body)
|
||||
end
|
||||
|
||||
RateLimiter.increase_counter(guild_id)
|
||||
Metrics.increase_request_count(guild_id, guild_name)
|
||||
@@ -107,7 +111,7 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
||||
guild_name: guild_name
|
||||
)
|
||||
|
||||
send_message("Unauthorizes requesnt to PlantNet API", message.channel_id, message.id)
|
||||
send_message("Unauthorized requesnt to PlantNet API", message.channel_id, message.id)
|
||||
|
||||
{:ok, %HTTPoison.Response{status_code: 404}} ->
|
||||
RateLimiter.increase_counter(guild_id)
|
||||
|
||||
Reference in New Issue
Block a user