@koishijs/plugin-adapter-discord 2.0.7 → 2.1.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/lib/bot.d.ts CHANGED
@@ -40,8 +40,10 @@ export declare class DiscordBot extends Bot<BotConfig> {
40
40
  discriminator?: string;
41
41
  isBot?: boolean;
42
42
  }>;
43
+ kickGuildMember(guildId: string, userId: string): Promise<void>;
43
44
  getGuild(guildId: string): Promise<Bot.Guild>;
44
45
  getGuildList(): Promise<Bot.Guild[]>;
45
46
  getChannelList(guildId: string): Promise<Bot.Channel[]>;
47
+ getChannelMessageHistory(channelId: string, before?: string): Promise<Bot.Message[]>;
46
48
  }
47
49
  export {};
package/lib/index.js CHANGED
@@ -51,9 +51,11 @@ __export(exports, {
51
51
  adaptChannel: () => adaptChannel,
52
52
  adaptGroup: () => adaptGroup,
53
53
  adaptMessage: () => adaptMessage,
54
+ adaptMessageSession: () => adaptMessageSession,
54
55
  adaptSession: () => adaptSession,
55
56
  adaptUser: () => adaptUser,
56
- default: () => src_default
57
+ default: () => src_default,
58
+ prepareMessageSession: () => prepareMessageSession
57
59
  });
58
60
  var import_koishi6 = __toModule(require("koishi"));
59
61
 
@@ -244,7 +246,7 @@ function adaptMessage(meta, session = {}) {
244
246
  }
245
247
  session.content = "";
246
248
  if (meta.content) {
247
- session.content = meta.content.replace(/<@[!&](.+?)>/g, (_, id) => {
249
+ session.content = meta.content.replace(/<@[!&]?(.+?)>/g, (_, id) => {
248
250
  var _a2;
249
251
  if (meta.mention_roles.includes(id)) {
250
252
  return (0, import_koishi2.segment)("at", { role: id });
@@ -302,7 +304,7 @@ function adaptMessage(meta, session = {}) {
302
304
  return session;
303
305
  }
304
306
  __name(adaptMessage, "adaptMessage");
305
- function adaptMessageSession(bot, meta, session = {}) {
307
+ function adaptMessageSession(meta, session = {}) {
306
308
  adaptMessage(meta, session);
307
309
  session.messageId = meta.id;
308
310
  session.timestamp = new Date(meta.timestamp).valueOf() || Date.now();
@@ -338,14 +340,14 @@ async function adaptSession(bot, input) {
338
340
  if (input.t === "MESSAGE_CREATE") {
339
341
  session.type = "message";
340
342
  prepareMessageSession(session, input.d);
341
- adaptMessageSession(bot, input.d, session);
343
+ adaptMessageSession(input.d, session);
342
344
  if (session.userId === bot.selfId)
343
345
  return;
344
346
  } else if (input.t === "MESSAGE_UPDATE") {
345
347
  session.type = "message-updated";
346
348
  prepareMessageSession(session, input.d);
347
349
  const msg = await bot.internal.getChannelMessage(input.d.channel_id, input.d.id);
348
- adaptMessageSession(bot, msg, session);
350
+ adaptMessageSession(msg, session);
349
351
  if (session.userId === bot.selfId)
350
352
  return;
351
353
  } else if (input.t === "MESSAGE_DELETE") {
@@ -367,6 +369,11 @@ async function adaptSession(bot, input) {
367
369
  session.type = "reaction-deleted";
368
370
  session.subtype = "emoji";
369
371
  prepareReactionSession(session, input.d);
372
+ } else if (input.t === "CHANNEL_UPDATE") {
373
+ session.type = "channel-updated";
374
+ session.guildId = input.d.guild_id;
375
+ session.subtype = input.d.guild_id ? "group" : "private";
376
+ session.channelId = input.d.id;
370
377
  } else {
371
378
  return;
372
379
  }
@@ -699,7 +706,7 @@ var GatewayOpcode;
699
706
  })(GatewayOpcode || (GatewayOpcode = {}));
700
707
  var GatewayIntent;
701
708
  (function(GatewayIntent2) {
702
- GatewayIntent2[GatewayIntent2["GUILD_CREATE"] = 1] = "GUILD_CREATE";
709
+ GatewayIntent2[GatewayIntent2["GUILDS"] = 1] = "GUILDS";
703
710
  GatewayIntent2[GatewayIntent2["GUILD_MEMBERS"] = 2] = "GUILD_MEMBERS";
704
711
  GatewayIntent2[GatewayIntent2["GUILD_BANS"] = 4] = "GUILD_BANS";
705
712
  GatewayIntent2[GatewayIntent2["GUILD_EMOJIS_AND_STICKERS"] = 8] = "GUILD_EMOJIS_AND_STICKERS";
@@ -1430,6 +1437,9 @@ var DiscordBot = class extends import_koishi4.Bot {
1430
1437
  nickname: member.nick
1431
1438
  });
1432
1439
  }
1440
+ async kickGuildMember(guildId, userId) {
1441
+ return this.internal.removeGuildMember(guildId, userId);
1442
+ }
1433
1443
  async getGuild(guildId) {
1434
1444
  const data = await this.internal.getGuild(guildId);
1435
1445
  return adaptGroup(data);
@@ -1442,17 +1452,29 @@ var DiscordBot = class extends import_koishi4.Bot {
1442
1452
  const data = await this.internal.getGuildChannels(guildId);
1443
1453
  return data.map((v) => adaptChannel(v));
1444
1454
  }
1455
+ async getChannelMessageHistory(channelId, before) {
1456
+ const data = (await this.internal.getChannelMessages(channelId, {
1457
+ before,
1458
+ limit: 50
1459
+ })).reverse();
1460
+ return data.map((v) => {
1461
+ const session = {};
1462
+ prepareMessageSession(session, v);
1463
+ adaptMessageSession(v, session);
1464
+ return session;
1465
+ });
1466
+ }
1445
1467
  };
1446
1468
  __name(DiscordBot, "DiscordBot");
1447
1469
  DiscordBot.schema = AdapterConfig;
1448
1470
 
1449
1471
  // plugins/adapter/discord/src/ws.ts
1450
1472
  var import_koishi5 = __toModule(require("koishi"));
1451
- var import_ws = __toModule(require("ws"));
1452
1473
  var logger = new import_koishi5.Logger("discord");
1453
1474
  var WebSocketClient = class extends import_koishi5.Adapter.WebSocketClient {
1454
1475
  prepare(bot) {
1455
- return new import_ws.default(bot.config.gateway);
1476
+ const http = this.ctx.http.extend(bot.config);
1477
+ return http.ws(bot.config.gateway);
1456
1478
  }
1457
1479
  heartbeat(bot) {
1458
1480
  logger.debug(`heartbeat d ${bot._d}`);
@@ -1534,7 +1556,9 @@ var src_default = import_koishi6.Adapter.define("discord", DiscordBot, WebSocket
1534
1556
  adaptChannel,
1535
1557
  adaptGroup,
1536
1558
  adaptMessage,
1559
+ adaptMessageSession,
1537
1560
  adaptSession,
1538
- adaptUser
1561
+ adaptUser,
1562
+ prepareMessageSession
1539
1563
  });
1540
1564
  //# sourceMappingURL=index.js.map