command.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.status = exports.unsub = exports.sub = exports.parseCmd = void 0;
  4. const fs = require("fs");
  5. const path = require("path");
  6. const datetime_1 = require("./datetime");
  7. const loggers_1 = require("./loggers");
  8. const twitter_1 = require("./twitter");
  9. const logger = (0, loggers_1.getLogger)('command');
  10. function parseCmd(message) {
  11. message = message.trim();
  12. message = message.replace('\\\\', '\\0x5c');
  13. message = message.replace('\\\"', '\\0x22');
  14. message = message.replace('\\\'', '\\0x27');
  15. const strs = message.match(/'[\s\S]*?'|(?:\S+=)?"[\s\S]*?"|\S+/mg);
  16. const cmd = (strs === null || strs === void 0 ? void 0 : strs.length) ? strs[0].length ? strs[0].substring(0, 1) === '/' ? strs[0].substring(1) : '' : '' : '';
  17. const args = (strs !== null && strs !== void 0 ? strs : []).slice(1).map(arg => {
  18. arg = arg.replace(/^(\S+=)?["']+(?!.*=)|["']+$/g, '$1');
  19. arg = arg.replace('\\0x27', '\\\'');
  20. arg = arg.replace('\\0x22', '\\\"');
  21. arg = arg.replace('\\0x5c', '\\\\');
  22. return arg;
  23. });
  24. return {
  25. cmd,
  26. args,
  27. };
  28. }
  29. exports.parseCmd = parseCmd;
  30. function sub(chat, args, reply, lock, lockfile) {
  31. if (chat.chatType === "temp") {
  32. return reply('请先添加机器人为好友。');
  33. }
  34. const index = lock.subscribers.findIndex(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType);
  35. if (index > -1)
  36. return reply('此聊天已订阅 IDOLY PRIDE BWIKI 更新提醒。');
  37. lock.subscribers.push(chat);
  38. logger.warn(`chat ${JSON.stringify(chat)} has subscribed to updates`);
  39. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  40. reply('已为此聊天订阅 IDOLY PRIDE BWIKI 更新提醒。');
  41. }
  42. exports.sub = sub;
  43. function unsub(chat, args, reply, lock, lockfile) {
  44. if (chat.chatType === "temp") {
  45. return reply('请先添加机器人为好友。');
  46. }
  47. const index = lock.subscribers.findIndex(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType);
  48. if (index === -1)
  49. return reply('此聊天未订阅 IDOLY PRIDE BWIKI 更新提醒。');
  50. lock.subscribers.splice(index, 1);
  51. logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed from updates`);
  52. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  53. reply('已为此聊天退订 IDOLY PRIDE BWIKI 更新提醒。');
  54. }
  55. exports.unsub = unsub;
  56. function status(chat, _, reply, lock) {
  57. const lastAction = lock.lastActions.sort((a1, a2) => Date.parse(a2.timestamp) - Date.parse(a1.timestamp))[0];
  58. reply(`IDOLY PRIDE 官方推特追踪情况:
  59. 上次更新时间:${(0, datetime_1.relativeDate)(lastAction.timestamp || '')}
  60. 上次更新内容:${(0, twitter_1.parseAction)(lastAction)}
  61. 上次检查时间:${(0, datetime_1.relativeDate)(lock.updatedAt || '')}`);
  62. }
  63. exports.status = status;