Skip to content

Discord Client

A TypeScript client for Discord — posts messages through webhooks, and (with a bot token) provisions a server’s channels, roles and webhooks.

Terminal window
pnpm add @theholocron/discord-client
import { createDiscordClient, parseWebhookUrl } from "@theholocron/discord-client";
const client = createDiscordClient();
const { id, token } = parseWebhookUrl(process.env.DISCORD_WEBHOOK_URL!);
const info = await client.webhooks.get(id, token);
console.log(info.name);
await client.webhooks.execute(id, token, "Deploy finished ✅");

Discord webhooks authenticate via the id+token embedded in the URL itself, not an Authorization header — createDiscordClient takes no token option; each call supplies its own.

createDiscordBotClient authenticates as a bot (Authorization: Bot <token>) and covers what you need to set a server up. channels.createMessage posts as the bot (needs View Channel and Send Messages in that channel); to post through a channel webhook with no token, use createDiscordClient().webhooks.execute.

import { ChannelType, createDiscordBotClient } from "@theholocron/discord-client";
const bot = createDiscordBotClient({
botToken: process.env.DISCORD_BOT_TOKEN!,
});
const category = await bot.guilds.createChannel(guildId, {
name: "ALERTS",
type: ChannelType.Category,
});
const channel = await bot.guilds.createChannel(guildId, {
name: "alerts",
parent_id: category.id,
});
const hook = await bot.channels.createWebhook(channel.id, {
name: "repo-sync",
});
console.log(hook.url); // a secret -- Discord returns it only for webhooks this bot created

baseUrl is a test seam: the bot token is sent to whatever hostname it names, so never derive it from untrusted input. Ids passed to any method must be Discord snowflakes (digits only); anything else throws before a request is made.

A 429 is retried, up to maxRetries times, waiting for the Retry-After Discord sends; a wait longer than 10 seconds fails immediately instead. A request that runs past timeoutMs is aborted. Other errors are not retried, and the create* calls are not idempotent: if a response is lost after Discord created the resource, look it up by name before creating it again. A webhook created that way cannot have its URL read back, since Discord returns the token only on the create response.

Resource Covers
client.users The bot’s identity and the servers it is in (look here before retrying guilds.create)
client.application The bot’s own application: description, tags, install settings, URLs, intent flags
client.guilds Servers: settings, channels, roles, emojis, members, welcome screen and onboarding
client.channels Webhooks, messages (read, post, edit), invites and pins on a channel
client.threads Forum posts and threads: create, retitle, retag, archive; active and archived listings
client.webhooks Renaming and deleting a webhook

application.modify cannot write redirect_uris (it can be read), so add those in the developer portal.

permissions([...names]) builds the decimal-string bitfield Discord expects, and permissionNames(bitfield) reads one back; Permission holds the bits themselves. Welcome screen and onboarding only work once the server has the community features on, and modifyOnboarding is a PUT that replaces the whole config. channels.createMessage, channels.editMessage and the first message of threads.createForumPost turn every mention off unless allowed_mentions says otherwise.

The bot needs Manage Channels, Manage Roles, Manage Webhooks and (for server settings, welcome screen and onboarding) Manage Server in the server, plus View Channel and Send Messages in any channel it posts to with channels.createMessage.

createDiscordOAuthClient({ clientId, clientSecret, redirectUri }) is “sign in with Discord”: authorizeUrl({ state }) builds the link a member opens, exchangeCode(code) trades what Discord sends back for a token, and user(token) and connections(token) read who they are and which accounts they have linked. findVerifiedConnection(connections, "github") picks the GitHub account Discord has confirmed they own (an unverified name proves nothing). revoke(token) discards the token once it has been read; nothing needs to keep it. signOAuthState(secret) and verifyOAuthState(secret, state) make and check a state that expires and cannot be forged, so a server with no storage can still refuse a forged callback.

guilds.membersWithRole(guildId, roleId) lists who holds a role; like guilds.members it needs the privileged Server Members Intent, switched on in the developer portal. guilds.member(guildId, userId) reads one member and needs no intent.

guilds.create makes a server the bot owns (only a bot in fewer than ten servers may), guilds.modify(id, { owner_id }) hands it over, channels.createInvite makes the link to join it, and guilds.delete removes it. On the webhook resource, executeAndWait posts and returns the message id and deleteMessage removes it, so a check can post a line, read it back with channels.messages, and clean up. channels.pin pins a message.

guilds.createChannel with type: ChannelType.Forum and available_tags makes a forum; channels.modify replaces its tags (send the whole list, with each kept tag’s id). threads.createForumPost(channelId, { name, applied_tags, message }) makes a post, and message.flags: MessageFlag.SuppressNotifications posts it without notifying anyone. threads.modify retitles or retags a post, and writing to an archived post unarchives it. Discord hides a post that has been quiet for a while, so threads.inChannel(guildId, forumId) reads the archived pages too before you decide a post does not exist. To look for one post, loop over threads.pages(guildId, forumId) (the active posts, then each archived page, newest first) and stop at it: a recently closed post is near the top, so it costs a call or two and not the whole archive. A 429 is waited out (up to maxRetries times, never longer than 10 seconds each) before the call fails. Creating a post is not idempotent: look it up first.

Discord only returns a webhook’s token/url for webhooks the same application created, so a pre-existing webhook can be found by name but its URL cannot be read back.

The full API reference is generated from the package’s TSDoc comments.