command.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.view = exports.parseCmd = void 0;
  4. const koishi_1 = require("./koishi");
  5. const twitter_1 = require("./twitter");
  6. function parseCmd(message) {
  7. message = message.trim();
  8. message = message.replace('\\\\', '\\0x5c');
  9. message = message.replace('\\\"', '\\0x22');
  10. message = message.replace('\\\'', '\\0x27');
  11. const strs = message.match(/'[\s\S]*?'|(?:\S+=)?"[\s\S]*?"|\S+/mg);
  12. const cmd = (strs === null || strs === void 0 ? void 0 : strs.length) ? strs[0].length ? strs[0].substring(0, 1) === '/' ? strs[0].substring(1) : '' : '' : '';
  13. const args = (strs !== null && strs !== void 0 ? strs : []).slice(1).map(arg => {
  14. arg = arg.replace(/^(\S+=)?["']+(?!.*=)|["']+$/g, '$1');
  15. arg = arg.replace('\\0x27', '\\\'');
  16. arg = arg.replace('\\0x22', '\\\"');
  17. arg = arg.replace('\\0x5c', '\\\\');
  18. return arg;
  19. });
  20. return {
  21. cmd,
  22. args,
  23. };
  24. }
  25. exports.parseCmd = parseCmd;
  26. function view(_chat, args, reply) {
  27. let query = Number(args[0]);
  28. if (args[0] && !Number.isInteger(query))
  29. return reply('查询格式有误。\n格式:/nanatsuu_view [〈整数〉]');
  30. twitter_1.queryByRegExp('t7s_staff', /の『それゆけ!ナナスタ☆通信』第(\d+)話はこちら!/, 3600 * 24 * 7)
  31. .then(match => {
  32. if (!match)
  33. throw Error();
  34. if (!args[0])
  35. query = Number(match[1]);
  36. if (query < 0)
  37. query += Number(match[1]);
  38. if (query < 0 || query > Number(match[1])) {
  39. return reply(`查询取值范围有误。当前可用的取值范围:${-Number(match[1])}~${Number(match[1])}`);
  40. }
  41. reply(`第 ${query} 话:\n` +
  42. koishi_1.Message.Image(`https://d2n19nac4w0gh6.cloudfront.net/resource/images/webview/comic/story/comic_${String(query).padStart(3, '0')}.jpg`));
  43. }).catch((err) => reply(`查询失败,请稍后重试。${err.message ? `原因:${err}` : ''}`));
  44. }
  45. exports.view = view;