mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-23 04:26:57 +00:00
info and help with test
This commit is contained in:
@@ -4,4 +4,5 @@ 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"}}}
|
||||
end
|
||||
|
||||
@@ -34,4 +34,19 @@ defmodule PlantIdDiscordBot do
|
||||
def handle_event({:INTERACTION_CREATE, %{data: %{name: "source"}} = interaction, _ws_state}) do
|
||||
Cog.Info.source(interaction)
|
||||
end
|
||||
|
||||
# Handle /invite command
|
||||
def handle_event({:INTERACTION_CREATE, %{data: %{name: "invite"}} = interaction, _ws_state}) do
|
||||
Cog.Info.invite(interaction)
|
||||
end
|
||||
|
||||
# Handle /help command
|
||||
def handle_event({:INTERACTION_CREATE, %{data: %{name: "help"}} = interaction, _ws_state}) do
|
||||
Cog.Info.help(interaction)
|
||||
end
|
||||
|
||||
# Handle /info command
|
||||
def handle_event({:INTERACTION_CREATE, %{data: %{name: "info"}} = interaction, _ws_state}) do
|
||||
Cog.Info.info(interaction)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
defmodule PlantIdDiscordBot.Cog.Info do
|
||||
use Nostrum.Consumer
|
||||
import Nostrum.Struct.Embed
|
||||
alias PlantIdDiscordBot.Utils
|
||||
|
||||
@api Application.compile_env(:plantid_discord_bot, :api)
|
||||
@source Application.compile_env(:plantid_discord_bot, :source)
|
||||
@invite Application.compile_env(:plantid_discord_bot, :invite)
|
||||
|
||||
@doc """
|
||||
Sends a link to the source code for this bot.
|
||||
@@ -10,4 +13,60 @@ defmodule PlantIdDiscordBot.Cog.Info do
|
||||
def source(interaction) do
|
||||
@api.create_interaction_response(interaction, %{type: 4, data: %{content: @source}})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an invite link for this bot.
|
||||
"""
|
||||
def invite(interaction) do
|
||||
message = "Invite the bot to your server:\n\n[Click Here](#{@invite})"
|
||||
@api.create_interaction_response(interaction, %{type: 4, data: %{content: message}})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Help menu for the bot.
|
||||
"""
|
||||
def help(interaction) do
|
||||
embed =
|
||||
%Nostrum.Struct.Embed{}
|
||||
|> put_title("Let's break this down a bit")
|
||||
|> put_description("Use the `/id` command and add up to 5 photos\n\n")
|
||||
|> put_author(
|
||||
"Plant ID Bot",
|
||||
"https://discordapp.com",
|
||||
"https://cdn.discordapp.com/embed/avatars/0.png"
|
||||
)
|
||||
|> put_footer(
|
||||
"Powered by Pl@ntNet API",
|
||||
"https://www.iona.edu/sites/default/files/2021-04/ancillary-images/green-flower.jpg"
|
||||
)
|
||||
|> put_field(
|
||||
"For best results:",
|
||||
"- all photos should be of the same plant\n- take photos of organs, not the whole plant\n- best results will be achieved by using a mixture of organs\n- use images at least 600x600px\n\n",
|
||||
true
|
||||
)
|
||||
|> put_field("/info", "for more commands")
|
||||
|
||||
response = %{type: 4, data: %{embeds: [embed]}}
|
||||
@api.create_interaction_response(interaction, response)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Information about the bot.
|
||||
"""
|
||||
def info(interaction) do
|
||||
{:ok, app_info} = @api.get_application_information()
|
||||
|
||||
embed =
|
||||
%Nostrum.Struct.Embed{}
|
||||
|> put_title(app_info.name)
|
||||
|> put_description("Stats etc.")
|
||||
|> put_color(0x1AAAE5)
|
||||
|> put_field("Server Count", app_info.approximate_guild_count, true)
|
||||
|> put_field("Uptime", Utils.get_uptime(), true)
|
||||
|> put_field("Latency", Utils.get_shard_latency(), true)
|
||||
|> put_footer("Made by #{app_info.owner.username}")
|
||||
|
||||
response = %{type: 4, data: %{embeds: [embed]}}
|
||||
@api.create_interaction_response(interaction, response)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
defmodule PlantIdDiscordBot.Cog.PlantNet do
|
||||
use Nostrum.Consumer
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
defmodule PlantIdDiscordBot.Utils do
|
||||
@start_time Application.compile_env(:plantid_discord_bot, :start_time)
|
||||
|
||||
def get_uptime() do
|
||||
DateTime.diff(DateTime.utc_now(), @start_time)
|
||||
|> PlantIdDiscordBot.Utils.Duration.sec_to_str()
|
||||
end
|
||||
|
||||
def get_shard_latency() do
|
||||
Nostrum.Util.get_all_shard_latencies()
|
||||
|> Map.get(0)
|
||||
|> to_string()
|
||||
|> Kernel.<>("ms")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
# https://rosettacode.org/wiki/Convert_seconds_to_compound_duration
|
||||
defmodule PlantIdDiscordBot.Utils.Duration do
|
||||
@minute 60
|
||||
@hour @minute * 60
|
||||
@day @hour * 24
|
||||
@week @day * 7
|
||||
@divisor [@week, @day, @hour, @minute, 1]
|
||||
|
||||
def sec_to_str(sec) do
|
||||
{_, [s, m, h, d, w]} =
|
||||
Enum.reduce(@divisor, {sec, []}, fn divisor, {n, acc} ->
|
||||
{rem(n, divisor), [div(n, divisor) | acc]}
|
||||
end)
|
||||
|
||||
["#{w}w", "#{d}d", "#{h}h", "#{m}m", "#{s}s"]
|
||||
|> Enum.reject(fn str -> String.starts_with?(str, "0") end)
|
||||
|> Enum.join(" ")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user