Lanyard Documentation

repository·main·Indexed 23 days ago

https://github.com/phineas/lanyard

A service that exports live Discord presence and activities to a RESTful API and WebSocket. It includes a real-time, globally-accessible Key-Value (KV) store manageable via API or Discord bot, a React integration library called use-lanyard, and support for self-hosting via Docker.

Tokens
5.1K
Snippets
11
Records
32
Agent score
79%

What's inside Lanyard

  1. What is Lanyard KV?

    main

    Lanyard KV is a dynamic, real-time key-value store integrated into the user API response. When a KV pair is updated, a PRESENCE_UPDATE event is emitted through the Lanyard WebSocket.

    Constraints:

    • Keys and values must be strings.
    • Maximum value length: 30,000 characters.
    • Key format: Letters, numbers, and underscores (a-zA-Z0-9_) only. Max 255 characters.
    • Maximum pairs per user: 512.
  2. Self-host Lanyard with Docker

    main

    To self-host Lanyard, you need a Redis server and a Discord Bot token. Ensure your Discord Bot has the PRESENCE INTENT and SERVER MEMBERS INTENT enabled in the Discord Developer Portal.

    Using Docker CLI

    1. Run Redis:
      docker run -d --name lanyard-redis -v <host_mount_location>:/data redis
    2. Run Lanyard:
      docker run --rm -it -p 4001:4001 -e REDIS_HOST=redis -e BOT_TOKEN=<token> --link lanyard-redis:redis phineas/lanyard:latest

    Using Docker Compose

    Create a docker-compose.yml file:

    version: '3.8'
    
    services:
      redis:
        image: redis
        restart: always
        container_name: lanyard_redis
      lanyard:
        image: phineas/lanyard:latest
        restart: always
        container_name: lanyard
        depends_on:
          - redis
        ports:
          - 4001:4001
        environment:
          BOT_TOKEN: <token>
          REDIS_HOST: redis

    Note: The server runs on HTTP. Use a reverse proxy like Traefik for HTTPS.

  3. Connect to the Lanyard WebSocket

    main

    The Lanyard WebSocket is available at wss://api.lanyard.rest/socket. You can request compression by appending ?compression=zlib_json to the URL.

    Connection Lifecycle:

    1. Connect: Upon connection, you will receive Opcode 1: Hello. This message contains a heartbeat_interval in the d field.
    2. Heartbeat: You must send Opcode 3: Heartbeat repeatedly at the interval specified by the heartbeat_interval received in the Hello message.
    3. Initialize: Immediately after receiving Opcode 1: Hello, you must send Opcode 2: Initialize to subscribe to presences.
  4. Use Lanyard in a React app with use-lanyard

    main

    The recommended way to use Lanyard in React is via the use-lanyard library. It uses a WebSocket connection to provide live updates, and multiple hooks can share a single connection.

    Note: The hook returns undefined until the first presence data arrives. You can pass a single Discord ID or an array of IDs to subscribe to multiple users.

    import { useLanyard } from 'use-lanyard';
    
    export function App() {
    	const presence = useLanyard(YOUR_DISCORD_ID);
    	return <p>Hi, I'm {presence?.discord_user.username}!</p>;
    }
  5. Manage user data with Lanyard.KV.Interface

    main

    The Lanyard.KV.Interface module provides a Key-Value (KV) store for associating data with a specific Discord user ID. This allows you to persist small amounts of custom data that is synchronized with the user's presence.

    Constraints:

    • Key Count Limit: A single user can have a maximum of 512 keys.
    • Key Length Limit: Keys must be 255 characters or less.
    • Key Format: Keys must be alphanumeric (including underscores: a-zA-Z0-9_).
    • Value Length Limit: Values must be 30000 characters or less.
  6. Use Lanyard Discord Bot commands

    main

    The Lanyard Discord bot allows you to store and manage custom data on your Lanyard profile via Discord commands. Commands are prefixed by the configured command prefix (defaulting to whatever is set in the :lanyard, :command_prefix application environment variable).

    Available commands:

    • `prefixget <key>`: Get the value of a specific key.
    • `prefixset <key> <value>`: Set a key to a specific value.
    • `prefixdel <key>`: Delete a specific key.
    • `prefixkv`: List all of your current keys.
    • `prefixapikey`: Get (or regenerate) your Lanyard API key. This key will be sent to you via Direct Message (DM).
  7. Generate a Lanyard API key via Discord DM

    main

    You can generate a unique API key for managing your Lanyard Key/Value (K/V) store by interacting with the Lanyard Discord bot.

    Important Security Notes:

    • Do not share or post this key anywhere. It is a secret key that allows anyone to manage your Lanyard K/V.
    • Do not use this key in a front-end application or website.
    • To access your public data, use your Discord user ID with the public endpoint: #{Application.get_env(:lanyard, :external_url)}/v1/users/{your_discord_user_id}.

    Usage:

    1. Open a Direct Message (DM) with the Lanyard bot.
    2. Run the .apikey command.
    3. The bot will send you an embed containing your new key. You must click the key to reveal it.
    `.apikey`
  8. Regenerate a Lanyard API key

    main

    If you accidentally post your API key in a public channel or believe it has been compromised, you must regenerate it immediately.

    Automatic Regeneration: If you use your API key within a K/V command in a public channel, the bot will detect it, warn you, and automatically regenerate a new key, sending the new one to your DMs.

    Manual Regeneration: To manually trigger a regeneration, send the .apikey command within your Direct Messages with the Lanyard bot.

    `.apikey`
  9. Manage Lanyard KV via HTTP

    main
    You can manage your KV store using the Lanyard API. You must include your API key in the Authorization header. To get your API key, DM the Lanyard bot (Lanyard#5766) with .apikey on Discord.
  10. Unsubscribe from user presences with Opcode 4

    main

    To stop receiving PRESENCE_UPDATE events for specific users, send Opcode 4: Unsubscribe. You can unsubscribe in three ways:

    • Specific Users: Use unsubscribe_from_id (string) for one user, or unsubscribe_from_ids (string[]) for multiple users.
    • All Users: Use unsubscribe_from_all: true to stop receiving updates for all currently subscribed users and remove yourself from the global subscriber list.
    {
      op: 4,
      d: {
        // unsubscribe_from_id should be a single user ID (string) you no longer want to receive updates for
        unsubscribe_from_id: "94490510688792576"
      }
    }
    {
      op: 4,
      d: {
        unsubscribe_from_ids: ["94490510688792576", "156114103033790464"]
      }
    }
    {
      op: 4,
      d: {
        unsubscribe_from_all: true
      }
    }