mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-23 04:26:57 +00:00
update error messages and get guild name
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
defmodule PlantIdDiscordBotTest.Mocks.Guild do
|
||||||
|
@moduledoc """
|
||||||
|
Mocks the Guild module.
|
||||||
|
"""
|
||||||
|
def get_guild_name!(guild_id),
|
||||||
|
do: %{guild_id: 1_002_507_312_159_797_318, guild_name: "Test Guild"}
|
||||||
|
end
|
||||||
@@ -3,11 +3,9 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
|
|
||||||
use Nostrum.Consumer
|
use Nostrum.Consumer
|
||||||
alias Nostrum.Api
|
alias Nostrum.Api
|
||||||
alias PlantIdDiscordBot.RateLimiter
|
alias PlantIdDiscordBot.{Guild, RateLimiter, Metrics}
|
||||||
alias PlantIdDiscordBot.FileServer.File
|
alias PlantIdDiscordBot.FileServer.File
|
||||||
alias PlantIdDiscordBot.PlantNet.Parser
|
alias PlantIdDiscordBot.PlantNet.Parser
|
||||||
alias Nostrum.Cache.GuildCache
|
|
||||||
alias PlantIdDiscordBot.Metrics
|
|
||||||
|
|
||||||
# TODO reintroduce mocks
|
# TODO reintroduce mocks
|
||||||
# @api Application.compile_env(:plantid_discord_bot, :api)
|
# @api Application.compile_env(:plantid_discord_bot, :api)
|
||||||
@@ -35,10 +33,16 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
|> Enum.map(fn attachment -> attachment.url end)
|
|> Enum.map(fn attachment -> attachment.url end)
|
||||||
|> File.download_and_save_files!()
|
|> File.download_and_save_files!()
|
||||||
rescue
|
rescue
|
||||||
# TODO add guild id and name into error
|
e ->
|
||||||
# TODO test by raising error
|
Logger.error(Exception.format(:error, e, __STACKTRACE__),
|
||||||
e in ArgumentError ->
|
guild_id: message.guild_id,
|
||||||
Api.create_message(message.channel_id, content: e.message)
|
guild_name: Guild.get_guild_name!(message.guild_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
Api.create_message(message.channel_id,
|
||||||
|
content: "An error has occured. Please try again later."
|
||||||
|
)
|
||||||
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -50,7 +54,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
|> get_response(message)
|
|> get_response(message)
|
||||||
rescue
|
rescue
|
||||||
e ->
|
e ->
|
||||||
Logger.error(Exception.format(:error, e, __STACKTRACE__))
|
Logger.error(Exception.format(:error, e, __STACKTRACE__),
|
||||||
|
guild_id: message.guild_id,
|
||||||
|
guild_name: Guild.get_guild_name!(message.guild_id)
|
||||||
|
)
|
||||||
|
|
||||||
Api.create_message(
|
Api.create_message(
|
||||||
message.channel_id,
|
message.channel_id,
|
||||||
@@ -76,9 +83,7 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
|
|
||||||
defp get_response(query_uri, message) do
|
defp get_response(query_uri, message) do
|
||||||
guild_id = message.guild_id
|
guild_id = message.guild_id
|
||||||
|
guild_name = get_guild_name!(guild_id)
|
||||||
# TODO move into its own function
|
|
||||||
{:ok, %{name: guild_name}} = GuildCache.get(guild_id)
|
|
||||||
|
|
||||||
case HTTPoison.get(query_uri) do
|
case HTTPoison.get(query_uri) do
|
||||||
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
|
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
|
||||||
@@ -95,7 +100,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
)
|
)
|
||||||
|
|
||||||
{:ok, %HTTPoison.Response{status_code: 401, body: body}} ->
|
{:ok, %HTTPoison.Response{status_code: 401, body: body}} ->
|
||||||
Logger.critical("Unauthorized request to PlantNet API: #{body}")
|
Logger.critical("Unauthorized request to PlantNet API: #{body}",
|
||||||
|
guild_id: guild_id,
|
||||||
|
guild_name: guild_name
|
||||||
|
)
|
||||||
|
|
||||||
Api.create_message(message.channel_id,
|
Api.create_message(message.channel_id,
|
||||||
content: "Unauthorized request to PlantNet API.",
|
content: "Unauthorized request to PlantNet API.",
|
||||||
@@ -120,7 +128,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
)
|
)
|
||||||
|
|
||||||
{_, _} ->
|
{_, _} ->
|
||||||
Logger.error("Internal server error when contacting the PlantNet API")
|
Logger.error("Internal server error when contacting the PlantNet API",
|
||||||
|
guild_id: guild_id,
|
||||||
|
guild_name: guild_name
|
||||||
|
)
|
||||||
|
|
||||||
Api.create_message(message.channel_id,
|
Api.create_message(message.channel_id,
|
||||||
content: "Internal Server Error",
|
content: "Internal Server Error",
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
defmodule PlantIdDiscordBot.Guild do
|
||||||
|
alias Nostrum.Cache.GuildCache
|
||||||
|
|
||||||
|
def get_guild_name!(guild_id) do
|
||||||
|
%{name: guild_name} = GuildCache.get!(guild_id)
|
||||||
|
guild_name
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
defmodule PlantIdDiscordBot.ErrorHandlingTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
test "do_identification/1 returns nil when an error occurs" do
|
||||||
|
message = PlantNetFixtures.Message.message()
|
||||||
|
PlantIdDiscordBot.Cog.PlantNetMessage.do_identification(message)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,5 +1,13 @@
|
|||||||
ExUnit.start()
|
ExUnit.start()
|
||||||
|
|
||||||
|
defmodule PlantNetFixtures.Message do
|
||||||
|
@message %{
|
||||||
|
guild_id: 1_002_507_312_159_797_318
|
||||||
|
}
|
||||||
|
|
||||||
|
def message(), do: @message
|
||||||
|
end
|
||||||
|
|
||||||
defmodule PlantNetFixtures do
|
defmodule PlantNetFixtures do
|
||||||
@plantnet_raw_response "{\"query\":{\"project\":\"all\",\"images\":[\"https://upload.wikimedia.org/wikipedia/commons/f/ff/Prunus_cerasifera_A.jpg\",\"https://le-jardin-de-pascal.com/2195113-large_default/prunus-cerasifera-atropurpurea-prunier-myrobolan-nigra.jpg\"],\"organs\":[\"auto\",\"auto\"],\"includeRelatedImages\":false,\"noReject\":false},\"language\":\"en\",\"preferedReferential\":\"k-world-flora\",\"bestMatch\":\"Prunus cerasifera Ehrh.\",\"results\":[{\"score\":0.87871,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus cerasifera\",\"scientificNameAuthorship\":\"Ehrh.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Cherry plum, myrobalan\",\"Cherry Plum\",\"Purple-leaf Plum\"],\"scientificName\":\"Prunus cerasifera Ehrh.\"},\"gbif\":{\"id\":\"3021730\"},\"powo\":{\"id\":\"729568-1\"},\"iucn\":{\"id\":\"172162\",\"category\":\"DD\"}},{\"score\":0.31668,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus × cistena\",\"scientificNameAuthorship\":\"N.E.Hansen ex Koehne\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Dwarf red-leaf plum\",\"Purple-leaf sand cherry\",\"Purple-leaved sand cherry\"],\"scientificName\":\"Prunus × cistena N.E.Hansen ex Koehne\"},\"gbif\":{\"id\":\"3022465\"},\"powo\":{\"id\":\"2959315-4\"}},{\"score\":0.01801,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus sargentii\",\"scientificNameAuthorship\":\"Rehder\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Sargent's cherry\",\"Northern Japanese hill cherry\",\"Sargent’s cherry\"],\"scientificName\":\"Prunus sargentii Rehder\"},\"gbif\":{\"id\":\"3020955\"},\"powo\":{\"id\":\"730239-1\"},\"iucn\":{\"id\":\"64127603\",\"category\":\"LC\"}},{\"score\":0.00896,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus × yedoensis\",\"scientificNameAuthorship\":\"Matsum.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Yoshino cherry\",\"Hybrid cherry\",\"Korean flowering cherry\"],\"scientificName\":\"Prunus × yedoensis Matsum.\"},\"gbif\":{\"id\":\"3021335\"},\"powo\":{\"id\":\"30119904-2\"}},{\"score\":0.00518,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus serrulata\",\"scientificNameAuthorship\":\"Lindl.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Japanese flowering cherry\",\"Japanese flowering cherry Kwanzan\",\"Tibetan Cherry\"],\"scientificName\":\"Prunus serrulata Lindl.\"},\"gbif\":{\"id\":\"3022609\"},\"powo\":{\"id\":\"730268-1\"},\"iucn\":{\"id\":\"217170511\",\"category\":\"LC\"}}],\"version\":\"2024-11-19 (7.3)\",\"remainingIdentificationRequests\":488}"
|
@plantnet_raw_response "{\"query\":{\"project\":\"all\",\"images\":[\"https://upload.wikimedia.org/wikipedia/commons/f/ff/Prunus_cerasifera_A.jpg\",\"https://le-jardin-de-pascal.com/2195113-large_default/prunus-cerasifera-atropurpurea-prunier-myrobolan-nigra.jpg\"],\"organs\":[\"auto\",\"auto\"],\"includeRelatedImages\":false,\"noReject\":false},\"language\":\"en\",\"preferedReferential\":\"k-world-flora\",\"bestMatch\":\"Prunus cerasifera Ehrh.\",\"results\":[{\"score\":0.87871,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus cerasifera\",\"scientificNameAuthorship\":\"Ehrh.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Cherry plum, myrobalan\",\"Cherry Plum\",\"Purple-leaf Plum\"],\"scientificName\":\"Prunus cerasifera Ehrh.\"},\"gbif\":{\"id\":\"3021730\"},\"powo\":{\"id\":\"729568-1\"},\"iucn\":{\"id\":\"172162\",\"category\":\"DD\"}},{\"score\":0.31668,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus × cistena\",\"scientificNameAuthorship\":\"N.E.Hansen ex Koehne\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Dwarf red-leaf plum\",\"Purple-leaf sand cherry\",\"Purple-leaved sand cherry\"],\"scientificName\":\"Prunus × cistena N.E.Hansen ex Koehne\"},\"gbif\":{\"id\":\"3022465\"},\"powo\":{\"id\":\"2959315-4\"}},{\"score\":0.01801,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus sargentii\",\"scientificNameAuthorship\":\"Rehder\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Sargent's cherry\",\"Northern Japanese hill cherry\",\"Sargent’s cherry\"],\"scientificName\":\"Prunus sargentii Rehder\"},\"gbif\":{\"id\":\"3020955\"},\"powo\":{\"id\":\"730239-1\"},\"iucn\":{\"id\":\"64127603\",\"category\":\"LC\"}},{\"score\":0.00896,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus × yedoensis\",\"scientificNameAuthorship\":\"Matsum.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Yoshino cherry\",\"Hybrid cherry\",\"Korean flowering cherry\"],\"scientificName\":\"Prunus × yedoensis Matsum.\"},\"gbif\":{\"id\":\"3021335\"},\"powo\":{\"id\":\"30119904-2\"}},{\"score\":0.00518,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus serrulata\",\"scientificNameAuthorship\":\"Lindl.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Japanese flowering cherry\",\"Japanese flowering cherry Kwanzan\",\"Tibetan Cherry\"],\"scientificName\":\"Prunus serrulata Lindl.\"},\"gbif\":{\"id\":\"3022609\"},\"powo\":{\"id\":\"730268-1\"},\"iucn\":{\"id\":\"217170511\",\"category\":\"LC\"}}],\"version\":\"2024-11-19 (7.3)\",\"remainingIdentificationRequests\":488}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user