command.js 2.7 KB

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