TopggEx.Api (topgg_ex v0.1.6)
View SourceTop.gg API Client for posting stats and fetching data.
This module provides a comprehensive client for interacting with the Top.gg API, allowing you to post bot statistics, retrieve bot information, check votes, and more.
Examples
iex> {:ok, api} = TopggEx.Api.new("your_topgg_token_here")
iex> {:ok, _stats} = TopggEx.Api.post_stats(api, %{server_count: 100})
{:ok, %{server_count: 100}}
Configuration
Before using this module, ensure you have a Finch instance running in your application supervision tree:
children = [
{Finch, name: :topgg_finch}
]
See also
Summary
Functions
Gets bot information from Top.gg.
Gets a list of bots from Top.gg.
Gets your bot's statistics from Top.gg.
Gets user information from Top.gg.
Gets recent unique users who have voted for your bot.
Checks whether or not a user has voted in the last 12 hours.
Checks whether or not the weekend multiplier is active.
Creates a new Top.gg API client instance.
Posts bot statistics to Top.gg.
Types
@type bot_info() :: map()
@type bot_stats_response() :: %{ optional(:server_count) => non_neg_integer(), :shards => [non_neg_integer()], optional(:shard_count) => non_neg_integer() }
@type bots_query() :: %{ optional(:limit) => non_neg_integer(), optional(:offset) => non_neg_integer(), optional(:search) => String.t() | map(), optional(:sort) => String.t(), optional(:fields) => [String.t()] | String.t() }
@type bots_response() :: %{ results: [bot_info()], limit: non_neg_integer(), offset: non_neg_integer(), count: non_neg_integer(), total: non_neg_integer() }
@type post_stats_body() :: %{ :server_count => non_neg_integer() | [non_neg_integer()], optional(:shard_count) => non_neg_integer(), optional(:shards) => [non_neg_integer()], optional(:shard_id) => non_neg_integer() }
@type snowflake() :: String.t()
@type user_info() :: map()
Functions
Gets bot information from Top.gg.
Returns {:ok, bot_info}
on success, or {:error, reason}
on failure.
Parameters
api
- API client instanceid
- Bot ID (snowflake)
Examples
iex> {:ok, bot} = TopggEx.Api.get_bot(api, "461521980492087297")
{:ok, %{"id" => "461521980492087297", "username" => "Shiro", ...}}
@spec get_bots(t(), bots_query() | nil) :: {:ok, bots_response()} | {:error, any()}
Gets a list of bots from Top.gg.
Returns {:ok, bots_response}
on success, or {:error, reason}
on failure.
Parameters
api
- API client instancequery
- Optional query parameters
Examples
# Finding by properties
iex> {:ok, response} = TopggEx.Api.get_bots(api, %{
...> search: %{username: "shiro"}
...> })
{:ok, %{results: [%{"id" => "461521980492087297", "username" => "Shiro", ...}], ...}}
# Restricting fields
iex> {:ok, response} = TopggEx.Api.get_bots(api, %{
...> fields: ["id", "username"]
...> })
{:ok, %{results: [%{"id" => "461521980492087297", "username" => "Shiro"}], ...}}
@spec get_stats(t()) :: {:ok, bot_stats_response()} | {:error, any()}
Gets your bot's statistics from Top.gg.
Returns {:ok, stats}
on success, or {:error, reason}
on failure.
Parameters
api
- API client instance
Examples
iex> {:ok, stats} = TopggEx.Api.get_stats(api)
{:ok, %{server_count: 28199, shard_count: 5, shards: [5000, 5000, 5000, 5000, 8199]}}
Gets user information from Top.gg.
Returns {:ok, user_info}
on success, or {:error, reason}
on failure.
Deprecated
This function is deprecated and no longer supported by Top.gg API v0.
Parameters
api
- API client instanceid
- User ID (snowflake)
Examples
iex> {:ok, user} = TopggEx.Api.get_user(api, "205680187394752512")
{:ok, %{"id" => "205680187394752512", "username" => "Xignotic", ...}}
@spec get_votes(t(), non_neg_integer() | nil) :: {:ok, [short_user()]} | {:error, any()}
Gets recent unique users who have voted for your bot.
Returns {:ok, [users]}
on success, or {:error, reason}
on failure.
Parameters
api
- API client instancepage
- Optional page number (each page has at most 100 voters)
Examples
iex> {:ok, voters} = TopggEx.Api.get_votes(api)
{:ok, [%{username: "Xignotic", id: "205680187394752512", avatar: "https://..."}, ...]}
iex> {:ok, voters} = TopggEx.Api.get_votes(api, 2)
{:ok, [%{username: "iara", id: "395526710101278721", avatar: "https://..."}, ...]}
Checks whether or not a user has voted in the last 12 hours.
Returns {:ok, boolean}
on success, or {:error, reason}
on failure.
Parameters
api
- API client instanceid
- User ID (snowflake)
Examples
iex> {:ok, voted?} = TopggEx.Api.has_voted(api, "205680187394752512")
{:ok, true}
Checks whether or not the weekend multiplier is active.
Returns {:ok, boolean}
on success, or {:error, reason}
on failure.
Parameters
api
- API client instance
Examples
iex> {:ok, is_weekend?} = TopggEx.Api.is_weekend(api)
{:ok, false}
@spec new(String.t(), api_options()) :: {:ok, t()} | {:error, String.t()}
Creates a new Top.gg API client instance.
Returns :ok
with the API client struct, or {:error, reason}
if the token is malformed.
Parameters
token
- Your Top.gg API tokenoptions
- Optional configuration map
Options
:finch_name
- Name of the Finch instance to use (default::topgg_finch
):base_url
- Base URL for the API (default:"https://top.gg/api"
)
Examples
iex> {:ok, api} = TopggEx.Api.new("your_token_here")
iex> {:ok, api} = TopggEx.Api.new("your_token_here", %{finch_name: :my_finch})
@spec post_stats(t(), post_stats_body()) :: {:ok, post_stats_body()} | {:error, any()}
Posts bot statistics to Top.gg.
Returns {:ok, stats}
on success, or {:error, reason}
if the server count
is missing, invalid, or the API request fails.
Parameters
api
- API client instancestats
- Stats map containing at least:server_count
Examples
# Posting server count as an integer
iex> {:ok, stats} = TopggEx.Api.post_stats(api, %{server_count: 28199})
{:ok, %{server_count: 28199}}
# Posting with shards (server_count as array)
iex> {:ok, stats} = TopggEx.Api.post_stats(api, %{server_count: [1000, 2000, 3000]})
{:ok, %{server_count: [1000, 2000, 3000]}}
# Posting with additional shard information
iex> {:ok, stats} = TopggEx.Api.post_stats(api, %{
...> server_count: 500,
...> shard_id: 0,
...> shard_count: 5
...> })
{:ok, %{server_count: 500, shard_id: 0, shard_count: 5}}