mioku-plugin-admin 3.0.0 → 3.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.
@@ -1,4 +1,4 @@
1
- import type { Bot, MessageEvent, MessageInput, MessageTarget, MiokuContext, MessageSegment } from "mioku";
1
+ import type { Bot, CommandDefinition, MessageEvent, MessageInput, MessageTarget, MiokuContext, MessageSegment } from "mioku";
2
2
  import {createGroupRef} from "mioku";
3
3
  import { extractImageUrl } from "../config";
4
4
  import { replyAdminErrorNotice } from "./notice";
@@ -150,28 +150,30 @@ async function sendForwardByEvent(options: {
150
150
  }
151
151
 
152
152
  export function registerPersonalCommands(ctx: MiokuContext) {
153
- ctx.handle("message", async (event) => {
154
- const text = ctx.text(event)?.trim();
155
- if (!text) return;
156
- if (event.user_id === event.self_id) return;
153
+ const register = (command: CommandDefinition) => {
154
+ const { handler, ...rest } = command;
155
+ ctx.command({
156
+ ...rest,
157
+ prefixes: false,
158
+ handler: async (c) => {
159
+ if (c.event.user_id === c.event.self_id) return;
160
+ await handler(c);
161
+ },
162
+ });
163
+ };
157
164
 
158
- const isMaster = ctx.isOwner?.(event) ?? false;
159
-
160
- const selfId = event.self_id;
161
- const bot = event.bot;
162
- if (!bot) return;
163
-
164
- const isGroup = event.message_type === "group";
165
-
166
- if (text.startsWith("/改头像")) {
167
- if (!isMaster) {
168
- ctx.logger.warn("[admin] 改头像功能仅主人可用");
169
- return;
170
- }
165
+ register({
166
+ name: "/改头像",
167
+ match: /^\/改头像/,
168
+ permission: "master",
169
+ description: "修改Bot头像",
170
+ handler: async ({ event }) => {
171
+ const bot = event.bot;
172
+ if (!bot) return;
171
173
  const imageUrl = extractImageUrl(event.message);
172
174
  if (!imageUrl) {
173
175
  await event.reply("图片呢图片呢~", true);
174
- return;
176
+ return;
175
177
  }
176
178
  try {
177
179
  await bot.setAvatar(imageUrl);
@@ -184,20 +186,22 @@ export function registerPersonalCommands(ctx: MiokuContext) {
184
186
  fallbackMessage: `出错了,笨蛋~ ${String(err)}`,
185
187
  error: err,
186
188
  });
187
- return;
188
189
  }
189
- return;
190
- }
190
+ },
191
+ });
191
192
 
192
- if (text.startsWith("/改昵称")) {
193
- if (!isMaster) {
194
- ctx.logger.warn("[admin] 改昵称功能仅主人可用");
195
- return;
196
- }
197
- const nickname = text.replace(/^\/改昵称\s*/, "").trim();
193
+ register({
194
+ name: "/改昵称",
195
+ match: /^\/改昵称/,
196
+ permission: "master",
197
+ description: "修改Bot昵称",
198
+ handler: async ({ event, body }) => {
199
+ const bot = event.bot;
200
+ if (!bot) return;
201
+ const nickname = body.replace(/^\/改昵称\s*/, "").trim();
198
202
  if (!nickname) {
199
203
  await event.reply("想改成什么昵称呀~", true);
200
- return;
204
+ return;
201
205
  }
202
206
  try {
203
207
  await bot.setProfile({ nickname });
@@ -211,18 +215,21 @@ export function registerPersonalCommands(ctx: MiokuContext) {
211
215
  error: err,
212
216
  });
213
217
  }
214
- return;
215
- }
218
+ },
219
+ });
216
220
 
217
- if (text.startsWith("/改签名")) {
218
- if (!isMaster) {
219
- ctx.logger.warn("[admin] 改签名功能仅主人可用");
220
- return;
221
- }
222
- const personalNote = text.replace(/^\/改签名\s*/, "").trim();
221
+ register({
222
+ name: "/改签名",
223
+ match: /^\/改签名/,
224
+ permission: "master",
225
+ description: "修改Bot个性签名",
226
+ handler: async ({ event, body }) => {
227
+ const bot = event.bot;
228
+ if (!bot) return;
229
+ const personalNote = body.replace(/^\/改签名\s*/, "").trim();
223
230
  if (!personalNote) {
224
231
  await event.reply("想改成什么签名呀~", true);
225
- return;
232
+ return;
226
233
  }
227
234
  try {
228
235
  await bot.setProfile({ personal_note: personalNote });
@@ -236,16 +243,18 @@ export function registerPersonalCommands(ctx: MiokuContext) {
236
243
  error: err,
237
244
  });
238
245
  }
239
- return;
240
- }
246
+ },
247
+ });
241
248
 
242
- if (text.startsWith("/改性别")) {
243
- if (!isMaster) {
244
- ctx.logger.warn("[admin] 改性别功能仅主人可用");
245
- return;
246
- }
247
- const genderText = text.replace(/^\/改性别\s*/, "").trim();
248
- const sex = parseProfileSex(genderText);
249
+ register({
250
+ name: "/改性别",
251
+ match: /^\/改性别/,
252
+ permission: "master",
253
+ description: "修改Bot性别",
254
+ handler: async ({ event, body }) => {
255
+ const bot = event.bot;
256
+ if (!bot) return;
257
+ const sex = parseProfileSex(body.replace(/^\/改性别\s*/, ""));
249
258
  try {
250
259
  await bot.setProfile({ sex });
251
260
  await event.reply("done");
@@ -258,15 +267,19 @@ export function registerPersonalCommands(ctx: MiokuContext) {
258
267
  error: err,
259
268
  });
260
269
  }
261
- return;
262
- }
270
+ },
271
+ });
263
272
 
264
- if (text.startsWith("/删好友")) {
265
- if (!isMaster) {
266
- ctx.logger.warn("[admin] 删好友功能仅主人可用");
267
- return;
268
- }
269
- const qq = parseInt(text.replace(/^\/删好友\s*/, "").trim(), 10);
273
+ register({
274
+ name: "/删好友",
275
+ match: /^\/删好友(?:\s|$)/,
276
+ permission: "master",
277
+ description: "删除好友",
278
+ usage: "/删好友 qq",
279
+ handler: async ({ event, body }) => {
280
+ const bot = event.bot;
281
+ if (!bot) return;
282
+ const qq = parseInt(body.replace(/^\/删好友\s*/, "").trim(), 10);
270
283
  if (!qq) {
271
284
  await replyAdminErrorNotice({
272
285
  ctx,
@@ -288,15 +301,19 @@ export function registerPersonalCommands(ctx: MiokuContext) {
288
301
  error: err,
289
302
  });
290
303
  }
291
- return;
292
- }
304
+ },
305
+ });
293
306
 
294
- if (text.startsWith("/退群")) {
295
- if (!isMaster) {
296
- ctx.logger.warn("[admin] 退群功能仅主人可用");
297
- return;
298
- }
299
- const targetGroup = parseInt(text.replace(/^\/退群\s*/, "").trim(), 10);
307
+ register({
308
+ name: "/退群",
309
+ match: /^\/退群(?:\s|$)/,
310
+ permission: "master",
311
+ description: "退出群聊",
312
+ usage: "/退群 群号",
313
+ handler: async ({ event, body }) => {
314
+ const bot = event.bot;
315
+ if (!bot) return;
316
+ const targetGroup = parseInt(body.replace(/^\/退群\s*/, "").trim(), 10);
300
317
  if (!targetGroup) {
301
318
  await replyAdminErrorNotice({
302
319
  ctx,
@@ -318,15 +335,19 @@ export function registerPersonalCommands(ctx: MiokuContext) {
318
335
  error: err,
319
336
  });
320
337
  }
321
- return;
322
- }
338
+ },
339
+ });
323
340
 
324
- if (text.startsWith("/发好友")) {
325
- if (!isMaster) {
326
- ctx.logger.warn("[admin] 发好友功能仅主人可用");
327
- return;
328
- }
329
- const matched = text.match(/^\/发好友\s*(\d+)\s*([\s\S]*)$/);
341
+ register({
342
+ name: "/发好友",
343
+ match: /^\/发好友(?:\s|$)/,
344
+ permission: "master",
345
+ description: "给好友发送私聊消息",
346
+ usage: "/发好友 qq号 内容",
347
+ handler: async ({ event, body }) => {
348
+ const bot = event.bot;
349
+ if (!bot) return;
350
+ const matched = body.match(/^\/发好友\s*(\d+)\s*([\s\S]*)$/);
330
351
  if (!matched) {
331
352
  await replyAdminErrorNotice({
332
353
  ctx,
@@ -367,15 +388,19 @@ export function registerPersonalCommands(ctx: MiokuContext) {
367
388
  error: err,
368
389
  });
369
390
  }
370
- return;
371
- }
391
+ },
392
+ });
372
393
 
373
- if (text.startsWith("/发群聊")) {
374
- if (!isMaster) {
375
- ctx.logger.warn("[admin] 发群聊功能仅主人可用");
376
- return;
377
- }
378
- const matched = text.match(/^\/发群聊\s*(\d+)\s*([\s\S]*)$/);
394
+ register({
395
+ name: "/发群聊",
396
+ match: /^\/发群聊(?:\s|$)/,
397
+ permission: "master",
398
+ description: "给群聊发送消息",
399
+ usage: "/发群聊 群号 内容",
400
+ handler: async ({ event, body }) => {
401
+ const bot = event.bot;
402
+ if (!bot) return;
403
+ const matched = body.match(/^\/发群聊\s*(\d+)\s*([\s\S]*)$/);
379
404
  if (!matched) {
380
405
  await replyAdminErrorNotice({
381
406
  ctx,
@@ -416,15 +441,18 @@ export function registerPersonalCommands(ctx: MiokuContext) {
416
441
  error: err,
417
442
  });
418
443
  }
419
- return;
420
- }
444
+ },
445
+ });
421
446
 
422
- if (text === "/全部好友") {
423
- if (!isMaster) {
424
- ctx.logger.warn("[admin] 全部好友功能仅主人可用");
425
- return;
426
- }
427
- if (isGroup) {
447
+ register({
448
+ name: "/全部好友",
449
+ match: /^\/全部好友$/,
450
+ permission: "master",
451
+ description: "获取全部好友列表",
452
+ handler: async ({ event }) => {
453
+ const bot = event.bot;
454
+ if (!bot) return;
455
+ if (event.message_type === "group") {
428
456
  await event.reply("在私聊使用试试看吧~", true);
429
457
  return;
430
458
  }
@@ -464,15 +492,18 @@ export function registerPersonalCommands(ctx: MiokuContext) {
464
492
  error: err,
465
493
  });
466
494
  }
467
- return;
468
- }
495
+ },
496
+ });
469
497
 
470
- if (text === "/全部群聊") {
471
- if (!isMaster) {
472
- ctx.logger.warn("[admin] 全部群聊功能仅主人可用");
473
- return;
474
- }
475
- if (isGroup) {
498
+ register({
499
+ name: "/全部群聊",
500
+ match: /^\/全部群聊$/,
501
+ permission: "master",
502
+ description: "获取全部群聊列表",
503
+ handler: async ({ event }) => {
504
+ const bot = event.bot;
505
+ if (!bot) return;
506
+ if (event.message_type === "group") {
476
507
  await event.reply("在私聊使用试试看吧~", true);
477
508
  return;
478
509
  }
@@ -489,8 +520,8 @@ export function registerPersonalCommands(ctx: MiokuContext) {
489
520
  }
490
521
  const nodes = groupList.map((group) =>
491
522
  ctx.segment.raw("node", {
492
- user_id: selfId,
493
- nickname: String(selfId),
523
+ user_id: event.self_id ?? "",
524
+ nickname: String(event.self_id ?? ""),
494
525
  content: [
495
526
  ctx.segment.image(
496
527
  `https://p.qlogo.cn/gh/${group.group_id}/${group.group_id}/640/`,
@@ -511,7 +542,6 @@ export function registerPersonalCommands(ctx: MiokuContext) {
511
542
  error: err,
512
543
  });
513
544
  }
514
- return;
515
- }
545
+ },
516
546
  });
517
547
  }