koishi-plugin-subscription 0.0.6 → 0.0.7

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/index.d.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  import { Context, Dict, Schema, Service, Fragment } from 'koishi';
2
+ interface RecordsItem {
3
+ app: string;
4
+ id: string;
5
+ createdAt: Date;
6
+ }
2
7
  interface SubscriptionItem {
3
8
  id: number;
4
9
  app: string;
@@ -31,16 +36,19 @@ declare module 'koishi' {
31
36
  }
32
37
  interface Tables {
33
38
  subscription_service: SubscriptionItem;
39
+ subscription_records: RecordsItem;
34
40
  }
35
41
  }
36
42
  declare class SubscriptionService extends Service {
37
- readonly tableName = "subscription_service";
43
+ readonly subTableName = "subscription_service";
44
+ readonly recTableName = "subscription_records";
38
45
  subsConfig: Config;
39
46
  appMap: Map<string, string>;
40
47
  accountMap: Map<string, string>;
41
48
  constructor(ctx: Context, config: Config);
42
49
  initMap(): void;
43
50
  private getApp;
51
+ checkExist(app: string, id: string): Promise<boolean>;
44
52
  getAccount(app: string, account: string): string;
45
53
  getName(app: string, account: string): string;
46
54
  getAvailableAccounts(app: string): string[];
package/lib/index.js CHANGED
@@ -77,7 +77,8 @@ var SubscriptionService = class extends import_koishi.Service {
77
77
  __name(this, "SubscriptionService");
78
78
  }
79
79
  // 数据表名称
80
- tableName = "subscription_service";
80
+ subTableName = "subscription_service";
81
+ recTableName = "subscription_records";
81
82
  subsConfig;
82
83
  appMap;
83
84
  accountMap;
@@ -85,7 +86,7 @@ var SubscriptionService = class extends import_koishi.Service {
85
86
  super(ctx, "subscription");
86
87
  this.subsConfig = config;
87
88
  this.initMap();
88
- ctx.model.extend(this.tableName, {
89
+ ctx.model.extend(this.subTableName, {
89
90
  id: "unsigned",
90
91
  app: "string",
91
92
  account: "string",
@@ -95,6 +96,14 @@ var SubscriptionService = class extends import_koishi.Service {
95
96
  primary: "id",
96
97
  autoInc: true
97
98
  });
99
+ ctx.model.extend(this.recTableName, {
100
+ app: "string",
101
+ id: "string",
102
+ createdAt: "timestamp"
103
+ }, {
104
+ primary: ["app", "id"],
105
+ autoInc: false
106
+ });
98
107
  this.registerCommands(ctx, config);
99
108
  }
100
109
  initMap() {
@@ -124,6 +133,18 @@ var SubscriptionService = class extends import_koishi.Service {
124
133
  getApp(app) {
125
134
  return this.appMap.get(app.toLowerCase());
126
135
  }
136
+ async checkExist(app, id) {
137
+ app = this.getApp(app);
138
+ if ((await this.ctx.database.get(this.recTableName, { app, id })).length > 0) {
139
+ return true;
140
+ }
141
+ await this.ctx.database.upsert(this.recTableName, [{
142
+ app,
143
+ id,
144
+ createdAt: /* @__PURE__ */ new Date()
145
+ }]);
146
+ return false;
147
+ }
127
148
  getAccount(app, account) {
128
149
  return this.accountMap.get((app + account).toLowerCase());
129
150
  }
@@ -140,7 +161,7 @@ var SubscriptionService = class extends import_koishi.Service {
140
161
  // 根据应用和账号获取订阅了这个账号的所有群组
141
162
  async getSubscribedGroups(app, account) {
142
163
  app = this.getApp(app), account = this.getAccount(app, account);
143
- const subscriptions = await this.ctx.database.get(this.tableName, {
164
+ const subscriptions = await this.ctx.database.get(this.subTableName, {
144
165
  app,
145
166
  account
146
167
  });
@@ -195,15 +216,26 @@ var SubscriptionService = class extends import_koishi.Service {
195
216
  },
196
217
  user: { id: bot.selfId }
197
218
  });
198
- const messageIds = Array.isArray(content) ? (await Promise.all(content.map(
199
- (msg) => {
219
+ let messageIds = [];
220
+ if (Array.isArray(content)) {
221
+ for (const msg of content) {
200
222
  try {
201
- return retry(3, () => bot.sendMessage(selfChannelId, msg, void 0, { session }));
223
+ const ids = await retry(
224
+ 3,
225
+ () => bot.sendMessage(selfChannelId, msg, void 0, { session })
226
+ );
227
+ if (Array.isArray(ids)) messageIds.push(...ids);
228
+ else if (ids != null) messageIds.push(ids);
202
229
  } catch (e) {
203
- return Promise.resolve([]);
230
+ continue;
204
231
  }
205
232
  }
206
- ))).flat(1) : await retry(3, () => bot.sendMessage(selfChannelId, content, void 0, { session }));
233
+ } else {
234
+ messageIds = await retry(
235
+ 3,
236
+ () => bot.sendMessage(selfChannelId, content, void 0, { session })
237
+ );
238
+ }
207
239
  const forwardContent = /* @__PURE__ */ (0, import_jsx_runtime.jsx)("message", { forward: true, children: messageIds.map((id) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("message", { id: `${id}` })) });
208
240
  const sessions = targets.map(({ id, guildId, locales }) => {
209
241
  const session2 = bot.session({
@@ -231,7 +263,7 @@ var SubscriptionService = class extends import_koishi.Service {
231
263
  if (!appConfig.includes(account)) {
232
264
  return { status: false, msg: "账号不在白名单中" };
233
265
  }
234
- const existing = await this.ctx.database.get(this.tableName, {
266
+ const existing = await this.ctx.database.get(this.subTableName, {
235
267
  app,
236
268
  account,
237
269
  groupId
@@ -239,7 +271,7 @@ var SubscriptionService = class extends import_koishi.Service {
239
271
  if (existing.length > 0) {
240
272
  return { status: false, msg: "已订阅该账号" };
241
273
  }
242
- await this.ctx.database.create(this.tableName, {
274
+ await this.ctx.database.create(this.subTableName, {
243
275
  app,
244
276
  account,
245
277
  groupId,
@@ -249,8 +281,8 @@ var SubscriptionService = class extends import_koishi.Service {
249
281
  }
250
282
  // 删除订阅
251
283
  async removeSubscription(app, account, groupId) {
252
- app = this.appMap.get(app), account = this.accountMap.get(app + account);
253
- const result = await this.ctx.database.remove(this.tableName, {
284
+ app = this.appMap.get(app), account = this.getAccount(app, account);
285
+ const result = await this.ctx.database.remove(this.subTableName, {
254
286
  app,
255
287
  account,
256
288
  groupId
@@ -259,11 +291,11 @@ var SubscriptionService = class extends import_koishi.Service {
259
291
  }
260
292
  // 获取群组的所有订阅
261
293
  async getGroupSubscriptions(groupId) {
262
- return await this.ctx.database.get(this.tableName, { groupId });
294
+ return await this.ctx.database.get(this.subTableName, { groupId });
263
295
  }
264
296
  // 获取所有订阅(用于管理)
265
297
  async getAllSubscriptions() {
266
- return await this.ctx.database.get(this.tableName, {});
298
+ return await this.ctx.database.get(this.subTableName, {});
267
299
  }
268
300
  // 按应用获取订阅统计
269
301
  async getSubscriptionStats() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-subscription",
3
3
  "description": "给多个应用提供订阅群组管理",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [