command.js 2.4 KB

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