command.ts 2.9 KB

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