generated from Andrew/typescript-project-template
No description
- TypeScript 100%
- Added OAuth2Client class for handling Discord OAuth2 flows including authorization code, implicit, client credentials, bot, and webhook flows. - Introduced error handling with custom error classes: OAuth2Error, DiscordAPIRequestError, ConfigurationError, and ValidationError. - Defined types for OAuth2 responses, authorization options, and Discord entities such as User, Guild, and Role. - Created utility functions for building URLs, encoding credentials, and handling permissions. - Developed an internal HTTP client for making requests to the Discord API with error handling. - Implemented permission calculation utilities to manage Discord's permission system. |
||
|---|---|---|
| .vscode | ||
| src | ||
| .editorconfig | ||
| .eslintrc.cjs | ||
| .gitattributes | ||
| .gitignore | ||
| .npmignore | ||
| .prettierignore | ||
| .prettierrc.json | ||
| bun.lock | ||
| LICENSE | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
discord-oauth2
A modern, type-safe OAuth2 client for Discord. Built with TypeScript. Works in Node.js and Bun.
Features
- Fully typed with strict TypeScript
- Zero dependencies
- Supports all Discord OAuth2 flows
- Built-in permission utilities
- ESM & CommonJS compatible
Installation
# npm
npm install @oneshot101/discord-oauth2
# pnpm
pnpm add @oneshot101/discord-oauth2
# yarn
yarn add @oneshot101/discord-oauth2
# bun
bun add @oneshot101/discord-oauth2
Quick Start
import { OAuth2Client } from '@oneshot101/discord-oauth2';
const client = new OAuth2Client({
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
redirectUri: 'https://your-app.com/callback',
});
const url = client.generateAuthUrl({
scopes: ['identify', 'email'],
state: 'csrf-token',
});
const tokens = await client.exchangeCode('authorization-code');
const user = await client.getUser(tokens.access_token);
console.log(user.username);
Bot Authorization
import { PermissionFlags, calculatePermissions } from '@oneshot101/discord-oauth2';
const perms = calculatePermissions([PermissionFlags.VIEW_CHANNEL, PermissionFlags.SEND_MESSAGES]);
const url = client.generateBotAuthUrl({ permissions: perms });
console.log(url);
Token Management
const refreshed = await client.refreshToken(tokens.refresh_token);
await client.revokeToken(tokens.access_token);
Key Methods
- generateAuthUrl()
- generateBotAuthUrl()
- exchangeCode()
- refreshToken()
- revokeToken()
- getUser()
- getUserGuilds()
- getUserConnections()
- getClientCredentials()
Permission Utilities
- calculatePermissions()
- hasPermission()
- getPermissionFlags()
- PermissionPresets
Links
- GitHub: https://github.com/0neShot101/discord-oauth2
- License: MIT