taverns.js 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/README.md +208 -0
- package/dist/client.d.ts +185 -0
- package/dist/client.js +517 -0
- package/dist/collection.d.ts +63 -0
- package/dist/collection.js +144 -0
- package/dist/constants.d.ts +117 -0
- package/dist/constants.js +190 -0
- package/dist/embed.d.ts +40 -0
- package/dist/embed.js +30 -0
- package/dist/gateway.d.ts +65 -0
- package/dist/gateway.js +273 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +59 -0
- package/dist/permissions.d.ts +161 -0
- package/dist/permissions.js +205 -0
- package/dist/rest.d.ts +119 -0
- package/dist/rest.js +425 -0
- package/dist/types.d.ts +408 -0
- package/dist/types.js +8 -0
- package/dist/webhook.d.ts +33 -0
- package/dist/webhook.js +68 -0
- package/package.json +44 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* taverns.js - PermissionsBitField
|
|
4
|
+
*
|
|
5
|
+
* Permissions utility for working with Tavern permission
|
|
6
|
+
* bitfields. Permissions are stored as BigInt flags, matching the server-side
|
|
7
|
+
* TavernPermission enum.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.PermissionsBitField = exports.PermissionFlags = void 0;
|
|
11
|
+
/**
|
|
12
|
+
* All Tavern permission flags. Each flag is a bit position (0-35).
|
|
13
|
+
* The actual bitfield value is `1n << BigInt(position)`.
|
|
14
|
+
*/
|
|
15
|
+
exports.PermissionFlags = {
|
|
16
|
+
// General
|
|
17
|
+
VIEW_CHANNELS: 0n,
|
|
18
|
+
MANAGE_CHANNELS: 1n,
|
|
19
|
+
MANAGE_ROLES: 2n,
|
|
20
|
+
MANAGE_TAVERN: 3n,
|
|
21
|
+
CREATE_INVITE: 4n,
|
|
22
|
+
KICK_MEMBERS: 5n,
|
|
23
|
+
BAN_MEMBERS: 6n,
|
|
24
|
+
MANAGE_NICKNAMES: 7n,
|
|
25
|
+
MANAGE_PERMISSIONS: 8n,
|
|
26
|
+
PIN_MESSAGES: 9n,
|
|
27
|
+
// Text
|
|
28
|
+
SEND_MESSAGES: 10n,
|
|
29
|
+
MANAGE_MESSAGES: 11n,
|
|
30
|
+
ATTACH_FILES: 13n,
|
|
31
|
+
ADD_REACTIONS: 14n,
|
|
32
|
+
MENTION_EVERYONE: 15n,
|
|
33
|
+
EMBED_LINKS: 16n,
|
|
34
|
+
READ_MESSAGE_HISTORY: 17n,
|
|
35
|
+
USE_EXTERNAL_EMOJI: 18n,
|
|
36
|
+
USE_EXTERNAL_STICKERS: 19n,
|
|
37
|
+
// Content
|
|
38
|
+
MANAGE_STICKERS: 12n,
|
|
39
|
+
// Moderation
|
|
40
|
+
MUTE_MEMBERS: 20n,
|
|
41
|
+
BYPASS_SLOWMODE: 21n,
|
|
42
|
+
// Voice
|
|
43
|
+
CONNECT_VOICE: 22n,
|
|
44
|
+
SPEAK: 23n,
|
|
45
|
+
MANAGE_VOICE: 24n,
|
|
46
|
+
VIDEO: 25n,
|
|
47
|
+
// Events
|
|
48
|
+
CREATE_EVENTS: 26n,
|
|
49
|
+
MANAGE_EVENTS: 27n,
|
|
50
|
+
// Polls
|
|
51
|
+
CREATE_POLLS: 28n,
|
|
52
|
+
VOTE_IN_POLLS: 29n,
|
|
53
|
+
// Admin
|
|
54
|
+
ADMINISTRATOR: 30n,
|
|
55
|
+
// Threads
|
|
56
|
+
CREATE_PUBLIC_THREADS: 31n,
|
|
57
|
+
CREATE_PRIVATE_THREADS: 32n,
|
|
58
|
+
MANAGE_THREADS: 33n,
|
|
59
|
+
SEND_MESSAGES_IN_THREADS: 34n,
|
|
60
|
+
// Bots
|
|
61
|
+
MANAGE_BOTS: 35n,
|
|
62
|
+
};
|
|
63
|
+
/** All permission flags OR'd together */
|
|
64
|
+
const ALL_PERMISSIONS = Object.values(exports.PermissionFlags).reduce((acc, bit) => acc | (1n << bit), 0n);
|
|
65
|
+
/**
|
|
66
|
+
* A bitfield that represents a set of permissions.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const perms = new PermissionsBitField('2048'); // from API string
|
|
71
|
+
*
|
|
72
|
+
* if (perms.has('SEND_MESSAGES')) {
|
|
73
|
+
* // Bot can send messages
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* const withAdmin = perms.add('ADMINISTRATOR');
|
|
77
|
+
* console.log(withAdmin.toArray()); // ['SEND_MESSAGES', 'ADMINISTRATOR']
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
class PermissionsBitField {
|
|
81
|
+
/**
|
|
82
|
+
* Create a new PermissionsBitField.
|
|
83
|
+
* @param bits - A BigInt, numeric string, number, or another PermissionsBitField
|
|
84
|
+
*/
|
|
85
|
+
constructor(bits) {
|
|
86
|
+
if (bits === undefined || bits === null) {
|
|
87
|
+
this.bitfield = 0n;
|
|
88
|
+
}
|
|
89
|
+
else if (bits instanceof PermissionsBitField) {
|
|
90
|
+
this.bitfield = bits.bitfield;
|
|
91
|
+
}
|
|
92
|
+
else if (typeof bits === 'bigint') {
|
|
93
|
+
this.bitfield = bits;
|
|
94
|
+
}
|
|
95
|
+
else if (typeof bits === 'string') {
|
|
96
|
+
this.bitfield = BigInt(bits);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.bitfield = BigInt(bits);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Check if this bitfield has a specific permission.
|
|
104
|
+
* @param permission - Permission flag name or bit position
|
|
105
|
+
*/
|
|
106
|
+
has(permission) {
|
|
107
|
+
const flag = this.resolve(permission);
|
|
108
|
+
// ADMINISTRATOR grants everything
|
|
109
|
+
if ((this.bitfield & (1n << exports.PermissionFlags.ADMINISTRATOR)) !== 0n) {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
return (this.bitfield & flag) === flag;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Check if this bitfield has any of the specified permissions.
|
|
116
|
+
*/
|
|
117
|
+
hasAny(...permissions) {
|
|
118
|
+
return permissions.some((p) => this.has(p));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Check if this bitfield has all of the specified permissions.
|
|
122
|
+
*/
|
|
123
|
+
hasAll(...permissions) {
|
|
124
|
+
return permissions.every((p) => this.has(p));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Return a new PermissionsBitField with the given permission(s) added.
|
|
128
|
+
*/
|
|
129
|
+
add(...permissions) {
|
|
130
|
+
let bits = this.bitfield;
|
|
131
|
+
for (const perm of permissions) {
|
|
132
|
+
bits |= this.resolve(perm);
|
|
133
|
+
}
|
|
134
|
+
return new PermissionsBitField(bits);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Return a new PermissionsBitField with the given permission(s) removed.
|
|
138
|
+
*/
|
|
139
|
+
remove(...permissions) {
|
|
140
|
+
let bits = this.bitfield;
|
|
141
|
+
for (const perm of permissions) {
|
|
142
|
+
bits &= ~this.resolve(perm);
|
|
143
|
+
}
|
|
144
|
+
return new PermissionsBitField(bits);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Return an array of all permission flag names that are set.
|
|
148
|
+
*/
|
|
149
|
+
toArray() {
|
|
150
|
+
const result = [];
|
|
151
|
+
for (const [name, bit] of Object.entries(exports.PermissionFlags)) {
|
|
152
|
+
const flag = 1n << bit;
|
|
153
|
+
if ((this.bitfield & flag) === flag) {
|
|
154
|
+
result.push(name);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Serialize to a decimal string (matches API format).
|
|
161
|
+
*/
|
|
162
|
+
toString() {
|
|
163
|
+
return this.bitfield.toString();
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Serialize to JSON as a decimal string.
|
|
167
|
+
*/
|
|
168
|
+
toJSON() {
|
|
169
|
+
return this.toString();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Check if this bitfield equals another.
|
|
173
|
+
*/
|
|
174
|
+
equals(other) {
|
|
175
|
+
const otherBits = other instanceof PermissionsBitField
|
|
176
|
+
? other.bitfield
|
|
177
|
+
: BigInt(other);
|
|
178
|
+
return this.bitfield === otherBits;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get a bitfield with all permissions set.
|
|
182
|
+
*/
|
|
183
|
+
static all() {
|
|
184
|
+
return new PermissionsBitField(ALL_PERMISSIONS);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Resolve a permission name or bigint to its bitfield flag.
|
|
188
|
+
*/
|
|
189
|
+
resolve(permission) {
|
|
190
|
+
if (typeof permission === 'bigint') {
|
|
191
|
+
return 1n << permission;
|
|
192
|
+
}
|
|
193
|
+
const bit = exports.PermissionFlags[permission];
|
|
194
|
+
if (bit === undefined) {
|
|
195
|
+
throw new RangeError(`Unknown permission: ${permission}`);
|
|
196
|
+
}
|
|
197
|
+
return 1n << bit;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.PermissionsBitField = PermissionsBitField;
|
|
201
|
+
/**
|
|
202
|
+
* Static reference to all permission flags.
|
|
203
|
+
* @example PermissionsBitField.FLAGS.SEND_MESSAGES
|
|
204
|
+
*/
|
|
205
|
+
PermissionsBitField.FLAGS = exports.PermissionFlags;
|
package/dist/rest.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* taverns.js - REST Client
|
|
3
|
+
*
|
|
4
|
+
* HTTP client for the Taverns API.
|
|
5
|
+
* Handles bot token authentication, rate limiting, and retries.
|
|
6
|
+
*/
|
|
7
|
+
import { EventEmitter } from 'events';
|
|
8
|
+
import type { BotSelf, InstalledTavern, Tavern, Channel, Member, Role, Message, SendMessageOptions, EditMessageOptions, GetMessagesOptions, SearchMessagesOptions, CreateChannelOptions, UpdateChannelOptions, CreateRoleOptions, UpdateRoleOptions, BotCommand, InteractionCallbackData } from './types';
|
|
9
|
+
export declare class RESTClient extends EventEmitter {
|
|
10
|
+
private token;
|
|
11
|
+
private baseUrl;
|
|
12
|
+
private buckets;
|
|
13
|
+
private globalRateLimit;
|
|
14
|
+
constructor(token: string, baseUrl?: string);
|
|
15
|
+
/** Update the bot token (used when login() provides a new token). */
|
|
16
|
+
setToken(token: string): void;
|
|
17
|
+
get<T = unknown>(path: string, query?: Record<string, string | number | undefined>): Promise<T>;
|
|
18
|
+
post<T = unknown>(path: string, body?: unknown): Promise<T>;
|
|
19
|
+
patch<T = unknown>(path: string, body?: unknown): Promise<T>;
|
|
20
|
+
put<T = unknown>(path: string, body?: unknown): Promise<T>;
|
|
21
|
+
delete<T = unknown>(path: string, body?: unknown): Promise<T>;
|
|
22
|
+
/** Get the bot's own profile and installed taverns. */
|
|
23
|
+
getSelf(): Promise<BotSelf>;
|
|
24
|
+
/** Get the list of taverns the bot is installed in. */
|
|
25
|
+
getMyTaverns(): Promise<InstalledTavern[]>;
|
|
26
|
+
/** Get a tavern by ID. */
|
|
27
|
+
getTavern(tavernId: string): Promise<Tavern>;
|
|
28
|
+
/** List all channels in a tavern. */
|
|
29
|
+
getChannels(tavernId: string): Promise<Channel[]>;
|
|
30
|
+
/** Create a new channel in a tavern. */
|
|
31
|
+
createChannel(tavernId: string, options: CreateChannelOptions): Promise<Channel>;
|
|
32
|
+
/** Update a channel. */
|
|
33
|
+
updateChannel(tavernId: string, channelId: string, options: UpdateChannelOptions): Promise<Channel>;
|
|
34
|
+
/** Delete a channel. */
|
|
35
|
+
deleteChannel(tavernId: string, channelId: string): Promise<void>;
|
|
36
|
+
/** Send a message to a channel. */
|
|
37
|
+
sendMessage(tavernId: string, channelId: string, options: SendMessageOptions): Promise<Message>;
|
|
38
|
+
/** Get messages from a channel. */
|
|
39
|
+
getMessages(tavernId: string, channelId: string, options?: GetMessagesOptions): Promise<Message[]>;
|
|
40
|
+
/** Edit a message. */
|
|
41
|
+
editMessage(tavernId: string, messageId: string, options: EditMessageOptions): Promise<Message>;
|
|
42
|
+
/** Delete a message. */
|
|
43
|
+
deleteMessage(tavernId: string, messageId: string): Promise<void>;
|
|
44
|
+
/** Pin a message. */
|
|
45
|
+
pinMessage(tavernId: string, messageId: string): Promise<void>;
|
|
46
|
+
/** Unpin a message. */
|
|
47
|
+
unpinMessage(tavernId: string, messageId: string): Promise<void>;
|
|
48
|
+
/** Get pinned messages in a channel. */
|
|
49
|
+
getPinnedMessages(tavernId: string, channelId: string): Promise<Message[]>;
|
|
50
|
+
/** Search messages in a tavern or specific channel. */
|
|
51
|
+
searchMessages(tavernId: string, options: SearchMessagesOptions): Promise<{
|
|
52
|
+
messages: Message[];
|
|
53
|
+
total: number;
|
|
54
|
+
}>;
|
|
55
|
+
/** Add a reaction to a message. */
|
|
56
|
+
addReaction(tavernId: string, messageId: string, emoji: string): Promise<void>;
|
|
57
|
+
/** Remove a reaction from a message. */
|
|
58
|
+
removeReaction(tavernId: string, messageId: string, emoji: string): Promise<void>;
|
|
59
|
+
/** Bulk delete messages in a channel. */
|
|
60
|
+
bulkDeleteMessages(tavernId: string, channelId: string, messageIds: string[]): Promise<void>;
|
|
61
|
+
/** List members of a tavern. */
|
|
62
|
+
getMembers(tavernId: string, options?: {
|
|
63
|
+
limit?: number;
|
|
64
|
+
after?: string;
|
|
65
|
+
}): Promise<Member[]>;
|
|
66
|
+
/** Get a specific member. */
|
|
67
|
+
getMember(tavernId: string, userId: string): Promise<Member>;
|
|
68
|
+
/** Kick a member from a tavern. */
|
|
69
|
+
kickMember(tavernId: string, userId: string, reason?: string): Promise<void>;
|
|
70
|
+
/** Ban a member from a tavern. */
|
|
71
|
+
banMember(tavernId: string, userId: string, reason?: string): Promise<void>;
|
|
72
|
+
/** Unban a member from a tavern. */
|
|
73
|
+
unbanMember(tavernId: string, userId: string): Promise<void>;
|
|
74
|
+
/** List roles in a tavern. */
|
|
75
|
+
getRoles(tavernId: string): Promise<Role[]>;
|
|
76
|
+
/** Create a new role in a tavern. */
|
|
77
|
+
createRole(tavernId: string, options: CreateRoleOptions): Promise<Role>;
|
|
78
|
+
/** Update a role. */
|
|
79
|
+
updateRole(tavernId: string, roleId: string, options: UpdateRoleOptions): Promise<Role>;
|
|
80
|
+
/** Delete a role. */
|
|
81
|
+
deleteRole(tavernId: string, roleId: string): Promise<void>;
|
|
82
|
+
/** Add a role to a member. */
|
|
83
|
+
addMemberRole(tavernId: string, userId: string, roleId: string): Promise<void>;
|
|
84
|
+
/** Remove a role from a member. */
|
|
85
|
+
removeMemberRole(tavernId: string, userId: string, roleId: string): Promise<void>;
|
|
86
|
+
/** Create a thread in a channel. */
|
|
87
|
+
createThread(tavernId: string, channelId: string, options: {
|
|
88
|
+
name: string;
|
|
89
|
+
rootMessageId?: string;
|
|
90
|
+
starterMessage?: string;
|
|
91
|
+
autoArchiveDuration?: number;
|
|
92
|
+
isPrivate?: boolean;
|
|
93
|
+
}): Promise<Channel>;
|
|
94
|
+
/** List threads in a channel. */
|
|
95
|
+
getThreads(tavernId: string, channelId: string): Promise<Channel[]>;
|
|
96
|
+
/** Get active threads across the tavern. */
|
|
97
|
+
getActiveThreads(tavernId: string): Promise<Channel[]>;
|
|
98
|
+
/** Register or overwrite all slash commands for this bot. */
|
|
99
|
+
registerCommands(commands: Omit<BotCommand, 'id' | 'version'>[]): Promise<BotCommand[]>;
|
|
100
|
+
/** Get all registered commands for this bot. */
|
|
101
|
+
getCommands(): Promise<BotCommand[]>;
|
|
102
|
+
/** Delete a registered command by ID. */
|
|
103
|
+
deleteCommand(commandId: string): Promise<void>;
|
|
104
|
+
/** Reply to an interaction. */
|
|
105
|
+
replyToInteraction(interactionId: string, data: InteractionCallbackData): Promise<void>;
|
|
106
|
+
/** Acknowledge an interaction with a deferred response (shows loading state). */
|
|
107
|
+
deferInteraction(interactionId: string): Promise<void>;
|
|
108
|
+
/** Send a follow-up message after replying or deferring an interaction. */
|
|
109
|
+
followUpInteraction(interactionId: string, data: InteractionCallbackData): Promise<void>;
|
|
110
|
+
private request;
|
|
111
|
+
private updateRateLimits;
|
|
112
|
+
private parseRetryAfter;
|
|
113
|
+
private sleep;
|
|
114
|
+
}
|
|
115
|
+
export declare class TavernAPIError extends Error {
|
|
116
|
+
readonly status: number;
|
|
117
|
+
readonly body: string;
|
|
118
|
+
constructor(message: string, status: number, body: string);
|
|
119
|
+
}
|