command.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.view = exports.unsub = exports.list = 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 = 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 linkFinder(userName, chat, lock) {
  31. const normalizedLink = twitter_1.linkBuilder({ userName });
  32. const link = Object.keys(lock.threads).find(realLink => normalizedLink === realLink.replace(/\/@/, '/').toLowerCase());
  33. if (!link)
  34. return [null, -1];
  35. const index = lock.threads[link].subscribers.findIndex(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType);
  36. return [link, index];
  37. }
  38. function sub(chat, args, reply, lock, lockfile) {
  39. if (chat.chatType === "temp") {
  40. return reply('请先添加机器人为好友。');
  41. }
  42. if (args.length === 0) {
  43. return reply('找不到要订阅 Instagram 限时动态的链接。');
  44. }
  45. const matched = twitter_1.parseLink(args[0]);
  46. if (!matched) {
  47. return reply(`订阅链接格式错误:
  48. 示例:
  49. https://www.instagram.com/tomoyo_kurosawa_/`);
  50. }
  51. const subscribeTo = (link, config = {}) => {
  52. const { id, msg = `已为此聊天订阅 ${link} 的 Instagram 限时动态` } = config;
  53. if (id) {
  54. lock.feed.push(link);
  55. lock.threads[link] = {
  56. id,
  57. offset: '0',
  58. subscribers: [],
  59. updatedAt: '',
  60. };
  61. }
  62. lock.threads[link].subscribers.push(chat);
  63. logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`);
  64. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  65. reply(msg);
  66. };
  67. const tryFindSub = (userName) => {
  68. const [realLink, index] = linkFinder(userName, chat, lock);
  69. if (index > -1) {
  70. reply('此聊天已订阅此链接的 Instagram 限时动态。');
  71. return true;
  72. }
  73. if (realLink) {
  74. subscribeTo(realLink);
  75. return true;
  76. }
  77. return false;
  78. };
  79. const newSub = (userName) => {
  80. const link = twitter_1.linkBuilder(matched);
  81. subscribeTo(link, { id: Number(userName.split(':')[1]) });
  82. };
  83. if (!tryFindSub(matched.userName)) {
  84. twitter_1.ScreenNameNormalizer.normalizeLive(matched.userName).then(userName => {
  85. if (!userName)
  86. return reply(`找不到用户 ${matched.userName.replace(/^@?(.*)$/, '@$1')}。`);
  87. else
  88. newSub(userName);
  89. });
  90. }
  91. }
  92. exports.sub = sub;
  93. function unsub(chat, args, reply, lock, lockfile) {
  94. var _a;
  95. if (chat.chatType === "temp") {
  96. return reply('请先添加机器人为好友。');
  97. }
  98. if (args.length === 0) {
  99. return reply('找不到要退订 Instagram 限时动态的链接。');
  100. }
  101. const match = (_a = twitter_1.parseLink(args[0])) === null || _a === void 0 ? void 0 : _a.userName;
  102. if (!match) {
  103. return reply('链接格式有误。');
  104. }
  105. const [link, index] = linkFinder(match, chat, lock);
  106. if (index === -1)
  107. return list(chat, args, msg => reply('您没有订阅此链接的 Instagram 限时动态。\n' + msg), lock);
  108. else {
  109. lock.threads[link].subscribers.splice(index, 1);
  110. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  111. logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
  112. return reply(`已为此聊天退订 ${link} 的 Instagram 限时动态`);
  113. }
  114. }
  115. exports.unsub = unsub;
  116. function list(chat, _, reply, lock) {
  117. if (chat.chatType === "temp") {
  118. return reply('请先添加机器人为好友。');
  119. }
  120. const links = [];
  121. Object.keys(lock.threads).forEach(key => {
  122. if (lock.threads[key].subscribers.find(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType))
  123. links.push(`${key} ${datetime_1.relativeDate(lock.threads[key].updatedAt)}`);
  124. });
  125. return reply('此聊天中订阅的 Instagram 限时动态动态链接:\n' + links.join('\n'));
  126. }
  127. exports.list = list;
  128. function view(chat, args, reply) {
  129. const promptOnError = (func) => (...args) => {
  130. try {
  131. func(...args);
  132. }
  133. catch (e) {
  134. reply('机器人尚未加载完毕,请稍后重试。');
  135. }
  136. };
  137. if (args.length === 0) {
  138. return reply('找不到要查看 Instagram 限时动态的链接。');
  139. }
  140. const match = twitter_1.parseLink(args[0]);
  141. if (!(match === null || match === void 0 ? void 0 : match.userName)) {
  142. return reply('链接格式有误。');
  143. }
  144. if (match === null || match === void 0 ? void 0 : match.storyId) {
  145. return promptOnError(twitter_1.sendStory)(match.userName, match.storyId, chat);
  146. }
  147. const conf = {};
  148. const confZH = {
  149. count: '最大查看数量',
  150. skip: '跳过数量',
  151. };
  152. for (const arg of args.slice(1)) {
  153. const optMatch = /^(count|skip)=(.*)/.exec(arg);
  154. if (!optMatch)
  155. return reply(`未定义的查看参数:${arg}。`);
  156. const optKey = optMatch[1];
  157. if (optMatch.length === 1 || !/^\d*$/.test(optMatch[2]))
  158. return reply(`${confZH[optKey]}参数应为数值。`);
  159. if (optMatch[2] === '')
  160. return reply(`${confZH[optKey]}参数值不可为空。`);
  161. conf[optKey] = Number(optMatch[2]);
  162. }
  163. promptOnError(twitter_1.sendAllStories)(match.userName, chat, conf.skip, conf.count);
  164. }
  165. exports.view = view;