command.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. if (!Number.isInteger(Number(args[0])))
  28. return reply('查询格式有误。格式:/nanatsuu_view〈整数〉');
  29. twitter_1.queryByRegExp('t7s_staff', /今週の『それゆけ!ナナスタ☆通信』第(\d+)話はこちら!/, 360)
  30. .then(match => {
  31. if (!match)
  32. throw Error();
  33. let query = Number(args[0] || Number(match[1]));
  34. if (query < 0)
  35. query += Number(match[1]);
  36. if (query < 0 || query > Number(match[1])) {
  37. return reply(`查询取值范围有误。当前可用的取值范围:${-Number(match[1])}~${Number(match[1])}`);
  38. }
  39. reply(`第 ${query} 话:\n` +
  40. koishi_1.Message.Image(`https://d2n19nac4w0gh6.cloudfront.net/resource/images/webview/comic/story/comic_${String(query).padStart(3, '0')}.jpg`));
  41. }).catch((err) => reply(`查询失败,请稍后重试。${err.message ? `原因:${err}` : ''}`));
  42. }
  43. exports.view = view;