command.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.picSearch = exports.search = exports.parseCmd = void 0;
  4. const loggers_1 = require("./loggers");
  5. const twitter_1 = require("./twitter");
  6. const logger = (0, loggers_1.getLogger)('command');
  7. function parseCmd(message) {
  8. message = message.trim();
  9. message = message.replace('\\\\', '\\0x5c');
  10. message = message.replace('\\\"', '\\0x22');
  11. message = message.replace('\\\'', '\\0x27');
  12. const strs = message.match(/'[\s\S]*?'|(?:\S+=)?"[\s\S]*?"|\S+/mg);
  13. const cmd = (strs === null || strs === void 0 ? void 0 : strs.length) ? strs[0].length ? strs[0].substring(0, 1) === '/' ? strs[0].substring(1) : '' : '' : '';
  14. const args = (strs !== null && strs !== void 0 ? strs : []).slice(1).map(arg => {
  15. arg = arg.replace(/^(\S+=)?["']+(?!.*=)|["']+$/g, '$1');
  16. arg = arg.replace('\\0x27', '\\\'');
  17. arg = arg.replace('\\0x22', '\\\"');
  18. arg = arg.replace('\\0x5c', '\\\\');
  19. return arg;
  20. });
  21. return {
  22. cmd,
  23. args,
  24. };
  25. }
  26. exports.parseCmd = parseCmd;
  27. function search(chat, args, reply) {
  28. if (args.length === 0 || !args[0]) {
  29. return reply('未输入自定义查询表达式。');
  30. }
  31. const conf = { query: args[0] };
  32. const confZH = {
  33. count: '数量上限',
  34. since: '起始点',
  35. until: '结束点',
  36. };
  37. for (const arg of args.slice(1)) {
  38. const optMatch = /^(count|since|until)=(.*)/.exec(arg);
  39. if (!optMatch)
  40. return reply(`未定义的查询参数:${arg}。`);
  41. const optKey = optMatch[1];
  42. if (optMatch.length === 1)
  43. return reply(`查询${confZH[optKey]}参数格式有误。`);
  44. conf[optKey] = optMatch[2];
  45. if (optMatch[2] === '')
  46. return reply(`查询${confZH[optKey]}参数值不可为空。`);
  47. }
  48. if (conf.count !== undefined && !Number(conf.count) || Number(conf.count) > 100) {
  49. return reply('查询数量上限参数非正、非数值或超出取值范围。');
  50. }
  51. try {
  52. (0, twitter_1.doSearch)(conf, chat);
  53. }
  54. catch (e) {
  55. logger.error(`error querying timeline, error: ${e}`);
  56. reply('推特机器人尚未加载完毕,请稍后重试。');
  57. }
  58. }
  59. exports.search = search;
  60. function picSearch(chat, args, reply) {
  61. search(chat, [args[0] && args[0] + '(filter:media)', ...args.slice(1)], reply);
  62. }
  63. exports.picSearch = picSearch;