KlangDocs

    Open Klang

    Bots

    View as Markdown

    Getting started

    Create an app in Klang’s developer portal, keep its bot token safe, choose its intents and add it to a server with its install link. Then check the token with one request.

    Before you start

    You need an account on the bots’ closed beta. If you aren’t on it yet, request access from app.klang.gg/developers (more in The closed beta). Everything below happens in the developer portal, inside the Klang app: open Settings › Developers and then Open the portal, or go straight to app.klang.gg/developers. It works the same in the desktop app.

    Create an app

    1. In Your apps, select New app.
    2. Give it a name of 2 to 32 characters. It’s also your bot’s name, and it can’t contain “Klang”.
    3. The first time, accept the developer terms.
    4. Select Create app.

    Every app comes with its bot, so there’s nothing else to switch on. You can have up to 10 apps during the beta.

    The app’s page has three sections:

    • General: the name, the icon (it’s also the bot’s picture), a description of up to 400 characters that shows on the bot’s profile and when someone adds it, and the App ID. The App ID is also the bot’s user ID, and it’s the one libraries and install links use. The Public key (with Copy) and the Interactions endpoint URL (with Save URL and, once saved, Stop using it) are here too, for bots that take their commands by HTTP (see HTTP interactions). So is deleting the app: its bot leaves every server and its token stops working.
    • Bot: the bot’s username, its token and its privileged intents.
    • Install: the permissions it asks for and its install link.

    The bot token

    Right after you create the app, Klang shows its token once. Copy it and keep it somewhere safe, such as your host’s secrets or an environment variable. Klang only stores a hash of it, so nobody can show it to you again, us included.

    A token has three parts separated by dots, and the last one starts with klang_:

    URL
    MTU3MTMzMjEwNzA1MzgwOTY2NA.aRe7xg.klang_…

    Libraries don’t look inside it: use it as it is wherever they ask for a bot token.

    The token is your bot’s password. Whoever has it can act as your bot in every server it’s in. Don’t paste it in chats, screenshots or code, and keep it out of repositories: load it from an environment variable instead.

    If it leaks, or you lose it, open the Bot section and select Reset token. Klang asks for your password and, if you use two-factor authentication, your code. Then:

    • the old token stops working at once, in the REST API and in the gateway;
    • your bot disconnects, and it won’t reconnect with the old token;
    • the new token shows once, as the first one did.

    The Bot section also says when the current token was created and when it was last used.

    Intents

    Intents say which groups of events your bot wants. It asks for them when it connects to the gateway, and most libraries take them as an option. Three of them are privileged, because they reach more personal data:

    Intent Switch in the portal What it gives
    GUILD_MEMBERS Server members People joining, leaving and changing, and the full member list of a server.
    MESSAGE_CONTENT Message content The text, embeds, attachments, poll and sticker of messages in servers.
    GUILD_PRESENCES Presences Who’s online. Not available yet: asking for it closes the connection with 4014.

    Switch on the ones your bot needs in the Bot section. They’re free while the bot is in 100 servers or fewer. Past that, only the ones Klang’s team has reviewed keep working (see Above 100 servers). A bot that asks the gateway for a privileged intent that isn’t switched on in the portal can’t connect (close code 4014).

    Without Message content, messages in servers reach your bot with those fields empty, except the ones that mention it, the ones it sent and its direct messages. A bot that answers commands typed as text (!ping) needs it. The details are in Gateway.

    Add the bot to a server

    1. Open the Install section and tick the permissions your bot needs. They’re grouped as general, text and voice, in Klang’s words.

    2. Copy the install link. It looks like this:

      URL
      https://app.klang.gg/oauth2/authorize?client_id={app.id}&scope=bot&permissions={bits}
    3. Open it, or share it with whoever manages the server. They pick one of their servers, see the bot, its description, its owner and the permissions it asks for, and select Authorize.

    To add a bot, you need Manage server in that server, and during the beta you need to be on the beta list. Whoever authorizes can untick any permission, and can never give one they don’t have themselves.

    The bot joins with a role of its own, named after it, at the bottom of the role list. That role carries the permissions from the link. Its permissions can be changed later like any role’s, but it can’t be given to anyone else or deleted while the bot is there. If your bot needs to manage other roles, move its role above them.

    The link takes the usual query parameters:

    Parameter What it does
    client_id The App ID. Required.
    scope Must include bot. applications.commands is accepted but not needed: a bot’s commands work in every server it’s in.
    permissions The permissions as a decimal number, in the bit numbers of Discord’s API. See Permissions.
    guild_id A server to select at the start.
    disable_guild_select With true and guild_id, the server can’t be changed.

    Install links built by a library, such as discord.js’s generateInvite, point to https://api.klang.gg/api/oauth2/authorize. That address sends people on to the app, so they work too.

    A few limits apply: a server can have up to 25 bots, and a bot can be in up to 100 servers until Klang’s team verifies it. Adding a bot doesn’t count as someone joining for the server’s raid protection, and the server’s verification level doesn’t apply to it.

    To remove a bot, open Server settings › Integrations › Bots and select Remove. It leaves with its role. A bot can also leave a server by itself, with DELETE /users/@me/guilds/{guild.id}.

    Check the token

    One request tells you the token works and which bot it belongs to:

    curl https://api.klang.gg/api/v10/users/@me \
      -H "Authorization: Bot $KLANG_BOT_TOKEN"
    const response = await fetch('https://api.klang.gg/api/v10/users/@me', {
      headers: { Authorization: `Bot ${process.env.KLANG_BOT_TOKEN}` },
    });
    
    console.log(response.status, await response.json());
    import os
    import requests
    
    response = requests.get(
        "https://api.klang.gg/api/v10/users/@me",
        headers={"Authorization": f"Bot {os.environ['KLANG_BOT_TOKEN']}"},
    )
    print(response.status_code, response.json())

    A 200 comes with your bot’s user, with "bot": true. A 401 means the token is wrong, was reset, or the app is suspended. Now connect your library.

    Verify your bot

    A bot nobody has checked can be in up to 100 servers. Past that, Klang’s team looks at who’s behind it and what it does with people’s data before it can grow. Once verified, it can be in up to 2,500 servers, and a check mark shows next to its Bot tag everywhere people see it.

    Your bot doesn’t need it to start: ask when it’s getting close to 100. Until then, someone who tries to add it to a 101st server reads that the bot needs to be verified first, and the bot doesn’t join. Your bot gets no error and no event for it.

    What you need before asking, which the portal lists with a link to fix each one:

    • a confirmed email on your Klang account;
    • two-step verification turned on for your account;
    • a description of the app, which people see before they add the bot.

    To ask, go to the developer portal, pick the app and open Verification. It asks for:

    • the address of your bot’s privacy policy and of its terms of service, both https;
    • for each privileged intent your bot needs above 100 servers (Server members, Message content), what it uses it for, in a few sentences.

    We don’t ask for identity documents. You can have one request waiting at a time. The team reviews it by hand and answers in your Klang inbox:

    • Verified: the bot can join more servers at once. The privileged intents you explained keep working above 100 servers, and your app’s flags carry them without _LIMITED (see the application).
    • Not verified: the portal says why (a policy or terms that are missing or don’t cover the bot, intents that aren’t explained, a purpose that isn’t clear, or the bot going against Klang’s rules), sometimes with a note. Fix it and ask again.

    Above 100 servers

    Past 100 servers, a privileged intent works only if Klang’s team has reviewed it. When your bot joins its 101st server, every privileged intent it has that wasn’t explained in an approved request is switched off, and you get a message in your Klang inbox saying which ones. Then:

    • an Identify that still asks for one of them closes with 4014, and so does the next Resume of a session that uses it. A session that’s already connected keeps what it had until it reconnects, so change your bot’s intents before it does;
    • in the portal, the intent shows Needs review and can’t be switched on again;
    • the reviewed ones keep working, and your app’s flags carry them without _LIMITED.

    To get one back, open Verification in the portal and ask for its review: once your bot is verified, the request takes only the intents and what your bot uses each one for (your pages stay the ones you sent). Your bot stays verified while the team looks at it, and the answer comes to your inbox. If your bot drops back to 100 servers or fewer, the intents don’t come back by themselves, but you can switch them on again in the portal, free.

    An unverified bot can’t get past 100 servers, so this only happens to verified bots.

    A verified bot’s user carries the VERIFIED_BOT flag (1 << 16) in public_flags, as libraries expect: user.flags.has('VerifiedBot') in discord.js, user.public_flags.verified_bot in discord.py.

    People can report a bot from its menu with Report this bot. The report reaches Klang’s team with your app and you as its owner.