mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-23 04:26:57 +00:00
Projects and Diseases placeholder application commands (#17)
* fix module name in backup * save projects to persistent_term * retrieve projects on bot startup * remove commented out code * register project autocomplete * placeholder text for project * placeholder disease command
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
defmodule PlantIdDiscordBot.Cog.Diseases do
|
||||
@moduledoc """
|
||||
Functions for using the /diseases application command.
|
||||
"""
|
||||
|
||||
alias PlantIdDiscordBot.PlantNet.Projects
|
||||
@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}})
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,71 @@
|
||||
defmodule PlantIdDiscordBot.Cog.Projects do
|
||||
@moduledoc """
|
||||
Functions for using the /projects application command.
|
||||
"""
|
||||
|
||||
alias PlantIdDiscordBot.PlantNet.Projects
|
||||
@api Application.compile_env(:plantid_discord_bot, :api)
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# 1. AUTOCOMPLETE HANDLER
|
||||
# ---------------------------------------------------------
|
||||
def autocomplete(interaction) do
|
||||
# Extract what the user is typing (may be nil)
|
||||
[%{value: user_input}] = interaction.data.options
|
||||
user_input = user_input || ""
|
||||
|
||||
# Fetch your project list
|
||||
projects = Projects.retrieve_projects()
|
||||
|
||||
# Filter based on description
|
||||
suggestions =
|
||||
projects
|
||||
|> Enum.filter(fn p ->
|
||||
String.contains?(
|
||||
String.downcase(p["description"]),
|
||||
String.downcase(user_input)
|
||||
)
|
||||
end)
|
||||
|> Enum.take(25)
|
||||
|> Enum.map(fn p ->
|
||||
%{
|
||||
# what the user sees
|
||||
name: p["description"],
|
||||
# what your command receives
|
||||
value: p["id"]
|
||||
}
|
||||
end)
|
||||
|
||||
# Respond with autocomplete choices
|
||||
@api.create_interaction_response(interaction, %{
|
||||
type: 8,
|
||||
data: %{choices: suggestions}
|
||||
})
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# 2. FINAL COMMAND EXECUTION
|
||||
# ---------------------------------------------------------
|
||||
def projects(interaction) do
|
||||
# Extract selected project ID
|
||||
project_id =
|
||||
interaction.data.options
|
||||
|> Enum.find(&(&1.name == "project"))
|
||||
|> then(&(&1 && &1.value))
|
||||
|
||||
@api.create_interaction_response(interaction, %{
|
||||
type: 4,
|
||||
data: %{
|
||||
content:
|
||||
case project_id do
|
||||
nil ->
|
||||
"Please choose a project using autocomplete."
|
||||
|
||||
id ->
|
||||
# "You selected project: **#{id}**"
|
||||
"Identification by project coming soon."
|
||||
end
|
||||
}
|
||||
})
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user