command.ts 2.5 KB

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