command.ts 2.2 KB

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