@koishijs/plugin-adapter-discord 2.0.0 → 2.0.1

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/lib/bot.d.ts CHANGED
@@ -2,10 +2,15 @@
2
2
  /// <reference types="node" />
3
3
  import { Adapter, Bot, Schema, Quester } from 'koishi';
4
4
  import { AdapterConfig } from './utils';
5
- import { Sender } from './sender';
6
- import * as Discord from './types';
7
- export interface BotConfig extends Bot.BaseConfig, Sender.Config {
5
+ import { Internal } from './types';
6
+ interface PrivilegedIntents {
7
+ members?: boolean;
8
+ presence?: boolean;
9
+ }
10
+ export interface BotConfig extends Bot.BaseConfig, Quester.Config {
8
11
  token: string;
12
+ gateway?: string;
13
+ intents?: PrivilegedIntents;
9
14
  }
10
15
  export declare const BotConfig: Schema<unknown, any>;
11
16
  export declare class DiscordBot extends Bot<BotConfig> {
@@ -14,8 +19,9 @@ export declare class DiscordBot extends Bot<BotConfig> {
14
19
  _ping: NodeJS.Timeout;
15
20
  _sessionId: string;
16
21
  http: Quester;
17
- internal: Discord.Internal;
22
+ internal: Internal;
18
23
  constructor(adapter: Adapter, config: BotConfig);
24
+ getIntents(): number;
19
25
  getSelf(): Promise<Bot.User>;
20
26
  private parseQuote;
21
27
  sendMessage(channelId: string, content: string, guildId?: string): Promise<string[]>;
@@ -38,3 +44,4 @@ export declare class DiscordBot extends Bot<BotConfig> {
38
44
  getGuildList(): Promise<Bot.Guild[]>;
39
45
  getChannelList(guildId: string): Promise<Bot.Channel[]>;
40
46
  }
47
+ export {};
package/lib/index.d.ts CHANGED
@@ -8,9 +8,6 @@ export * from './sender';
8
8
  export * from './utils';
9
9
  export * from './ws';
10
10
  declare module 'koishi' {
11
- interface Modules {
12
- 'adapter-discord': typeof import('.');
13
- }
14
11
  interface Session {
15
12
  discord?: Discord.GatewayPayload & Discord.Internal;
16
13
  }
package/lib/index.js CHANGED
@@ -61,15 +61,155 @@ var import_koishi6 = __toModule(require("koishi"));
61
61
  var import_koishi4 = __toModule(require("koishi"));
62
62
 
63
63
  // plugins/adapter/discord/src/utils.ts
64
+ var import_koishi2 = __toModule(require("koishi"));
65
+
66
+ // plugins/adapter/discord/src/sender.ts
67
+ var import_fs = __toModule(require("fs"));
68
+ var import_path = __toModule(require("path"));
69
+ var import_file_type = __toModule(require("file-type"));
70
+ var import_form_data = __toModule(require("form-data"));
71
+ var import_es_aggregate_error = __toModule(require("es-aggregate-error"));
64
72
  var import_koishi = __toModule(require("koishi"));
65
- var AdapterConfig = import_koishi.Schema.intersect([
66
- import_koishi.Schema.object({
67
- intents: import_koishi.Schema.object({
68
- members: import_koishi.Schema.boolean()
69
- })
70
- }),
71
- import_koishi.App.Config.Request,
72
- import_koishi.Adapter.WebSocketClient.Config
73
+ var _Sender = class {
74
+ constructor(bot, url) {
75
+ this.bot = bot;
76
+ this.url = url;
77
+ this.results = [];
78
+ this.errors = [];
79
+ }
80
+ static from(bot, url) {
81
+ const sender = new _Sender(bot, url);
82
+ return sender.sendMessage.bind(sender);
83
+ }
84
+ async post(data, headers) {
85
+ try {
86
+ const result = await this.bot.http.post(this.url, data, { headers });
87
+ this.results.push(result.id);
88
+ } catch (e) {
89
+ this.errors.push(e);
90
+ }
91
+ }
92
+ async sendEmbed(fileBuffer, payload_json = {}, filename) {
93
+ const fd = new import_form_data.default();
94
+ const type = await (0, import_file_type.fromBuffer)(fileBuffer);
95
+ filename || (filename = "file." + type.ext);
96
+ fd.append("file", fileBuffer, filename);
97
+ fd.append("payload_json", JSON.stringify(payload_json));
98
+ return this.post(fd, fd.getHeaders());
99
+ }
100
+ async sendContent(content, addition) {
101
+ return this.post(__spreadProps(__spreadValues({}, addition), { content }));
102
+ }
103
+ async sendAsset(type, data, addition) {
104
+ const { handleMixedContent, handleExternalAsset } = this.bot.adapter.config;
105
+ if (handleMixedContent === "separate" && addition.content) {
106
+ await this.post(addition);
107
+ addition.content = "";
108
+ }
109
+ if (data.url.startsWith("file://")) {
110
+ const filename = (0, import_path.basename)(data.url.slice(7));
111
+ return await this.sendEmbed((0, import_fs.readFileSync)(data.url.slice(7)), addition, data.file || filename);
112
+ } else if (data.url.startsWith("base64://")) {
113
+ const a = Buffer.from(data.url.slice(9), "base64");
114
+ return await this.sendEmbed(a, addition, data.file);
115
+ }
116
+ const sendDirect = /* @__PURE__ */ __name(async () => {
117
+ if (addition.content) {
118
+ await this.post(addition);
119
+ }
120
+ return this.post(__spreadProps(__spreadValues({}, addition), { content: data.url }));
121
+ }, "sendDirect");
122
+ const sendDownload = /* @__PURE__ */ __name(async () => {
123
+ const filename = (0, import_path.basename)(data.url);
124
+ const buffer = await this.bot.app.http.get(data.url, {
125
+ headers: { accept: type + "/*" },
126
+ responseType: "arraybuffer"
127
+ });
128
+ return this.sendEmbed(buffer, addition, data.file || filename);
129
+ }, "sendDownload");
130
+ const mode = data.mode || handleExternalAsset;
131
+ if (mode === "download" || handleMixedContent === "attach" && addition.content || type === "file") {
132
+ return sendDownload();
133
+ } else if (mode === "direct") {
134
+ return sendDirect();
135
+ }
136
+ return await this.bot.app.http.head(data.url, {
137
+ headers: { accept: type + "/*" }
138
+ }).then((headers) => {
139
+ if (headers["content-type"].startsWith(type)) {
140
+ return sendDirect();
141
+ } else {
142
+ return sendDownload();
143
+ }
144
+ }, sendDownload);
145
+ }
146
+ async sendMessage(content, addition = {}) {
147
+ const chain = import_koishi.segment.parse(content);
148
+ let textBuffer = "";
149
+ delete addition.content;
150
+ const sendBuffer = /* @__PURE__ */ __name(async () => {
151
+ const content2 = textBuffer.trim();
152
+ if (!content2)
153
+ return;
154
+ await this.post(__spreadProps(__spreadValues({}, addition), { content: content2 }));
155
+ textBuffer = "";
156
+ }, "sendBuffer");
157
+ for (const code of chain) {
158
+ const { type, data } = code;
159
+ if (type === "text") {
160
+ textBuffer += data.content.trim();
161
+ } else if (type === "at" && data.id) {
162
+ textBuffer += `<@${data.id}>`;
163
+ } else if (type === "at" && data.type === "all") {
164
+ textBuffer += `@everyone`;
165
+ } else if (type === "at" && data.type === "here") {
166
+ textBuffer += `@here`;
167
+ } else if (type === "sharp" && data.id) {
168
+ textBuffer += `<#${data.id}>`;
169
+ } else if (type === "face" && data.name && data.id) {
170
+ textBuffer += `<:${data.name}:${data.id}>`;
171
+ } else if ((type === "image" || type === "video") && data.url) {
172
+ await this.sendAsset(type, data, __spreadProps(__spreadValues({}, addition), {
173
+ content: textBuffer.trim()
174
+ }));
175
+ textBuffer = "";
176
+ } else if (type === "share") {
177
+ await sendBuffer();
178
+ await this.post(__spreadProps(__spreadValues({}, addition), {
179
+ embeds: [__spreadValues({}, data)]
180
+ }));
181
+ } else if (type === "record") {
182
+ await this.sendAsset("file", data, __spreadProps(__spreadValues({}, addition), {
183
+ content: textBuffer.trim()
184
+ }));
185
+ textBuffer = "";
186
+ }
187
+ }
188
+ await sendBuffer();
189
+ if (!this.errors.length)
190
+ return this.results;
191
+ throw new import_es_aggregate_error.default(this.errors);
192
+ }
193
+ };
194
+ var Sender = _Sender;
195
+ __name(Sender, "Sender");
196
+ Sender.Config = import_koishi.Schema.object({
197
+ handleExternalAsset: import_koishi.Schema.union([
198
+ import_koishi.Schema.const("download").description("先下载后发送"),
199
+ import_koishi.Schema.const("direct").description("直接发送链接"),
200
+ import_koishi.Schema.const("auto").description("发送一个 HEAD 请求,根据返回的 Content-Type 决定发送方式")
201
+ ]).description("发送外链资源时采用的方式。").default("auto"),
202
+ handleMixedContent: import_koishi.Schema.union([
203
+ import_koishi.Schema.const("separate").description("将每个不同形式的内容分开发送"),
204
+ import_koishi.Schema.const("attach").description("图片前如果有文本内容,则将文本作为图片的附带信息进行发送"),
205
+ import_koishi.Schema.const("auto").description("如果图片本身采用直接发送则与前面的文本分开,否则将文本作为图片的附带信息发送")
206
+ ]).description("发送图文等混合内容时采用的方式。").default("auto")
207
+ }).description("发送设置");
208
+
209
+ // plugins/adapter/discord/src/utils.ts
210
+ var AdapterConfig = import_koishi2.Schema.intersect([
211
+ Sender.Config,
212
+ import_koishi2.Adapter.WebSocketClient.Config
73
213
  ]);
74
214
  var adaptUser = /* @__PURE__ */ __name((user) => ({
75
215
  userId: user.id,
@@ -109,40 +249,40 @@ function adaptMessage(bot, meta, session = {}) {
109
249
  session.content = meta.content.replace(/<@[!&](.+?)>/, (_, id) => {
110
250
  var _a2;
111
251
  if (meta.mention_roles.includes(id)) {
112
- return (0, import_koishi.segment)("at", { role: id });
252
+ return (0, import_koishi2.segment)("at", { role: id });
113
253
  } else {
114
254
  const user = (_a2 = meta.mentions) == null ? void 0 : _a2.find((u) => u.id === id);
115
- return import_koishi.segment.at(id, { name: user == null ? void 0 : user.username });
255
+ return import_koishi2.segment.at(id, { name: user == null ? void 0 : user.username });
116
256
  }
117
- }).replace(/<:(.*):(.+?)>/, (_, name, id) => (0, import_koishi.segment)("face", { id, name })).replace(/<a:(.*):(.+?)>/, (_, name, id) => (0, import_koishi.segment)("face", { id, name, animated: true })).replace(/@everyone/, () => (0, import_koishi.segment)("at", { type: "all" })).replace(/@here/, () => (0, import_koishi.segment)("at", { type: "here" })).replace(/<#(.+?)>/, (_, id) => {
257
+ }).replace(/<:(.*):(.+?)>/, (_, name, id) => (0, import_koishi2.segment)("face", { id, name })).replace(/<a:(.*):(.+?)>/, (_, name, id) => (0, import_koishi2.segment)("face", { id, name, animated: true })).replace(/@everyone/, () => (0, import_koishi2.segment)("at", { type: "all" })).replace(/@here/, () => (0, import_koishi2.segment)("at", { type: "here" })).replace(/<#(.+?)>/, (_, id) => {
118
258
  var _a2;
119
259
  const channel = (_a2 = meta.mention_channels) == null ? void 0 : _a2.find((c) => c.id === id);
120
- return import_koishi.segment.sharp(id, { name: channel == null ? void 0 : channel.name });
260
+ return import_koishi2.segment.sharp(id, { name: channel == null ? void 0 : channel.name });
121
261
  });
122
262
  }
123
263
  if ((_c = meta.attachments) == null ? void 0 : _c.length) {
124
264
  session.content += meta.attachments.map((v) => {
125
265
  var _a2, _b2, _c2;
126
266
  if (v.height && v.width && ((_a2 = v.content_type) == null ? void 0 : _a2.startsWith("image/"))) {
127
- return (0, import_koishi.segment)("image", {
267
+ return (0, import_koishi2.segment)("image", {
128
268
  url: v.url,
129
269
  proxy_url: v.proxy_url,
130
270
  file: v.filename
131
271
  });
132
272
  } else if (v.height && v.width && ((_b2 = v.content_type) == null ? void 0 : _b2.startsWith("video/"))) {
133
- return (0, import_koishi.segment)("video", {
273
+ return (0, import_koishi2.segment)("video", {
134
274
  url: v.url,
135
275
  proxy_url: v.proxy_url,
136
276
  file: v.filename
137
277
  });
138
278
  } else if ((_c2 = v.content_type) == null ? void 0 : _c2.startsWith("audio/")) {
139
- return (0, import_koishi.segment)("record", {
279
+ return (0, import_koishi2.segment)("record", {
140
280
  url: v.url,
141
281
  proxy_url: v.proxy_url,
142
282
  file: v.filename
143
283
  });
144
284
  } else {
145
- return (0, import_koishi.segment)("file", {
285
+ return (0, import_koishi2.segment)("file", {
146
286
  url: v.url,
147
287
  proxy_url: v.proxy_url,
148
288
  file: v.filename
@@ -152,13 +292,13 @@ function adaptMessage(bot, meta, session = {}) {
152
292
  }
153
293
  for (const embed of meta.embeds) {
154
294
  if (embed.image) {
155
- session.content += (0, import_koishi.segment)("image", { url: embed.image.url, proxy_url: embed.image.proxy_url });
295
+ session.content += (0, import_koishi2.segment)("image", { url: embed.image.url, proxy_url: embed.image.proxy_url });
156
296
  }
157
297
  if (embed.thumbnail) {
158
- session.content += (0, import_koishi.segment)("image", { url: embed.thumbnail.url, proxy_url: embed.thumbnail.proxy_url });
298
+ session.content += (0, import_koishi2.segment)("image", { url: embed.thumbnail.url, proxy_url: embed.thumbnail.proxy_url });
159
299
  }
160
300
  if (embed.video) {
161
- session.content += (0, import_koishi.segment)("video", { url: embed.video.url, proxy_url: embed.video.proxy_url });
301
+ session.content += (0, import_koishi2.segment)("video", { url: embed.video.url, proxy_url: embed.video.proxy_url });
162
302
  }
163
303
  }
164
304
  return session;
@@ -170,7 +310,7 @@ function adaptMessageSession(bot, meta, session = {}) {
170
310
  session.timestamp = new Date(meta.timestamp).valueOf() || Date.now();
171
311
  if (meta.message_reference) {
172
312
  const { message_id, channel_id } = meta.message_reference;
173
- session.content = (0, import_koishi.segment)("quote", { id: message_id, channelId: channel_id }) + session.content;
313
+ session.content = (0, import_koishi2.segment)("quote", { id: message_id, channelId: channel_id }) + session.content;
174
314
  }
175
315
  return session;
176
316
  }
@@ -232,140 +372,10 @@ async function adaptSession(bot, input) {
232
372
  } else {
233
373
  return;
234
374
  }
235
- return new import_koishi.Session(bot, session);
375
+ return new import_koishi2.Session(bot, session);
236
376
  }
237
377
  __name(adaptSession, "adaptSession");
238
378
 
239
- // plugins/adapter/discord/src/sender.ts
240
- var import_fs = __toModule(require("fs"));
241
- var import_path = __toModule(require("path"));
242
- var import_file_type = __toModule(require("file-type"));
243
- var import_form_data = __toModule(require("form-data"));
244
- var import_es_aggregate_error = __toModule(require("es-aggregate-error"));
245
- var import_koishi2 = __toModule(require("koishi"));
246
- var Sender = class {
247
- constructor(bot, url) {
248
- this.bot = bot;
249
- this.url = url;
250
- this.results = [];
251
- this.errors = [];
252
- }
253
- static from(bot, url) {
254
- const sender = new Sender(bot, url);
255
- return sender.sendMessage.bind(sender);
256
- }
257
- async post(data, headers) {
258
- try {
259
- const result = await this.bot.http.post(this.url, data, { headers });
260
- this.results.push(result.id);
261
- } catch (e) {
262
- this.errors.push(e);
263
- }
264
- }
265
- async sendEmbed(fileBuffer, payload_json = {}, filename) {
266
- const fd = new import_form_data.default();
267
- const type = await (0, import_file_type.fromBuffer)(fileBuffer);
268
- filename || (filename = "file." + type.ext);
269
- fd.append("file", fileBuffer, filename);
270
- fd.append("payload_json", JSON.stringify(payload_json));
271
- return this.post(fd, fd.getHeaders());
272
- }
273
- async sendContent(content, addition) {
274
- return this.post(__spreadProps(__spreadValues({}, addition), { content }));
275
- }
276
- async sendAsset(type, data, addition) {
277
- const { handleMixedContent, handleExternalAsset } = this.bot.config;
278
- if (handleMixedContent === "separate" && addition.content) {
279
- await this.post(addition);
280
- addition.content = "";
281
- }
282
- if (data.url.startsWith("file://")) {
283
- const filename = (0, import_path.basename)(data.url.slice(7));
284
- return await this.sendEmbed((0, import_fs.readFileSync)(data.url.slice(7)), addition, data.file || filename);
285
- } else if (data.url.startsWith("base64://")) {
286
- const a = Buffer.from(data.url.slice(9), "base64");
287
- return await this.sendEmbed(a, addition, data.file);
288
- }
289
- const sendDirect = /* @__PURE__ */ __name(async () => {
290
- if (addition.content) {
291
- await this.post(addition);
292
- }
293
- return this.post(__spreadProps(__spreadValues({}, addition), { content: data.url }));
294
- }, "sendDirect");
295
- const sendDownload = /* @__PURE__ */ __name(async () => {
296
- const filename = (0, import_path.basename)(data.url);
297
- const buffer = await this.bot.app.http.get(data.url, {
298
- headers: { accept: type + "/*" },
299
- responseType: "arraybuffer"
300
- });
301
- return this.sendEmbed(buffer, addition, data.file || filename);
302
- }, "sendDownload");
303
- const mode = data.mode || handleExternalAsset;
304
- if (mode === "download" || handleMixedContent === "attach" && addition.content || type === "file") {
305
- return sendDownload();
306
- } else if (mode === "direct") {
307
- return sendDirect();
308
- }
309
- return await this.bot.app.http.head(data.url, {
310
- headers: { accept: type + "/*" }
311
- }).then((headers) => {
312
- if (headers["content-type"].startsWith(type)) {
313
- return sendDirect();
314
- } else {
315
- return sendDownload();
316
- }
317
- }, sendDownload);
318
- }
319
- async sendMessage(content, addition = {}) {
320
- const chain = import_koishi2.segment.parse(content);
321
- let textBuffer = "";
322
- delete addition.content;
323
- const sendBuffer = /* @__PURE__ */ __name(async () => {
324
- const content2 = textBuffer.trim();
325
- if (!content2)
326
- return;
327
- await this.post(__spreadProps(__spreadValues({}, addition), { content: content2 }));
328
- textBuffer = "";
329
- }, "sendBuffer");
330
- for (const code of chain) {
331
- const { type, data } = code;
332
- if (type === "text") {
333
- textBuffer += data.content.trim();
334
- } else if (type === "at" && data.id) {
335
- textBuffer += `<@${data.id}>`;
336
- } else if (type === "at" && data.type === "all") {
337
- textBuffer += `@everyone`;
338
- } else if (type === "at" && data.type === "here") {
339
- textBuffer += `@here`;
340
- } else if (type === "sharp" && data.id) {
341
- textBuffer += `<#${data.id}>`;
342
- } else if (type === "face" && data.name && data.id) {
343
- textBuffer += `<:${data.name}:${data.id}>`;
344
- } else if ((type === "image" || type === "video") && data.url) {
345
- await this.sendAsset(type, data, __spreadProps(__spreadValues({}, addition), {
346
- content: textBuffer.trim()
347
- }));
348
- textBuffer = "";
349
- } else if (type === "share") {
350
- await sendBuffer();
351
- await this.post(__spreadProps(__spreadValues({}, addition), {
352
- embeds: [__spreadValues({}, data)]
353
- }));
354
- } else if (type === "record") {
355
- await this.sendAsset("file", data, __spreadProps(__spreadValues({}, addition), {
356
- content: textBuffer.trim()
357
- }));
358
- textBuffer = "";
359
- }
360
- }
361
- await sendBuffer();
362
- if (!this.errors.length)
363
- return this.results;
364
- throw new import_es_aggregate_error.default(this.errors);
365
- }
366
- };
367
- __name(Sender, "Sender");
368
-
369
379
  // plugins/adapter/discord/src/types/index.ts
370
380
  var types_exports = {};
371
381
  __export(types_exports, {
@@ -1317,30 +1327,41 @@ Internal.define({
1317
1327
  // plugins/adapter/discord/src/bot.ts
1318
1328
  var BotConfig = import_koishi4.Schema.intersect([
1319
1329
  import_koishi4.Schema.object({
1320
- token: import_koishi4.Schema.string().description("机器人的用户令牌。").required(),
1321
- handleExternalAsset: import_koishi4.Schema.union([
1322
- import_koishi4.Schema.const("download").description("先下载后发送"),
1323
- import_koishi4.Schema.const("direct").description("直接发送链接"),
1324
- import_koishi4.Schema.const("auto").description("发送一个 HEAD 请求,如果返回的 Content-Type 正确,则直接发送链接,否则先下载后发送")
1325
- ]).description("发送外链资源时采用的方式。").default("auto"),
1326
- handleMixedContent: import_koishi4.Schema.union([
1327
- import_koishi4.Schema.const("separate").description("将每个不同形式的内容分开发送"),
1328
- import_koishi4.Schema.const("attach").description("图片前如果有文本内容,则将文本作为图片的附带信息进行发送"),
1329
- import_koishi4.Schema.const("auto").description("如果图片本身采用直接发送则与前面的文本分开,否则将文本作为图片的附带信息发送")
1330
- ]).description("发送图文等混合内容时采用的方式。").default("auto")
1330
+ token: import_koishi4.Schema.string().description("机器人的用户令牌。").role("secret").required()
1331
1331
  }),
1332
- import_koishi4.App.Config.Request
1332
+ import_koishi4.Schema.object({
1333
+ gateway: import_koishi4.Schema.string().role("url").default("wss://gateway.discord.gg/?v=8&encoding=json").description("要连接的 WebSocket 网关。"),
1334
+ intents: import_koishi4.Schema.object({
1335
+ members: import_koishi4.Schema.boolean().description("启用 GUILD_MEMBERS 推送。").default(true),
1336
+ presence: import_koishi4.Schema.boolean().description("启用 GUILD_PRESENCES 推送。").default(false)
1337
+ })
1338
+ }).description("推送设置"),
1339
+ import_koishi4.Quester.createSchema({
1340
+ endpoint: "https://discord.com/api/v8"
1341
+ })
1333
1342
  ]);
1334
1343
  var DiscordBot = class extends import_koishi4.Bot {
1335
1344
  constructor(adapter, config) {
1336
1345
  super(adapter, config);
1337
1346
  this._d = 0;
1338
1347
  this._sessionId = "";
1339
- this.http = adapter.http.extend({
1340
- headers: { Authorization: `Bot ${config.token}` }
1341
- });
1348
+ this.http = adapter.ctx.http.extend(__spreadProps(__spreadValues({}, config), {
1349
+ headers: __spreadValues({
1350
+ Authorization: `Bot ${config.token}`
1351
+ }, config.headers)
1352
+ }));
1342
1353
  this.internal = new Internal(this.http);
1343
1354
  }
1355
+ getIntents() {
1356
+ let intents = 0 | GatewayIntent.GUILD_MESSAGES | GatewayIntent.GUILD_MESSAGE_REACTIONS | GatewayIntent.DIRECT_MESSAGES | GatewayIntent.DIRECT_MESSAGE_REACTIONS;
1357
+ if (this.config.intents.members) {
1358
+ intents |= GatewayIntent.GUILD_MEMBERS;
1359
+ }
1360
+ if (this.config.intents.presence) {
1361
+ intents |= GatewayIntent.GUILD_PRESENCES;
1362
+ }
1363
+ return intents;
1364
+ }
1344
1365
  async getSelf() {
1345
1366
  const data = await this.internal.getCurrentUser();
1346
1367
  return adaptUser(data);
@@ -1444,14 +1465,8 @@ var import_koishi5 = __toModule(require("koishi"));
1444
1465
  var import_ws = __toModule(require("ws"));
1445
1466
  var logger = new import_koishi5.Logger("discord");
1446
1467
  var WebSocketClient = class extends import_koishi5.Adapter.WebSocketClient {
1447
- constructor(ctx, config) {
1448
- super(ctx, config);
1449
- this.http = ctx.http.extend(__spreadValues({
1450
- endpoint: "https://discord.com/api/v8"
1451
- }, config.request));
1452
- }
1453
- prepare() {
1454
- return new import_ws.default("wss://gateway.discord.gg/?v=8&encoding=json");
1468
+ prepare(bot) {
1469
+ return new import_ws.default(bot.config.gateway);
1455
1470
  }
1456
1471
  heartbeat(bot) {
1457
1472
  logger.debug(`heartbeat d ${bot._d}`);
@@ -1460,13 +1475,6 @@ var WebSocketClient = class extends import_koishi5.Adapter.WebSocketClient {
1460
1475
  d: bot._d
1461
1476
  }));
1462
1477
  }
1463
- getIntents() {
1464
- let intents = 0 | GatewayIntent.GUILD_MESSAGES | GatewayIntent.GUILD_MESSAGE_REACTIONS | GatewayIntent.DIRECT_MESSAGES | GatewayIntent.DIRECT_MESSAGE_REACTIONS;
1465
- if (this.config.intents.members !== false) {
1466
- intents |= GatewayIntent.GUILD_MEMBERS;
1467
- }
1468
- return intents;
1469
- }
1470
1478
  accept(bot) {
1471
1479
  if (bot._sessionId) {
1472
1480
  logger.debug("resuming");
@@ -1480,10 +1488,9 @@ var WebSocketClient = class extends import_koishi5.Adapter.WebSocketClient {
1480
1488
  }));
1481
1489
  }
1482
1490
  bot.socket.on("message", async (data) => {
1483
- data = data.toString();
1484
1491
  let parsed;
1485
1492
  try {
1486
- parsed = JSON.parse(data);
1493
+ parsed = JSON.parse(data.toString());
1487
1494
  } catch (error) {
1488
1495
  return logger.warn("cannot parse message", data);
1489
1496
  }
@@ -1501,7 +1508,7 @@ var WebSocketClient = class extends import_koishi5.Adapter.WebSocketClient {
1501
1508
  token: bot.config.token,
1502
1509
  properties: {},
1503
1510
  compress: false,
1504
- intents: this.getIntents()
1511
+ intents: bot.getIntents()
1505
1512
  }
1506
1513
  }));
1507
1514
  }