command.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fs = require("fs");
  4. const log4js = require("log4js");
  5. const path = require("path");
  6. const datetime_1 = require("./datetime");
  7. const logger = log4js.getLogger('command');
  8. logger.level = global.loglevel;
  9. function sub(chat, args, lock, lockfile) {
  10. if (args.length === 0) {
  11. return '找不到要订阅的链接。';
  12. }
  13. const link = args[0];
  14. let flag = false;
  15. let match = link.match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
  16. if (match)
  17. flag = true;
  18. else {
  19. match = link.match(/https:\/\/twitter.com\/([^\/]+)/);
  20. if (match)
  21. flag = true;
  22. }
  23. if (!flag) {
  24. return `订阅链接格式错误:
  25. 示例:
  26. https://twitter.com/Saito_Shuka
  27. https://twitter.com/rikakomoe/lists/lovelive`;
  28. }
  29. flag = false;
  30. lock.feed.forEach(fl => {
  31. if (fl === link)
  32. flag = true;
  33. });
  34. if (!flag)
  35. lock.feed.push(link);
  36. if (!lock.threads[link]) {
  37. lock.threads[link] = {
  38. offset: 0,
  39. subscribers: [],
  40. updatedAt: '',
  41. };
  42. }
  43. flag = false;
  44. lock.threads[link].subscribers.forEach(c => {
  45. if (c.chatID === chat.chatID && c.chatType === chat.chatType)
  46. flag = true;
  47. });
  48. if (!flag)
  49. lock.threads[link].subscribers.push(chat);
  50. logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`);
  51. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  52. return `已为此聊天订阅 ${link}`;
  53. }
  54. exports.sub = sub;
  55. function unsub(chat, args, lock, lockfile) {
  56. if (args.length === 0) {
  57. return '找不到要退订的链接。';
  58. }
  59. const link = args[0];
  60. if (!lock.threads[link]) {
  61. return '您没有订阅此链接。\n' + list(chat, args, lock);
  62. }
  63. let flag = false;
  64. lock.threads[link].subscribers.forEach((c, index) => {
  65. if (c.chatID === chat.chatID && c.chatType === chat.chatType) {
  66. flag = true;
  67. lock.threads[link].subscribers.splice(index, 1);
  68. }
  69. });
  70. if (flag) {
  71. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  72. logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
  73. return `已为此聊天退订 ${link}`;
  74. }
  75. return '您没有订阅此链接。\n' + list(chat, args, lock);
  76. }
  77. exports.unsub = unsub;
  78. function list(chat, args, lock) {
  79. const links = [];
  80. Object.keys(lock.threads).forEach(key => {
  81. lock.threads[key].subscribers.forEach(c => {
  82. if (c.chatID === chat.chatID && c.chatType === chat.chatType)
  83. links.push(`${key} ${datetime_1.relativeDate(lock.threads[key].updatedAt)}`);
  84. });
  85. });
  86. return '此聊天中订阅的链接:\n' + links.join('\n');
  87. }
  88. exports.list = list;