request limiter

This commit is contained in:
Owen
2024-12-15 22:34:59 +01:00
parent 7cf2e79c3e
commit 81c95c28b2
12 changed files with 177 additions and 48 deletions
+46 -7
View File
@@ -63,15 +63,16 @@ defmodule PlantIdDiscordBot.Cog.Info do
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_description("This bot is designed to help you identify plants")
|> put_color(0x41C03F)
|> put_field("/info", "Show this message", true)
|> put_field("/help", "Gives detailed help on the plant ID bot", true)
|> put_field("/stats", "Bot stats", true)
|> put_field("/invite", "Invite the bot to your server", true)
|> put_field("/source", "Link to the source code", true)
|> put_footer("Made by #{app_info.owner.username}")
response = %{type: 4, data: %{embeds: [embed]}}
@api.create_interaction_response(interaction, response)
@api.create_interaction_response(interaction, %{type: 4, data: %{embeds: [embed]}})
end
@doc """
@@ -89,4 +90,42 @@ defmodule PlantIdDiscordBot.Cog.Info do
@api.create_interaction_response(interaction, %{type: 4, data: %{content: message}})
end
@doc """
Bot stats.
"""
def stats(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}")
@api.create_interaction_response(interaction, %{type: 4, data: %{embeds: [embed]}})
end
@doc """
List all servers the bot is connected to.
"""
def servers(interaction) do
{:ok, %{owner: owner}} = @api.get_application_information()
guilds = PlantIdDiscordBot.Utils.get_guilds_names()
guilds_string = Enum.join(guilds, "\n")
embed =
%Nostrum.Struct.Embed{}
|> put_title("Connected on #{length(guilds)} server(s):")
|> put_description(guilds_string)
|> put_color(0x1AAAE5)
|> put_footer("Made by #{owner.username}")
@api.create_interaction_response(interaction, %{type: 4, data: %{embeds: [embed]}})
end
end
+46
View File
@@ -1,3 +1,49 @@
defmodule PlantIdDiscordBot.Cog.PlantNet do
use Nostrum.Consumer
alias PlantIdDiscordBot.RateLimiter
@api Application.compile_env(:plantid_discord_bot, :api)
@doc """
ID a plant from up to 5 images of organs.
"""
def id(interaction) do
guild_id = interaction.guild_id
case RateLimiter.check_limit(guild_id) do
{:limit_exceeded, value} ->
@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
# TODO increase on success (failed id or otherwise)
# RateLimiter.increase_counter(guild_id)
end
defp do_identification(interaction) do
attachment_urls = get_attachment_urls(interaction)
original_images = get_original_images(attachment_urls)
@api.create_interaction_response(interaction, %{
type: 4,
data: %{content: "This command is not yet implemented." <> "\n#{original_images}"}
})
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")
end