From 20f2a0228e3fd225ab11d21a902d29ab2e7e3120 Mon Sep 17 00:00:00 2001 From: Dustin Rouillard Date: Thu, 11 Jun 2026 18:52:01 -0600 Subject: [PATCH 1/5] Add slash cmds, switch to embeds, and add terms/privacy --- lib/api/router.ex | 9 ++ lib/api/routes/legal.ex | 268 ++++++++++++++++++++++++++++++++ lib/bot/command_cache.ex | 30 ++++ lib/bot/command_handler.ex | 57 +++++-- lib/bot/command_registration.ex | 71 +++++++++ lib/bot/commands/about.ex | 30 ++++ lib/bot/commands/api_key.ex | 89 ++++++++--- lib/bot/commands/del.ex | 87 +++++++++-- lib/bot/commands/get.ex | 124 +++++++++++---- lib/bot/commands/help.ex | 28 ++-- lib/bot/commands/kv.ex | 65 +++++++- lib/bot/commands/set.ex | 119 +++++++++++--- lib/bot/discord_api.ex | 37 +++++ lib/bot/embed.ex | 11 ++ lib/bot/interaction_handler.ex | 49 ++++++ lib/gateway/client.ex | 24 ++- lib/gateway/utility.ex | 1 + lib/lanyard.ex | 1 + 18 files changed, 976 insertions(+), 124 deletions(-) create mode 100644 lib/api/routes/legal.ex create mode 100644 lib/bot/command_cache.ex create mode 100644 lib/bot/command_registration.ex create mode 100644 lib/bot/commands/about.ex create mode 100644 lib/bot/embed.ex create mode 100644 lib/bot/interaction_handler.ex diff --git a/lib/api/router.ex b/lib/api/router.ex index 5707939..d28d1db 100644 --- a/lib/api/router.ex +++ b/lib/api/router.ex @@ -3,6 +3,7 @@ defmodule Lanyard.Api.Router do alias Lanyard.Api.Routes.V1 alias Lanyard.Api.Routes.Discord + alias Lanyard.Api.Routes.Legal alias Lanyard.Api.Routes.Metrics alias Lanyard.Api.Util alias Lanyard.Api.Quicklinks @@ -106,6 +107,14 @@ defmodule Lanyard.Api.Router do end end + get "/privacy" do + Legal.privacy(conn) + end + + get "/terms" do + Legal.terms(conn) + end + forward("/v1", to: V1) forward("/discord", to: Discord) forward("/metrics", to: Metrics) diff --git a/lib/api/routes/legal.ex b/lib/api/routes/legal.ex new file mode 100644 index 0000000..3832f27 --- /dev/null +++ b/lib/api/routes/legal.ex @@ -0,0 +1,268 @@ +defmodule Lanyard.Api.Routes.Legal do + import Plug.Conn + + @last_updated "2026-06-11" + + @subtitle "Lanyard — public presence & profile API for members of the Lanyard Discord server" + + @style """ + :root { color-scheme: dark; } + * { box-sizing: border-box; } + body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif; + background: #0d0f12; + color: #e6e8eb; + line-height: 1.6; + } + main { + max-width: 760px; + margin: 0 auto; + padding: 64px 24px 96px; + } + header { + margin-bottom: 32px; + padding-bottom: 16px; + border-bottom: 1px solid #1f2329; + } + header h1 { + margin: 0 0 8px; + font-size: 28px; + font-weight: 600; + } + header p { + margin: 0; + color: #8b9098; + font-size: 14px; + } + header .updated { + margin-top: 8px; + color: #6c7178; + font-size: 13px; + } + h2 { + margin: 32px 0 12px; + font-size: 18px; + font-weight: 600; + color: #f4f6f8; + } + p, li { color: #c4c8cd; font-size: 15px; } + ul { padding-left: 20px; } + a { color: #7aa7ff; text-decoration: none; } + a:hover { text-decoration: underline; } + footer { + margin-top: 48px; + padding-top: 16px; + border-top: 1px solid #1f2329; + color: #6c7178; + font-size: 13px; + } + """ + + @terms_body """ +

+ These terms cover your use of the Lanyard service — + the public API and WebSocket at + api.lanyard.rest that exposes + Discord presence, activity, and user-set key/value data for members of + the Lanyard Discord server. +

+ +

Scope

+

+ The Lanyard service only collects presence and activity for users who + are members of the Lanyard Discord server. Presence and activity from + any other Discord server you are in is never collected. If you are not + a member of the Lanyard server, no data about you is collected or + exposed by the service. +

+ +

What the service does

+ + +

Public exposure

+

+ The Lanyard API is, by design, publicly readable: anyone who knows + your Discord user ID can query your presence, activity, and KV data + through it. That public exposure is the purpose of the service. + By participating — that is, by being a member of the Lanyard + Discord server — you acknowledge and accept that this data is + publicly available for the duration of your membership. KV entries + you create are also public; do not put anything sensitive in them. +

+ +

Using the service

+

+ Lanyard is provided as-is, with no guarantee of uptime, availability, + or accuracy. Your use of the API is also subject to Discord's + Terms of Service. +

+ +

Acceptable use

+ + +

Termination

+

+ We may restrict your access to the API or KV system at any time, for + any reason, particularly for violations of these terms. Leaving the + Lanyard Discord server stops further presence collection and removes + user-set KV data (see the Privacy Policy). +

+ +

Changes

+

+ These terms may be updated from time to time. Continued use of the + service after changes constitutes acceptance of the updated terms. +

+ """ + + @privacy_body """ +

+ This policy describes what data the Lanyard service + — the public presence and profile API at + api.lanyard.rest — processes + for members of the Lanyard Discord server, why, and + how long it is kept. +

+ +

Scope of collection

+

+ Lanyard only collects presence and activity data while you are a + member of the Lanyard Discord server, and only as reported by Discord + to that server. Presence in other Discord servers you are in is never + collected. If you are not a member of the Lanyard server, the service + holds no data about you. +

+ +

What we process

+ + +

How it's used

+ + +

Sharing & public exposure

+

+ Presence, activity, profile fields, and KV data exposed via the + Lanyard API are publicly readable by anyone who knows your Discord + user ID. That is the purpose of the service. We do not sell data + and do not share it with third parties beyond what is required to + run the service (Discord, Cloudflare). Because the API is public, + we cannot control who consumes it once data has been served. +

+ +

Retention & deletion

+

+ Presence and activity data is ephemeral — it only exists while + you are a member of the Lanyard server and connected to Discord, and + is not retained as historical records. User-set KV data persists + until you remove it or until you leave the Lanyard Discord server, + at which point all data set by you or provided by you to the service + is deleted. +

+ +

Your choices

+ + +

Contact

+

+ Questions about this policy can be raised in the Lanyard Discord server. +

+ """ + + def terms(conn) do + render(conn, + title: "Terms of Service", + document_title: "Terms of Service — Lanyard", + active_path: "/terms", + body: @terms_body + ) + end + + def privacy(conn) do + render(conn, + title: "Privacy Policy", + document_title: "Privacy Policy — Lanyard", + active_path: "/privacy", + body: @privacy_body + ) + end + + defp render(conn, opts) do + conn + |> put_resp_content_type("text/html; charset=utf-8") + |> send_resp(200, page(opts)) + end + + defp page(opts) do + """ + + + + + + #{opts[:document_title]} + + + +
+
+

#{opts[:title]}

+

#{@subtitle}

+

Last updated: #{@last_updated}

+
+ #{opts[:body]} + #{footer(opts[:active_path])} +
+ + + """ + end + + defp footer(active_path) do + """ + + """ + end + + defp link(href, label, active_path) when href == active_path, do: "#{label}" + defp link(href, label, _active_path), do: ~s(#{label}) +end diff --git a/lib/bot/command_cache.ex b/lib/bot/command_cache.ex new file mode 100644 index 0000000..08369f2 --- /dev/null +++ b/lib/bot/command_cache.ex @@ -0,0 +1,30 @@ +defmodule Lanyard.DiscordBot.CommandCache do + @table :slash_commands + + def init do + :ets.new(@table, [:named_table, :set, :public]) + end + + def put_many(commands) when is_list(commands) do + Enum.each(commands, fn + %{"name" => name, "id" => id} -> :ets.insert(@table, {name, id}) + _ -> :ok + end) + end + + def get_id(name) do + case :ets.lookup(@table, name) do + [{^name, id}] -> id + _ -> nil + end + rescue + ArgumentError -> nil + end + + def mention(name) do + case get_id(name) do + nil -> "`/#{name}`" + id -> "" + end + end +end diff --git a/lib/bot/command_handler.ex b/lib/bot/command_handler.ex index 3eaf7a5..8a1e25e 100644 --- a/lib/bot/command_handler.ex +++ b/lib/bot/command_handler.ex @@ -1,37 +1,66 @@ defmodule Lanyard.DiscordBot.CommandHandler do + alias Lanyard.DiscordBot.CommandCache + alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed + @command_map %{ "get" => Lanyard.DiscordBot.Commands.Get, "set" => Lanyard.DiscordBot.Commands.Set, "del" => Lanyard.DiscordBot.Commands.Del, "apikey" => Lanyard.DiscordBot.Commands.ApiKey, "kv" => Lanyard.DiscordBot.Commands.KV, - "help" => Lanyard.DiscordBot.Commands.Help + "help" => Lanyard.DiscordBot.Commands.Help, + "about" => Lanyard.DiscordBot.Commands.About } def handle_message(payload) do case payload.data do - # Don't handle messages from other bots %{"author" => %{"bot" => true}} -> :ok - %{"content" => content} -> - if String.starts_with?(content, Application.get_env(:lanyard, :command_prefix)) do - [attempted_command | args] = - content - |> String.to_charlist() - |> tl() - |> to_string() - |> String.split(" ") - - unless @command_map[attempted_command] == nil do - @command_map[attempted_command].handle(args, payload.data) - end + %{"content" => content} = data -> + prefix = Application.get_env(:lanyard, :command_prefix) + + cond do + String.starts_with?(content, prefix) -> handle_prefix_command(content, data) + dm?(data) -> send_dm_hint(data["channel_id"]) + true -> :ok end + %{"channel_id" => channel_id} = data -> + if dm?(data), do: send_dm_hint(channel_id) + _ -> :ok end end def handle_command(_unknown_command, _args), do: :ok + + defp handle_prefix_command(content, data) do + [attempted_command | args] = + content + |> String.to_charlist() + |> tl() + |> to_string() + |> String.split(" ") + + unless @command_map[attempted_command] == nil do + @command_map[attempted_command].handle(args, data) + end + end + + defp dm?(data), do: is_nil(data["guild_id"]) + + defp send_dm_hint(channel_id) do + embed = + Embed.info(%{ + title: ":wave: Hi! I'm Lanyard", + description: + "Looking for your API key? Use #{CommandCache.mention("apikey")} — the response is private to you.\n\n**Other slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("about")} — privacy policy & terms of service", + footer: %{text: "Type / in any channel to see my commands"} + }) + + DiscordApi.send_message(channel_id, embed) + end end diff --git a/lib/bot/command_registration.ex b/lib/bot/command_registration.ex new file mode 100644 index 0000000..a28994c --- /dev/null +++ b/lib/bot/command_registration.ex @@ -0,0 +1,71 @@ +defmodule Lanyard.DiscordBot.CommandRegistration do + require Logger + + alias Lanyard.DiscordBot.CommandCache + alias Lanyard.DiscordBot.InteractionHandler + + @api_host "https://discord.com/api/v9" + + def register(application_id) do + definitions = InteractionHandler.command_definitions() + + result = + :put + |> Finch.build( + "#{@api_host}/applications/#{application_id}/commands", + [ + {"Authorization", "Bot " <> Application.get_env(:lanyard, :bot_token)}, + {"Content-Type", "application/json"} + ], + Jason.encode!(definitions) + ) + |> Finch.request(Lanyard.Finch) + + case result do + {:ok, %{status: status, body: body}} when status in 200..299 -> + cache_from_body(body) + Logger.info("Discord: Registered #{length(definitions)} slash commands") + :ok + + {:ok, %{status: status, body: body}} -> + Logger.error("Discord: Failed to register slash commands (status #{status}): #{body}") + fetch(application_id) + :error + + {:error, reason} -> + Logger.error("Discord: Failed to register slash commands: #{inspect(reason)}") + fetch(application_id) + :error + end + end + + def fetch(application_id) do + result = + :get + |> Finch.build( + "#{@api_host}/applications/#{application_id}/commands", + [{"Authorization", "Bot " <> Application.get_env(:lanyard, :bot_token)}] + ) + |> Finch.request(Lanyard.Finch) + + case result do + {:ok, %{status: status, body: body}} when status in 200..299 -> + cache_from_body(body) + :ok + + _ -> + Logger.error("Discord: Failed to fetch slash commands: #{inspect(result)}") + :error + end + end + + defp cache_from_body(body) do + case Jason.decode(body) do + {:ok, commands} when is_list(commands) -> + CommandCache.put_many(commands) + + _ -> + :ok + end + end +end diff --git a/lib/bot/commands/about.ex b/lib/bot/commands/about.ex new file mode 100644 index 0000000..60f8afe --- /dev/null +++ b/lib/bot/commands/about.ex @@ -0,0 +1,30 @@ +defmodule Lanyard.DiscordBot.Commands.About do + alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed + + def definition do + %{ + name: "about", + description: "View Lanyard's privacy policy and terms of service" + } + end + + def handle(_, payload) do + DiscordApi.send_message(payload["channel_id"], embed()) + end + + def handle_interaction(interaction) do + DiscordApi.respond_to_interaction(interaction["id"], interaction["token"], embed()) + end + + defp embed do + base = Application.get_env(:lanyard, :external_url) + + Embed.info(%{ + title: "Lanyard", + description: + "Discord presence as an API. Embed your status on websites, dashboards, and more.\n\n:lock: **Privacy Policy:** #{base}/privacy\n:scroll: **Terms of Service:** #{base}/terms", + footer: %{text: "lanyard.rest"} + }) + end +end diff --git a/lib/bot/commands/api_key.ex b/lib/bot/commands/api_key.ex index 194df86..fcb3a59 100644 --- a/lib/bot/commands/api_key.ex +++ b/lib/bot/commands/api_key.ex @@ -1,25 +1,42 @@ defmodule Lanyard.DiscordBot.Commands.ApiKey do alias Lanyard.Connectivity.Redis alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed + + def definition do + %{ + name: "apikey", + description: "Generate (or regenerate) your private Lanyard API key" + } + end def handle(_, %{"channel_id" => channel_id, "guild_id" => _guild_id} = _p) do - DiscordApi.send_message(channel_id, ":x: You can only perform this command in DMs with me") + embed = + Embed.error(%{ + title: ":x: DM only", + description: "You can only perform this command in DMs with me." + }) + + DiscordApi.send_message(channel_id, embed) end def handle(_, payload) do user_id = payload["author"]["id"] key = generate_api_key(user_id) - Lanyard.DiscordBot.DiscordApi.send_message(payload["channel_id"], %{ - title: "Lanyard API Key", - description: - "**Absolutely do not share or post this key anywhere, it is a secret key that will allow anyone to manage your Lanyard K/V**\n\n**This key is not to be used in a front-end application/website**\n\nIf you are looking for the public endpoint for your data, you would use your discord user ID like so\n#{Application.get_env(:lanyard, :external_url)}/v1/users/#{user_id}", - color: 0x5865F2, - footer: %{text: "Run this command again if you need to re-generate your key"}, - fields: [ - %{name: "Key", value: "||`#{key}`||\n-# *click above to reveal*", inline: false} - ] - }) + DiscordApi.send_message(payload["channel_id"], key_embed(user_id, key, ephemeral?: false)) + end + + def handle_interaction(interaction) do + user_id = interaction_user_id(interaction) + key = generate_api_key(user_id) + + DiscordApi.respond_to_interaction( + interaction["id"], + interaction["token"], + key_embed(user_id, key, ephemeral?: true), + ephemeral: true + ) end def contains_api_key?(user_id, candidates) do @@ -30,20 +47,12 @@ defmodule Lanyard.DiscordBot.Commands.ApiKey do |> Enum.any?(&(&1 == stored_key)) end - def generate_and_send_new(original_channel, user_id) do - DiscordApi.send_message( - original_channel, - ":x: Whoops, you just posted your API key, this is meant to stay private, regenerating this for you, check your DM" - ) - - key = generate_api_key(user_id) - - dm_channel = DiscordApi.create_dm(user_id) - - DiscordApi.send_message( - dm_channel, - ":repeat: **We've regenerated your api key as you used it in a K/V command.**\nYour new Lanyard API key is `#{key}`\n\n**ABSOLUTELY DO NOT SHARE OR POST THIS KEY ANYWHERE IT WILL ALLOW ANYONE TO MANAGE YOUR LANYARD K/V**\n*Run `.apikey` in this DM if you need to re-generate your key*" - ) + def leak_warning_embed do + Embed.warn(%{ + title: ":warning: That looks like your API key", + description: + "Your private Lanyard API key isn't a K/V key or value — it's a separate credential for the HTTP API and shouldn't be pasted as a command argument.\n\nIf you think the key has been exposed and want to rotate it, run #{Lanyard.DiscordBot.CommandCache.mention("apikey")}." + }) end def generate_api_key(user_id) do @@ -59,4 +68,34 @@ defmodule Lanyard.DiscordBot.Commands.ApiKey do key end + + defp key_embed(user_id, key, opts) do + base = Application.get_env(:lanyard, :external_url) + ephemeral? = Keyword.get(opts, :ephemeral?, false) + + save_line = + if ephemeral? do + "**Save this key now — this message is only visible to you and won't stick around.** You can always run #{Lanyard.DiscordBot.CommandCache.mention("apikey")} to generate a new one, but the old one will stop working." + else + "**Save this key somewhere safe.** You can always run #{Lanyard.DiscordBot.CommandCache.mention("apikey")} to generate a new one, but the old one will stop working." + end + + Embed.info(%{ + title: "Lanyard API Key", + description: + "#{save_line}\n\n**Do not share or post this key anywhere — it lets anyone manage your Lanyard K/V.**\n\nThis key is not meant for front-end applications. For the public read endpoint, use your Discord user ID:\n#{base}/v1/users/#{user_id}", + fields: [ + %{name: "Key", value: "||`#{key}`||\n-# *click to reveal*", inline: false} + ], + footer: %{text: "Run /apikey again if you need to regenerate"} + }) + end + + defp interaction_user_id(interaction) do + cond do + user = interaction["user"] -> user["id"] + member = interaction["member"] -> member["user"]["id"] + true -> nil + end + end end diff --git a/lib/bot/commands/del.ex b/lib/bot/commands/del.ex index 15cd1a9..070d41c 100644 --- a/lib/bot/commands/del.ex +++ b/lib/bot/commands/del.ex @@ -1,27 +1,88 @@ defmodule Lanyard.DiscordBot.Commands.Del do + alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed alias Lanyard.DiscordBot.Commands.ApiKey + alias Lanyard.DiscordBot.InteractionHandler - def handle([key], payload) do - if ApiKey.contains_api_key?(payload["author"]["id"], key) do - ApiKey.generate_and_send_new(payload["channel_id"], payload["author"]["id"]) - else - Lanyard.KV.Interface.del(payload["author"]["id"], key) + def definition do + %{ + name: "del", + description: "Delete a key from your Lanyard K/V store", + options: [ + %{name: "key", description: "The key to delete", type: 3, required: true} + ] + } + end + + def handle(args, payload) do + user_id = payload["author"]["id"] + channel_id = payload["channel_id"] + + cond do + ApiKey.contains_api_key?(user_id, args) -> + DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) - DiscordApi.send_message(payload["channel_id"], ":white_check_mark: Deleted key: `#{key}`") + dm?(payload) -> + embed = + case args do + [key] -> kv_del_embed(user_id, key) + _ -> usage_embed() + end + + DiscordApi.send_message(channel_id, embed) + + true -> + DiscordApi.send_message(channel_id, moved_embed("del")) end + + :ok end - def handle(args, payload) do - if ApiKey.contains_api_key?(payload["author"]["id"], args) do - ApiKey.generate_and_send_new(payload["channel_id"], payload["author"]["id"]) + def handle_interaction(interaction) do + user_id = InteractionHandler.user_id(interaction) + key = InteractionHandler.option(interaction, "key") + interaction_id = interaction["id"] + interaction_token = interaction["token"] + + if ApiKey.contains_api_key?(user_id, key) do + DiscordApi.respond_to_interaction( + interaction_id, + interaction_token, + ApiKey.leak_warning_embed(), + ephemeral: true + ) else - DiscordApi.send_message( - payload["channel_id"], - "Invalid usage. Example `del` command usage:\n`#{Application.get_env(:lanyard, :command_prefix)}del `" + DiscordApi.respond_to_interaction( + interaction_id, + interaction_token, + kv_del_embed(user_id, key) ) end + end - :ok + defp kv_del_embed(user_id, key) do + Lanyard.KV.Interface.del(user_id, key) + + Embed.success(%{ + title: ":white_check_mark: Key Deleted", + description: "Deleted key `#{key}` from your K/V store." + }) + end + + defp usage_embed do + Embed.error(%{ + title: ":x: Invalid usage", + description: "Usage: `#{Application.get_env(:lanyard, :command_prefix)}del `" + }) + end + + defp dm?(payload), do: is_nil(payload["guild_id"]) + + defp moved_embed(name) do + Embed.info(%{ + title: ":sparkles: K/V commands have moved", + description: "Use #{CommandCache.mention(name)} instead." + }) end end diff --git a/lib/bot/commands/get.ex b/lib/bot/commands/get.ex index 7c34566..47b6970 100644 --- a/lib/bot/commands/get.ex +++ b/lib/bot/commands/get.ex @@ -1,46 +1,116 @@ defmodule Lanyard.DiscordBot.Commands.Get do + alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed alias Lanyard.DiscordBot.Commands.ApiKey + alias Lanyard.DiscordBot.InteractionHandler - def handle([key], payload) do - if ApiKey.contains_api_key?(payload["author"]["id"], key) do - ApiKey.generate_and_send_new(payload["channel_id"], payload["author"]["id"]) - else - case Lanyard.KV.Interface.get(payload["author"]["id"], key) do - {:ok, v} -> - DiscordApi.send_message( - payload["channel_id"], - response_content(key, v, payload["author"]["id"]) - ) - - {:error, msg} -> - DiscordApi.send_message(payload["channel_id"], ":x: #{msg}") - end - end + def definition do + %{ + name: "get", + description: "Get a value from your Lanyard K/V store", + options: [ + %{name: "key", description: "The key to get", type: 3, required: true} + ] + } end def handle(args, payload) do - if ApiKey.contains_api_key?(payload["author"]["id"], args) do - ApiKey.generate_and_send_new(payload["channel_id"], payload["author"]["id"]) - else - DiscordApi.send_message( - payload["channel_id"], - "Invalid usage. Example `get` command usage:\n`#{Application.get_env(:lanyard, :command_prefix)}get `" - ) + user_id = payload["author"]["id"] + channel_id = payload["channel_id"] + + cond do + ApiKey.contains_api_key?(user_id, args) -> + DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) + + dm?(payload) -> + embed = + case args do + [key] -> + case kv_get_embed(user_id, key) do + {_kind, e} -> e + end + + _ -> + usage_embed() + end + + DiscordApi.send_message(channel_id, embed) + + true -> + DiscordApi.send_message(channel_id, moved_embed("get")) end :ok end + def handle_interaction(interaction) do + user_id = InteractionHandler.user_id(interaction) + key = InteractionHandler.option(interaction, "key") + interaction_id = interaction["id"] + interaction_token = interaction["token"] + + if ApiKey.contains_api_key?(user_id, key) do + DiscordApi.respond_to_interaction( + interaction_id, + interaction_token, + ApiKey.leak_warning_embed(), + ephemeral: true + ) + else + case kv_get_embed(user_id, key) do + {:ok, embed} -> + DiscordApi.respond_to_interaction(interaction_id, interaction_token, embed) + + {:error, embed} -> + DiscordApi.respond_to_interaction(interaction_id, interaction_token, embed, ephemeral: true) + end + end + end + + @max_field_length 1_024 + + defp kv_get_embed(user_id, key) do + case Lanyard.KV.Interface.get(user_id, key) do + {:ok, v} -> + {:ok, + Embed.success(%{ + title: ":white_check_mark: Key Retrieved", + fields: [ + %{name: "Key", value: "`#{key}`", inline: false}, + %{name: "Value", value: value_field(key, v, user_id), inline: false} + ] + })} + + {:error, msg} -> + {:error, Embed.error(%{title: ":x: Error", description: msg})} + end + end + @doc false - def response_content(key, value, user_id) do - content = - ":white_check_mark: Key: `#{key}` | Value: ```#{String.replace(value, "`", "`\u200b")}```" + def value_field(key, value, user_id) do + content = "```#{String.replace(value, "`", "`\u200b")}```" - if DiscordApi.content_within_limit?(content) do + if String.length(content) <= @max_field_length do content else - ":x: The value for `#{key}` is too long to display in Discord.\nView it at #{Application.get_env(:lanyard, :external_url)}/v1/users/#{user_id}" + "The value for `#{key}` is too long to display in Discord.\nView it at #{Application.get_env(:lanyard, :external_url)}/v1/users/#{user_id}" end end + + defp usage_embed do + Embed.error(%{ + title: ":x: Invalid usage", + description: "Usage: `#{Application.get_env(:lanyard, :command_prefix)}get `" + }) + end + + defp dm?(payload), do: is_nil(payload["guild_id"]) + + defp moved_embed(name) do + Embed.info(%{ + title: ":sparkles: K/V commands have moved", + description: "Use #{CommandCache.mention(name)} instead." + }) + end end diff --git a/lib/bot/commands/help.ex b/lib/bot/commands/help.ex index b0304ca..9bcc55a 100644 --- a/lib/bot/commands/help.ex +++ b/lib/bot/commands/help.ex @@ -1,19 +1,29 @@ defmodule Lanyard.DiscordBot.Commands.Help do + alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed + + def definition do + %{ + name: "help", + description: "List Lanyard's commands" + } + end def handle(_, payload) do - prefix = Application.get_env(:lanyard, :command_prefix) + DiscordApi.send_message(payload["channel_id"], embed()) + end + + def handle_interaction(interaction) do + DiscordApi.respond_to_interaction(interaction["id"], interaction["token"], embed(), ephemeral: true) + end - DiscordApi.send_message(payload["channel_id"], %{ + defp embed do + Embed.info(%{ title: "Lanyard Bot Commands", - description: "Store and manage custom data on your Lanyard profile.", - color: 0x5865F2, + description: + "Store and manage custom data on your Lanyard profile.\n\n#{CommandCache.mention("get")} `` — get the value of a key\n#{CommandCache.mention("set")} ` ` — set a key to a value\n#{CommandCache.mention("del")} `` — delete a key\n#{CommandCache.mention("kv")} — list all of your current keys\n#{CommandCache.mention("apikey")} — get (or regenerate) your API key, only visible to you\n#{CommandCache.mention("about")} — privacy policy & terms of service", fields: [ - %{name: "`#{prefix}get `", value: "Get the value of a key", inline: false}, - %{name: "`#{prefix}set `", value: "Set a key to a value", inline: false}, - %{name: "`#{prefix}del `", value: "Delete a key", inline: false}, - %{name: "`#{prefix}kv`", value: "List all of your current keys", inline: false}, - %{name: "`#{prefix}apikey`", value: "Get (or regenerate) your Lanyard API key, sent via DM", inline: false}, %{ name: "Docs & Source", value: diff --git a/lib/bot/commands/kv.ex b/lib/bot/commands/kv.ex index 3a50f99..2853143 100644 --- a/lib/bot/commands/kv.ex +++ b/lib/bot/commands/kv.ex @@ -1,17 +1,66 @@ defmodule Lanyard.DiscordBot.Commands.KV do + alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed + alias Lanyard.DiscordBot.InteractionHandler + + def definition do + %{ + name: "kv", + description: "List the keys in your Lanyard K/V store" + } + end def handle(_, payload) do - kv = - Lanyard.KV.Interface.get_all(payload["author"]["id"]) - |> Enum.map(fn {k, _v} -> k end) - |> Enum.join(", ") + user_id = payload["author"]["id"] + channel_id = payload["channel_id"] - kv = if String.length(kv) > 0, do: kv, else: "No keys" + embed = + if dm?(payload), + do: kv_list_embed(user_id), + else: moved_embed() - DiscordApi.send_message( - payload["channel_id"], - "*`#{Application.get_env(:lanyard, :command_prefix)}get ` to get a value*\n*`#{Application.get_env(:lanyard, :command_prefix)}del ` to delete an existing key*\n*`#{Application.get_env(:lanyard, :command_prefix)}set ` to set a key*\n\n**Keys:** ```#{kv}```" + DiscordApi.send_message(channel_id, embed) + end + + def handle_interaction(interaction) do + user_id = InteractionHandler.user_id(interaction) + + DiscordApi.respond_to_interaction( + interaction["id"], + interaction["token"], + kv_list_embed(user_id) ) end + + defp kv_list_embed(user_id) do + keys = + Lanyard.KV.Interface.get_all(user_id) + |> Enum.map(fn {k, _v} -> k end) + + keys_value = + case keys do + [] -> "_No keys yet._" + list -> "```#{Enum.join(list, ", ")}```" + end + + Embed.info(%{ + title: ":file_folder: Your K/V Store", + description: + "#{CommandCache.mention("get")} `` — get a value\n#{CommandCache.mention("set")} ` ` — set a key\n#{CommandCache.mention("del")} `` — delete a key", + fields: [ + %{name: "Keys (#{length(keys)})", value: keys_value, inline: false} + ] + }) + end + + defp moved_embed do + Embed.info(%{ + title: ":sparkles: K/V commands have moved", + description: + "Use #{CommandCache.mention("kv")} to view your keys, #{CommandCache.mention("get")} to retrieve, #{CommandCache.mention("set")} to update, or #{CommandCache.mention("del")} to remove." + }) + end + + defp dm?(payload), do: is_nil(payload["guild_id"]) end diff --git a/lib/bot/commands/set.ex b/lib/bot/commands/set.ex index 1f74466..e922a67 100644 --- a/lib/bot/commands/set.ex +++ b/lib/bot/commands/set.ex @@ -1,41 +1,110 @@ defmodule Lanyard.DiscordBot.Commands.Set do + alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi + alias Lanyard.DiscordBot.Embed alias Lanyard.DiscordBot.Commands.ApiKey + alias Lanyard.DiscordBot.InteractionHandler - def handle([key | value_s], payload) when length(value_s) > 0 do - value = Enum.join(value_s, " ") + def definition do + %{ + name: "set", + description: "Set a key in your Lanyard K/V store", + options: [ + %{name: "key", description: "The key to set", type: 3, required: true}, + %{name: "value", description: "The value to store", type: 3, required: true} + ] + } + end - if ApiKey.contains_api_key?(payload["author"]["id"], [key, value]) do - ApiKey.generate_and_send_new(payload["channel_id"], payload["author"]["id"]) - else - case Lanyard.KV.Interface.set(payload["author"]["id"], key, value) do - {:error, reason} -> - DiscordApi.send_message( - payload["channel_id"], - ":x: #{reason}" - ) - - _ -> - DiscordApi.send_message( - payload["channel_id"], - ":white_check_mark: `#{key}` was set. View it with `#{Application.get_env(:lanyard, :command_prefix)}get #{key}` or go to #{Application.get_env(:lanyard, :external_url)}/v1/users/#{payload["author"]["id"]}" - ) - end + def handle(args, payload) do + user_id = payload["author"]["id"] + channel_id = payload["channel_id"] + + cond do + ApiKey.contains_api_key?(user_id, args) -> + DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) + + dm?(payload) -> + embed = + case args do + [key | value_parts] when value_parts != [] -> + case kv_set_embed(user_id, key, Enum.join(value_parts, " ")) do + {_kind, e} -> e + end + + _ -> + usage_embed() + end + + DiscordApi.send_message(channel_id, embed) + + true -> + DiscordApi.send_message(channel_id, moved_embed("set")) end :ok end - def handle(args, payload) do - if ApiKey.contains_api_key?(payload["author"]["id"], args) do - ApiKey.generate_and_send_new(payload["channel_id"], payload["author"]["id"]) - else - DiscordApi.send_message( - payload["channel_id"], - "Invalid usage. Example `set` command usage:\n`#{Application.get_env(:lanyard, :command_prefix)}set `" + def handle_interaction(interaction) do + user_id = InteractionHandler.user_id(interaction) + key = InteractionHandler.option(interaction, "key") + value = InteractionHandler.option(interaction, "value") + interaction_id = interaction["id"] + interaction_token = interaction["token"] + + if ApiKey.contains_api_key?(user_id, [key, value]) do + DiscordApi.respond_to_interaction( + interaction_id, + interaction_token, + ApiKey.leak_warning_embed(), + ephemeral: true ) + else + case kv_set_embed(user_id, key, value) do + {:ok, embed} -> + DiscordApi.respond_to_interaction(interaction_id, interaction_token, embed) + + {:error, embed} -> + DiscordApi.respond_to_interaction(interaction_id, interaction_token, embed, ephemeral: true) + end end :ok end + + defp kv_set_embed(user_id, key, value) do + case Lanyard.KV.Interface.set(user_id, key, value) do + {:error, reason} -> + {:error, Embed.error(%{title: ":x: Could not set key", description: reason})} + + _ -> + external_url = Application.get_env(:lanyard, :external_url) + profile_url = "#{external_url}/v1/users/#{user_id}" + + {:ok, + Embed.success(%{ + title: ":white_check_mark: Key Set", + description: "View it with #{CommandCache.mention("get")} or open [your profile](#{profile_url}).", + fields: [ + %{name: "Key", value: "`#{key}`", inline: true} + ] + })} + end + end + + defp usage_embed do + Embed.error(%{ + title: ":x: Invalid usage", + description: "Usage: `#{Application.get_env(:lanyard, :command_prefix)}set `" + }) + end + + defp dm?(payload), do: is_nil(payload["guild_id"]) + + defp moved_embed(name) do + Embed.info(%{ + title: ":sparkles: K/V commands have moved", + description: "Use #{CommandCache.mention(name)} instead." + }) + end end diff --git a/lib/bot/discord_api.ex b/lib/bot/discord_api.ex index 29002a6..d61f5f9 100644 --- a/lib/bot/discord_api.ex +++ b/lib/bot/discord_api.ex @@ -46,6 +46,43 @@ defmodule Lanyard.DiscordBot.DiscordApi do |> Kernel.<=(@max_content_length) end + def respond_to_interaction(interaction_id, interaction_token, content, opts \\ []) + + def respond_to_interaction(interaction_id, interaction_token, content, opts) + when is_binary(content) do + Lanyard.Metrics.Collector.inc(:counter, :lanyard_discord_messages_sent) + + sanitized_content = sanitize_content(content) + + data = %{content: sanitized_content} + data = if opts[:ephemeral], do: Map.put(data, :flags, 64), else: data + + :post + |> Finch.build( + "#{@api_host}/interactions/#{interaction_id}/#{interaction_token}/callback", + [{"Content-Type", "application/json"}], + Jason.encode!(%{type: 4, data: data}) + ) + |> Finch.request(Lanyard.Finch) + |> track_response() + end + + def respond_to_interaction(interaction_id, interaction_token, %{} = embed, opts) do + Lanyard.Metrics.Collector.inc(:counter, :lanyard_discord_messages_sent) + + data = %{embeds: [embed]} + data = if opts[:ephemeral], do: Map.put(data, :flags, 64), else: data + + :post + |> Finch.build( + "#{@api_host}/interactions/#{interaction_id}/#{interaction_token}/callback", + [{"Content-Type", "application/json"}], + Jason.encode!(%{type: 4, data: data}) + ) + |> Finch.request(Lanyard.Finch) + |> track_response() + end + def create_dm(recipient) do {:ok, response} = :post diff --git a/lib/bot/embed.ex b/lib/bot/embed.ex new file mode 100644 index 0000000..01a181b --- /dev/null +++ b/lib/bot/embed.ex @@ -0,0 +1,11 @@ +defmodule Lanyard.DiscordBot.Embed do + @success 0x57F287 + @error 0xED4245 + @info 0x5865F2 + @warn 0xFEE75C + + def success(fields), do: Map.put(fields, :color, @success) + def error(fields), do: Map.put(fields, :color, @error) + def info(fields), do: Map.put(fields, :color, @info) + def warn(fields), do: Map.put(fields, :color, @warn) +end diff --git a/lib/bot/interaction_handler.ex b/lib/bot/interaction_handler.ex new file mode 100644 index 0000000..1cb3c23 --- /dev/null +++ b/lib/bot/interaction_handler.ex @@ -0,0 +1,49 @@ +defmodule Lanyard.DiscordBot.InteractionHandler do + @command_map %{ + "get" => Lanyard.DiscordBot.Commands.Get, + "set" => Lanyard.DiscordBot.Commands.Set, + "del" => Lanyard.DiscordBot.Commands.Del, + "kv" => Lanyard.DiscordBot.Commands.KV, + "about" => Lanyard.DiscordBot.Commands.About, + "help" => Lanyard.DiscordBot.Commands.Help, + "apikey" => Lanyard.DiscordBot.Commands.ApiKey + } + + @application_command 2 + + def handle_interaction(%{"type" => @application_command, "data" => %{"name" => name}} = interaction) do + case @command_map[name] do + nil -> :ok + module -> module.handle_interaction(interaction) + end + end + + def handle_interaction(_), do: :ok + + def user_id(interaction) do + cond do + user = interaction["user"] -> user["id"] + member = interaction["member"] -> member["user"]["id"] + true -> nil + end + end + + def option(interaction, name) do + case get_in(interaction, ["data", "options"]) do + nil -> + nil + + options -> + Enum.find_value(options, fn opt -> + if opt["name"] == name, do: opt["value"] + end) + end + end + + def command_definitions do + @command_map + |> Map.values() + |> Enum.uniq() + |> Enum.map(& &1.definition()) + end +end diff --git a/lib/gateway/client.ex b/lib/gateway/client.ex index 17d925f..8035710 100644 --- a/lib/gateway/client.ex +++ b/lib/gateway/client.ex @@ -13,9 +13,7 @@ defmodule Lanyard.Gateway.Client do guilds: 1 <<< 0, guild_members: 1 <<< 1, guild_presences: 1 <<< 8, - guild_messages: 1 <<< 9, - direct_messages: 1 <<< 12, - message_content: 1 <<< 15 + direct_messages: 1 <<< 12 } @intents_mask Enum.reduce(@intents, 0, fn {_k, bit}, acc -> acc ||| bit end) @@ -255,6 +253,16 @@ defmodule Lanyard.Gateway.Client do Logger.info("Discord: Ready") + if Application.get_env(:lanyard, :is_idempotent) do + application_id = get_in(payload.data, ["application", "id"]) + + if application_id do + Task.start(fn -> + Lanyard.DiscordBot.CommandRegistration.register(application_id) + end) + end + end + {:ok, new_state} end @@ -268,6 +276,16 @@ defmodule Lanyard.Gateway.Client do {:ok, state} end + def handle_event({:interaction_create, payload}, state) do + if Application.get_env(:lanyard, :is_idempotent) do + Task.start(fn -> + Lanyard.DiscordBot.InteractionHandler.handle_interaction(payload.data) + end) + end + + {:ok, state} + end + def handle_event({:guild_create, payload}, state) do create_member_presences(payload) diff --git a/lib/gateway/utility.ex b/lib/gateway/utility.ex index bffb6c7..1bf3a32 100644 --- a/lib/gateway/utility.ex +++ b/lib/gateway/utility.ex @@ -19,6 +19,7 @@ defmodule Lanyard.Gateway.Utility do @dispatch_events %{ "READY" => :ready, "MESSAGE_CREATE" => :message_create, + "INTERACTION_CREATE" => :interaction_create, "GUILD_CREATE" => :guild_create, "PRESENCE_UPDATE" => :presence_update, "GUILD_MEMBER_ADD" => :guild_member_add, diff --git a/lib/lanyard.ex b/lib/lanyard.ex index 2723e5c..51bd787 100644 --- a/lib/lanyard.ex +++ b/lib/lanyard.ex @@ -6,6 +6,7 @@ defmodule Lanyard do :ets.new(:cached_presences, [:named_table, :set, :public]) :ets.new(:global_subscribers, [:named_table, :set, :public]) + Lanyard.DiscordBot.CommandCache.init() children = [ {Finch, name: Lanyard.Finch}, From 7e87121ef2c78a3b3e2f5e949797ec46bceee682 Mon Sep 17 00:00:00 2001 From: Dustin Rouillard Date: Thu, 11 Jun 2026 19:51:14 -0600 Subject: [PATCH 2/5] Respond with info message when the bot is mentioned --- lib/bot/command_handler.ex | 40 ++++++++++++++++++++++++++++++-------- lib/gateway/client.ex | 8 ++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/bot/command_handler.ex b/lib/bot/command_handler.ex index 8a1e25e..23d4945 100644 --- a/lib/bot/command_handler.ex +++ b/lib/bot/command_handler.ex @@ -18,18 +18,13 @@ defmodule Lanyard.DiscordBot.CommandHandler do %{"author" => %{"bot" => true}} -> :ok - %{"content" => content} = data -> - prefix = Application.get_env(:lanyard, :command_prefix) - + %{} = data -> cond do - String.starts_with?(content, prefix) -> handle_prefix_command(content, data) - dm?(data) -> send_dm_hint(data["channel_id"]) + dm?(data) -> handle_dm(data) + bot_mentioned?(data) -> send_mention_hint(data["channel_id"]) true -> :ok end - %{"channel_id" => channel_id} = data -> - if dm?(data), do: send_dm_hint(channel_id) - _ -> :ok end @@ -37,6 +32,17 @@ defmodule Lanyard.DiscordBot.CommandHandler do def handle_command(_unknown_command, _args), do: :ok + defp handle_dm(data) do + prefix = Application.get_env(:lanyard, :command_prefix) + content = data["content"] || "" + + if String.starts_with?(content, prefix) do + handle_prefix_command(content, data) + else + send_dm_hint(data["channel_id"]) + end + end + defp handle_prefix_command(content, data) do [attempted_command | args] = content @@ -52,6 +58,12 @@ defmodule Lanyard.DiscordBot.CommandHandler do defp dm?(data), do: is_nil(data["guild_id"]) + defp bot_mentioned?(data) do + bot_id = Application.get_env(:lanyard, :bot_user_id) + mentions = data["mentions"] || [] + bot_id != nil && Enum.any?(mentions, fn m -> m["id"] == bot_id end) + end + defp send_dm_hint(channel_id) do embed = Embed.info(%{ @@ -63,4 +75,16 @@ defmodule Lanyard.DiscordBot.CommandHandler do DiscordApi.send_message(channel_id, embed) end + + defp send_mention_hint(channel_id) do + embed = + Embed.info(%{ + title: ":wave: Hi, I'm Lanyard", + description: + "I expose your Discord presence as a public API. Embed your status on websites, dashboards, and more.\n\n**Slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("apikey")} — generate or rotate your private API key (response is private to you)\n#{CommandCache.mention("about")} — privacy policy & terms of service", + footer: %{text: "Type / in any channel to see my commands"} + }) + + DiscordApi.send_message(channel_id, embed) + end end diff --git a/lib/gateway/client.ex b/lib/gateway/client.ex index 8035710..c578f16 100644 --- a/lib/gateway/client.ex +++ b/lib/gateway/client.ex @@ -13,6 +13,7 @@ defmodule Lanyard.Gateway.Client do guilds: 1 <<< 0, guild_members: 1 <<< 1, guild_presences: 1 <<< 8, + guild_messages: 1 <<< 9, direct_messages: 1 <<< 12 } @@ -253,6 +254,13 @@ defmodule Lanyard.Gateway.Client do Logger.info("Discord: Ready") + bot_user_id = + get_in(payload.data, ["user", "id"]) || get_in(payload.data, ["application", "id"]) + + if bot_user_id do + Application.put_env(:lanyard, :bot_user_id, bot_user_id) + end + if Application.get_env(:lanyard, :is_idempotent) do application_id = get_in(payload.data, ["application", "id"]) From 30bfa634c92be8ab8574c599684776575822bf8f Mon Sep 17 00:00:00 2001 From: Dustin Rouillard Date: Mon, 2 Nov 2026 13:27:56 -0700 Subject: [PATCH 3/5] Replace hosted terms/privacy pages with privacy.lanyard.rest link --- lib/api/router.ex | 9 -- lib/api/routes/legal.ex | 268 ------------------------------------- lib/bot/command_handler.ex | 4 +- lib/bot/commands/about.ex | 6 +- lib/bot/commands/help.ex | 2 +- 5 files changed, 5 insertions(+), 284 deletions(-) delete mode 100644 lib/api/routes/legal.ex diff --git a/lib/api/router.ex b/lib/api/router.ex index d28d1db..5707939 100644 --- a/lib/api/router.ex +++ b/lib/api/router.ex @@ -3,7 +3,6 @@ defmodule Lanyard.Api.Router do alias Lanyard.Api.Routes.V1 alias Lanyard.Api.Routes.Discord - alias Lanyard.Api.Routes.Legal alias Lanyard.Api.Routes.Metrics alias Lanyard.Api.Util alias Lanyard.Api.Quicklinks @@ -107,14 +106,6 @@ defmodule Lanyard.Api.Router do end end - get "/privacy" do - Legal.privacy(conn) - end - - get "/terms" do - Legal.terms(conn) - end - forward("/v1", to: V1) forward("/discord", to: Discord) forward("/metrics", to: Metrics) diff --git a/lib/api/routes/legal.ex b/lib/api/routes/legal.ex deleted file mode 100644 index 3832f27..0000000 --- a/lib/api/routes/legal.ex +++ /dev/null @@ -1,268 +0,0 @@ -defmodule Lanyard.Api.Routes.Legal do - import Plug.Conn - - @last_updated "2026-06-11" - - @subtitle "Lanyard — public presence & profile API for members of the Lanyard Discord server" - - @style """ - :root { color-scheme: dark; } - * { box-sizing: border-box; } - body { - margin: 0; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif; - background: #0d0f12; - color: #e6e8eb; - line-height: 1.6; - } - main { - max-width: 760px; - margin: 0 auto; - padding: 64px 24px 96px; - } - header { - margin-bottom: 32px; - padding-bottom: 16px; - border-bottom: 1px solid #1f2329; - } - header h1 { - margin: 0 0 8px; - font-size: 28px; - font-weight: 600; - } - header p { - margin: 0; - color: #8b9098; - font-size: 14px; - } - header .updated { - margin-top: 8px; - color: #6c7178; - font-size: 13px; - } - h2 { - margin: 32px 0 12px; - font-size: 18px; - font-weight: 600; - color: #f4f6f8; - } - p, li { color: #c4c8cd; font-size: 15px; } - ul { padding-left: 20px; } - a { color: #7aa7ff; text-decoration: none; } - a:hover { text-decoration: underline; } - footer { - margin-top: 48px; - padding-top: 16px; - border-top: 1px solid #1f2329; - color: #6c7178; - font-size: 13px; - } - """ - - @terms_body """ -

- These terms cover your use of the Lanyard service — - the public API and WebSocket at - api.lanyard.rest that exposes - Discord presence, activity, and user-set key/value data for members of - the Lanyard Discord server. -

- -

Scope

-

- The Lanyard service only collects presence and activity for users who - are members of the Lanyard Discord server. Presence and activity from - any other Discord server you are in is never collected. If you are not - a member of the Lanyard server, no data about you is collected or - exposed by the service. -

- -

What the service does

-
    -
  • Reads your Discord presence (online status, current game, Spotify, custom status, etc.) while you are a member of the Lanyard server.
  • -
  • Exposes that presence, plus your Discord user ID, username, and avatar, through a public REST API and WebSocket keyed on your Discord user ID.
  • -
  • Stores and serves user-set key/value (KV) entries that you create through bot commands, as part of the same public API.
  • -
- -

Public exposure

-

- The Lanyard API is, by design, publicly readable: anyone who knows - your Discord user ID can query your presence, activity, and KV data - through it. That public exposure is the purpose of the service. - By participating — that is, by being a member of the Lanyard - Discord server — you acknowledge and accept that this data is - publicly available for the duration of your membership. KV entries - you create are also public; do not put anything sensitive in them. -

- -

Using the service

-

- Lanyard is provided as-is, with no guarantee of uptime, availability, - or accuracy. Your use of the API is also subject to Discord's - Terms of Service. -

- -

Acceptable use

-
    -
  • Do not abuse the API (excessive request rates, attempts to disrupt service).
  • -
  • Do not use Lanyard data to harass, dox, or profile other users.
  • -
  • Do not resell, license, or commercialize Lanyard data.
  • -
  • Do not use Lanyard data to train machine learning or AI models.
  • -
- -

Termination

-

- We may restrict your access to the API or KV system at any time, for - any reason, particularly for violations of these terms. Leaving the - Lanyard Discord server stops further presence collection and removes - user-set KV data (see the Privacy Policy). -

- -

Changes

-

- These terms may be updated from time to time. Continued use of the - service after changes constitutes acceptance of the updated terms. -

- """ - - @privacy_body """ -

- This policy describes what data the Lanyard service - — the public presence and profile API at - api.lanyard.rest — processes - for members of the Lanyard Discord server, why, and - how long it is kept. -

- -

Scope of collection

-

- Lanyard only collects presence and activity data while you are a - member of the Lanyard Discord server, and only as reported by Discord - to that server. Presence in other Discord servers you are in is never - collected. If you are not a member of the Lanyard server, the service - holds no data about you. -

- -

What we process

-
    -
  • - Profile fields — your Discord user ID, - username, and avatar are read so the API can return them alongside - your presence. -
  • -
  • - Presence & activity — while you are a - member of the Lanyard Discord server, your online status and - activities Discord exposes to that server (current game, Spotify - track, custom status, etc.) are read so they can be served through - the public REST API and WebSocket. Presence is only read from the - Lanyard server — not from any other server you are in. -
  • -
  • - User-set KV data — key/value entries you set - yourself through bot commands are stored so the API can serve them - back. KV entries are public, alongside the rest of your Lanyard - profile. -
  • -
- -

How it's used

-
    -
  • Powering the public Lanyard REST API and WebSocket.
  • -
  • Letting you embed your presence on status sites, personal websites, dashboards, and other places of your choosing.
  • -
- -

Sharing & public exposure

-

- Presence, activity, profile fields, and KV data exposed via the - Lanyard API are publicly readable by anyone who knows your Discord - user ID. That is the purpose of the service. We do not sell data - and do not share it with third parties beyond what is required to - run the service (Discord, Cloudflare). Because the API is public, - we cannot control who consumes it once data has been served. -

- -

Retention & deletion

-

- Presence and activity data is ephemeral — it only exists while - you are a member of the Lanyard server and connected to Discord, and - is not retained as historical records. User-set KV data persists - until you remove it or until you leave the Lanyard Discord server, - at which point all data set by you or provided by you to the service - is deleted. -

- -

Your choices

-
    -
  • Leave the Lanyard Discord server to stop presence collection and clear any user-set data tied to your account.
  • -
  • Adjust your Discord privacy settings to control what presence/activity Discord exposes to servers in the first place.
  • -
  • Remove KV entries you've set via the bot at any time.
  • -
  • Avoid putting sensitive information in KV entries, since they are publicly readable.
  • -
- -

Contact

-

- Questions about this policy can be raised in the Lanyard Discord server. -

- """ - - def terms(conn) do - render(conn, - title: "Terms of Service", - document_title: "Terms of Service — Lanyard", - active_path: "/terms", - body: @terms_body - ) - end - - def privacy(conn) do - render(conn, - title: "Privacy Policy", - document_title: "Privacy Policy — Lanyard", - active_path: "/privacy", - body: @privacy_body - ) - end - - defp render(conn, opts) do - conn - |> put_resp_content_type("text/html; charset=utf-8") - |> send_resp(200, page(opts)) - end - - defp page(opts) do - """ - - - - - - #{opts[:document_title]} - - - -
-
-

#{opts[:title]}

-

#{@subtitle}

-

Last updated: #{@last_updated}

-
- #{opts[:body]} - #{footer(opts[:active_path])} -
- - - """ - end - - defp footer(active_path) do - """ -
- #{link("/terms", "Terms", active_path)} · #{link("/privacy", "Privacy", active_path)} -
- """ - end - - defp link(href, label, active_path) when href == active_path, do: "#{label}" - defp link(href, label, _active_path), do: ~s(#{label}) -end diff --git a/lib/bot/command_handler.ex b/lib/bot/command_handler.ex index 23d4945..7f34b3f 100644 --- a/lib/bot/command_handler.ex +++ b/lib/bot/command_handler.ex @@ -69,7 +69,7 @@ defmodule Lanyard.DiscordBot.CommandHandler do Embed.info(%{ title: ":wave: Hi! I'm Lanyard", description: - "Looking for your API key? Use #{CommandCache.mention("apikey")} — the response is private to you.\n\n**Other slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("about")} — privacy policy & terms of service", + "Looking for your API key? Use #{CommandCache.mention("apikey")} — the response is private to you.\n\n**Other slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("about")} — privacy policy", footer: %{text: "Type / in any channel to see my commands"} }) @@ -81,7 +81,7 @@ defmodule Lanyard.DiscordBot.CommandHandler do Embed.info(%{ title: ":wave: Hi, I'm Lanyard", description: - "I expose your Discord presence as a public API. Embed your status on websites, dashboards, and more.\n\n**Slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("apikey")} — generate or rotate your private API key (response is private to you)\n#{CommandCache.mention("about")} — privacy policy & terms of service", + "I expose your Discord presence as a public API. Embed your status on websites, dashboards, and more.\n\n**Slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("apikey")} — generate or rotate your private API key (response is private to you)\n#{CommandCache.mention("about")} — privacy policy", footer: %{text: "Type / in any channel to see my commands"} }) diff --git a/lib/bot/commands/about.ex b/lib/bot/commands/about.ex index 60f8afe..b11c2da 100644 --- a/lib/bot/commands/about.ex +++ b/lib/bot/commands/about.ex @@ -5,7 +5,7 @@ defmodule Lanyard.DiscordBot.Commands.About do def definition do %{ name: "about", - description: "View Lanyard's privacy policy and terms of service" + description: "View Lanyard's privacy policy" } end @@ -18,12 +18,10 @@ defmodule Lanyard.DiscordBot.Commands.About do end defp embed do - base = Application.get_env(:lanyard, :external_url) - Embed.info(%{ title: "Lanyard", description: - "Discord presence as an API. Embed your status on websites, dashboards, and more.\n\n:lock: **Privacy Policy:** #{base}/privacy\n:scroll: **Terms of Service:** #{base}/terms", + "Discord presence as an API. Embed your status on websites, dashboards, and more.\n\n:lock: **Privacy Policy:** https://privacy.lanyard.rest/", footer: %{text: "lanyard.rest"} }) end diff --git a/lib/bot/commands/help.ex b/lib/bot/commands/help.ex index 9bcc55a..3d80b51 100644 --- a/lib/bot/commands/help.ex +++ b/lib/bot/commands/help.ex @@ -22,7 +22,7 @@ defmodule Lanyard.DiscordBot.Commands.Help do Embed.info(%{ title: "Lanyard Bot Commands", description: - "Store and manage custom data on your Lanyard profile.\n\n#{CommandCache.mention("get")} `` — get the value of a key\n#{CommandCache.mention("set")} ` ` — set a key to a value\n#{CommandCache.mention("del")} `` — delete a key\n#{CommandCache.mention("kv")} — list all of your current keys\n#{CommandCache.mention("apikey")} — get (or regenerate) your API key, only visible to you\n#{CommandCache.mention("about")} — privacy policy & terms of service", + "Store and manage custom data on your Lanyard profile.\n\n#{CommandCache.mention("get")} `` — get the value of a key\n#{CommandCache.mention("set")} ` ` — set a key to a value\n#{CommandCache.mention("del")} `` — delete a key\n#{CommandCache.mention("kv")} — list all of your current keys\n#{CommandCache.mention("apikey")} — get (or regenerate) your API key, only visible to you\n#{CommandCache.mention("about")} — privacy policy", fields: [ %{ name: "Docs & Source", From 17ea10945c49a110bb889d1cbdf26ce7df732ba6 Mon Sep 17 00:00:00 2001 From: Dustin Rouillard Date: Mon, 2 Nov 2026 13:36:17 -0700 Subject: [PATCH 4/5] Expand /about embed with commands, presence link, and docs --- lib/bot/command_handler.ex | 4 ++-- lib/bot/commands/about.ex | 27 +++++++++++++++++++++------ lib/bot/commands/help.ex | 2 +- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/bot/command_handler.ex b/lib/bot/command_handler.ex index 7f34b3f..aa6424a 100644 --- a/lib/bot/command_handler.ex +++ b/lib/bot/command_handler.ex @@ -69,7 +69,7 @@ defmodule Lanyard.DiscordBot.CommandHandler do Embed.info(%{ title: ":wave: Hi! I'm Lanyard", description: - "Looking for your API key? Use #{CommandCache.mention("apikey")} — the response is private to you.\n\n**Other slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("about")} — privacy policy", + "Looking for your API key? Use #{CommandCache.mention("apikey")} — the response is private to you.\n\n**Other slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("about")} — about Lanyard, docs & privacy policy", footer: %{text: "Type / in any channel to see my commands"} }) @@ -81,7 +81,7 @@ defmodule Lanyard.DiscordBot.CommandHandler do Embed.info(%{ title: ":wave: Hi, I'm Lanyard", description: - "I expose your Discord presence as a public API. Embed your status on websites, dashboards, and more.\n\n**Slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("apikey")} — generate or rotate your private API key (response is private to you)\n#{CommandCache.mention("about")} — privacy policy", + "I expose your Discord presence as a public API. Embed your status on websites, dashboards, and more.\n\n**Slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("apikey")} — generate or rotate your private API key (response is private to you)\n#{CommandCache.mention("about")} — about Lanyard, docs & privacy policy", footer: %{text: "Type / in any channel to see my commands"} }) diff --git a/lib/bot/commands/about.ex b/lib/bot/commands/about.ex index b11c2da..9ed1f3c 100644 --- a/lib/bot/commands/about.ex +++ b/lib/bot/commands/about.ex @@ -1,27 +1,42 @@ defmodule Lanyard.DiscordBot.Commands.About do + alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi alias Lanyard.DiscordBot.Embed + alias Lanyard.DiscordBot.InteractionHandler def definition do %{ name: "about", - description: "View Lanyard's privacy policy" + description: "Learn what Lanyard is, how to use it, and where to find the docs" } end def handle(_, payload) do - DiscordApi.send_message(payload["channel_id"], embed()) + DiscordApi.send_message(payload["channel_id"], embed(payload["author"]["id"])) end def handle_interaction(interaction) do - DiscordApi.respond_to_interaction(interaction["id"], interaction["token"], embed()) + user_id = InteractionHandler.user_id(interaction) + + DiscordApi.respond_to_interaction(interaction["id"], interaction["token"], embed(user_id)) end - defp embed do + defp embed(user_id) do + base = Application.get_env(:lanyard, :external_url) + Embed.info(%{ - title: "Lanyard", + title: ":wave: Hi, I'm Lanyard", description: - "Discord presence as an API. Embed your status on websites, dashboards, and more.\n\n:lock: **Privacy Policy:** https://privacy.lanyard.rest/", + "I expose your Discord presence as a public API. Embed your status on websites, dashboards, and more.\n\n**Slash commands:**\n#{CommandCache.mention("kv")} — view your K/V keys\n#{CommandCache.mention("get")} — get a value\n#{CommandCache.mention("set")} — set a key\n#{CommandCache.mention("del")} — delete a key\n#{CommandCache.mention("apikey")} — generate or rotate your private API key (response is private to you)\n#{CommandCache.mention("help")} — list all commands", + fields: [ + %{name: "Your presence", value: "#{base}/v1/users/#{user_id}", inline: false}, + %{ + name: "Links", + value: + "[API/Socket docs](https://github.com/Phineas/lanyard#readme) · [Source code](https://github.com/Phineas/lanyard) · [Privacy policy](https://privacy.lanyard.rest/)", + inline: false + } + ], footer: %{text: "lanyard.rest"} }) end diff --git a/lib/bot/commands/help.ex b/lib/bot/commands/help.ex index 3d80b51..4f83737 100644 --- a/lib/bot/commands/help.ex +++ b/lib/bot/commands/help.ex @@ -22,7 +22,7 @@ defmodule Lanyard.DiscordBot.Commands.Help do Embed.info(%{ title: "Lanyard Bot Commands", description: - "Store and manage custom data on your Lanyard profile.\n\n#{CommandCache.mention("get")} `` — get the value of a key\n#{CommandCache.mention("set")} ` ` — set a key to a value\n#{CommandCache.mention("del")} `` — delete a key\n#{CommandCache.mention("kv")} — list all of your current keys\n#{CommandCache.mention("apikey")} — get (or regenerate) your API key, only visible to you\n#{CommandCache.mention("about")} — privacy policy", + "Store and manage custom data on your Lanyard profile.\n\n#{CommandCache.mention("get")} `` — get the value of a key\n#{CommandCache.mention("set")} ` ` — set a key to a value\n#{CommandCache.mention("del")} `` — delete a key\n#{CommandCache.mention("kv")} — list all of your current keys\n#{CommandCache.mention("apikey")} — get (or regenerate) your API key, only visible to you\n#{CommandCache.mention("about")} — about Lanyard, docs & privacy policy", fields: [ %{ name: "Docs & Source", From dc71fcd10ee2884670e7b0883b2f28b02c6f9efc Mon Sep 17 00:00:00 2001 From: Dustin Rouillard Date: Mon, 2 Nov 2026 14:39:06 -0700 Subject: [PATCH 5/5] Address review: drop dead guild paths, register commands once, cap /kv list --- lib/bot/commands/api_key.ex | 10 -------- lib/bot/commands/del.ex | 33 +++++++------------------- lib/bot/commands/get.ex | 45 ++++++++++++----------------------- lib/bot/commands/kv.ex | 47 +++++++++++++++++++++++-------------- lib/bot/commands/set.ex | 44 ++++++++++++---------------------- lib/gateway/client.ex | 22 +++++++---------- 6 files changed, 77 insertions(+), 124 deletions(-) diff --git a/lib/bot/commands/api_key.ex b/lib/bot/commands/api_key.ex index fcb3a59..f5f2ed9 100644 --- a/lib/bot/commands/api_key.ex +++ b/lib/bot/commands/api_key.ex @@ -10,16 +10,6 @@ defmodule Lanyard.DiscordBot.Commands.ApiKey do } end - def handle(_, %{"channel_id" => channel_id, "guild_id" => _guild_id} = _p) do - embed = - Embed.error(%{ - title: ":x: DM only", - description: "You can only perform this command in DMs with me." - }) - - DiscordApi.send_message(channel_id, embed) - end - def handle(_, payload) do user_id = payload["author"]["id"] key = generate_api_key(user_id) diff --git a/lib/bot/commands/del.ex b/lib/bot/commands/del.ex index 070d41c..6097562 100644 --- a/lib/bot/commands/del.ex +++ b/lib/bot/commands/del.ex @@ -1,5 +1,4 @@ defmodule Lanyard.DiscordBot.Commands.Del do - alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi alias Lanyard.DiscordBot.Embed alias Lanyard.DiscordBot.Commands.ApiKey @@ -19,21 +18,16 @@ defmodule Lanyard.DiscordBot.Commands.Del do user_id = payload["author"]["id"] channel_id = payload["channel_id"] - cond do - ApiKey.contains_api_key?(user_id, args) -> - DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) - - dm?(payload) -> - embed = - case args do - [key] -> kv_del_embed(user_id, key) - _ -> usage_embed() - end - - DiscordApi.send_message(channel_id, embed) + if ApiKey.contains_api_key?(user_id, args) do + DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) + else + embed = + case args do + [key] -> kv_del_embed(user_id, key) + _ -> usage_embed() + end - true -> - DiscordApi.send_message(channel_id, moved_embed("del")) + DiscordApi.send_message(channel_id, embed) end :ok @@ -76,13 +70,4 @@ defmodule Lanyard.DiscordBot.Commands.Del do description: "Usage: `#{Application.get_env(:lanyard, :command_prefix)}del `" }) end - - defp dm?(payload), do: is_nil(payload["guild_id"]) - - defp moved_embed(name) do - Embed.info(%{ - title: ":sparkles: K/V commands have moved", - description: "Use #{CommandCache.mention(name)} instead." - }) - end end diff --git a/lib/bot/commands/get.ex b/lib/bot/commands/get.ex index 47b6970..622c3c2 100644 --- a/lib/bot/commands/get.ex +++ b/lib/bot/commands/get.ex @@ -1,5 +1,4 @@ defmodule Lanyard.DiscordBot.Commands.Get do - alias Lanyard.DiscordBot.CommandCache alias Lanyard.DiscordBot.DiscordApi alias Lanyard.DiscordBot.Embed alias Lanyard.DiscordBot.Commands.ApiKey @@ -19,26 +18,21 @@ defmodule Lanyard.DiscordBot.Commands.Get do user_id = payload["author"]["id"] channel_id = payload["channel_id"] - cond do - ApiKey.contains_api_key?(user_id, args) -> - DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) - - dm?(payload) -> - embed = - case args do - [key] -> - case kv_get_embed(user_id, key) do - {_kind, e} -> e - end - - _ -> - usage_embed() - end - - DiscordApi.send_message(channel_id, embed) - - true -> - DiscordApi.send_message(channel_id, moved_embed("get")) + if ApiKey.contains_api_key?(user_id, args) do + DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) + else + embed = + case args do + [key] -> + case kv_get_embed(user_id, key) do + {_kind, e} -> e + end + + _ -> + usage_embed() + end + + DiscordApi.send_message(channel_id, embed) end :ok @@ -104,13 +98,4 @@ defmodule Lanyard.DiscordBot.Commands.Get do description: "Usage: `#{Application.get_env(:lanyard, :command_prefix)}get `" }) end - - defp dm?(payload), do: is_nil(payload["guild_id"]) - - defp moved_embed(name) do - Embed.info(%{ - title: ":sparkles: K/V commands have moved", - description: "Use #{CommandCache.mention(name)} instead." - }) - end end diff --git a/lib/bot/commands/kv.ex b/lib/bot/commands/kv.ex index 2853143..af1e3cc 100644 --- a/lib/bot/commands/kv.ex +++ b/lib/bot/commands/kv.ex @@ -12,15 +12,7 @@ defmodule Lanyard.DiscordBot.Commands.KV do end def handle(_, payload) do - user_id = payload["author"]["id"] - channel_id = payload["channel_id"] - - embed = - if dm?(payload), - do: kv_list_embed(user_id), - else: moved_embed() - - DiscordApi.send_message(channel_id, embed) + DiscordApi.send_message(payload["channel_id"], kv_list_embed(payload["author"]["id"])) end def handle_interaction(interaction) do @@ -41,7 +33,7 @@ defmodule Lanyard.DiscordBot.Commands.KV do keys_value = case keys do [] -> "_No keys yet._" - list -> "```#{Enum.join(list, ", ")}```" + list -> keys_field(list, user_id) end Embed.info(%{ @@ -54,13 +46,32 @@ defmodule Lanyard.DiscordBot.Commands.KV do }) end - defp moved_embed do - Embed.info(%{ - title: ":sparkles: K/V commands have moved", - description: - "Use #{CommandCache.mention("kv")} to view your keys, #{CommandCache.mention("get")} to retrieve, #{CommandCache.mention("set")} to update, or #{CommandCache.mention("del")} to remove." - }) - end + # Embed field values are capped at 1024 chars, users can have 512 keys of up to 255 chars each + @max_field_length 1_024 + # Room for the code fence and the "...and N more" footer + @reserved_length 120 - defp dm?(payload), do: is_nil(payload["guild_id"]) + @doc false + def keys_field(keys, user_id) do + {shown, _length} = + keys + |> Enum.map(&String.replace(&1, "`", "`\u200b")) + |> Enum.reduce_while({[], 0}, fn key, {acc, length} -> + new_length = length + String.length(key) + 2 + + if new_length > @max_field_length - @reserved_length, + do: {:halt, {acc, length}}, + else: {:cont, {[key | acc], new_length}} + end) + + content = "```#{shown |> Enum.reverse() |> Enum.join(", ")}```" + + case length(keys) - length(shown) do + 0 -> + content + + hidden -> + "#{content}…and #{hidden} more, see all at #{Application.get_env(:lanyard, :external_url)}/v1/users/#{user_id}" + end + end end diff --git a/lib/bot/commands/set.ex b/lib/bot/commands/set.ex index e922a67..bb2eca4 100644 --- a/lib/bot/commands/set.ex +++ b/lib/bot/commands/set.ex @@ -20,26 +20,21 @@ defmodule Lanyard.DiscordBot.Commands.Set do user_id = payload["author"]["id"] channel_id = payload["channel_id"] - cond do - ApiKey.contains_api_key?(user_id, args) -> - DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) - - dm?(payload) -> - embed = - case args do - [key | value_parts] when value_parts != [] -> - case kv_set_embed(user_id, key, Enum.join(value_parts, " ")) do - {_kind, e} -> e - end - - _ -> - usage_embed() - end - - DiscordApi.send_message(channel_id, embed) - - true -> - DiscordApi.send_message(channel_id, moved_embed("set")) + if ApiKey.contains_api_key?(user_id, args) do + DiscordApi.send_message(channel_id, ApiKey.leak_warning_embed()) + else + embed = + case args do + [key | value_parts] when value_parts != [] -> + case kv_set_embed(user_id, key, Enum.join(value_parts, " ")) do + {_kind, e} -> e + end + + _ -> + usage_embed() + end + + DiscordApi.send_message(channel_id, embed) end :ok @@ -98,13 +93,4 @@ defmodule Lanyard.DiscordBot.Commands.Set do description: "Usage: `#{Application.get_env(:lanyard, :command_prefix)}set `" }) end - - defp dm?(payload), do: is_nil(payload["guild_id"]) - - defp moved_embed(name) do - Embed.info(%{ - title: ":sparkles: K/V commands have moved", - description: "Use #{CommandCache.mention(name)} instead." - }) - end end diff --git a/lib/gateway/client.ex b/lib/gateway/client.ex index c578f16..df42c95 100644 --- a/lib/gateway/client.ex +++ b/lib/gateway/client.ex @@ -254,21 +254,17 @@ defmodule Lanyard.Gateway.Client do Logger.info("Discord: Ready") - bot_user_id = - get_in(payload.data, ["user", "id"]) || get_in(payload.data, ["application", "id"]) + bot_user_id = payload.data["user"]["id"] + Application.put_env(:lanyard, :bot_user_id, bot_user_id) - if bot_user_id do - Application.put_env(:lanyard, :bot_user_id, bot_user_id) - end - - if Application.get_env(:lanyard, :is_idempotent) do - application_id = get_in(payload.data, ["application", "id"]) + # READY also fires on full reconnects, commands only need registering once per boot + if Application.get_env(:lanyard, :is_idempotent) and + not :persistent_term.get(:lanyard_commands_registered, false) do + :persistent_term.put(:lanyard_commands_registered, true) - if application_id do - Task.start(fn -> - Lanyard.DiscordBot.CommandRegistration.register(application_id) - end) - end + Task.start(fn -> + Lanyard.DiscordBot.CommandRegistration.register(bot_user_id) + end) end {:ok, new_state}