main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env node
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. const fs = require("fs");
  5. const path = require("path");
  6. const commandLineUsage = require("command-line-usage");
  7. const exampleConfig = require("../config.example.json");
  8. const command_1 = require("./command");
  9. const loggers_1 = require("./loggers");
  10. const koishi_1 = require("./koishi");
  11. const twitter_1 = require("./twitter");
  12. const logger = loggers_1.getLogger();
  13. const sections = [
  14. {
  15. header: 'GoCQHTTP Twitter Bot',
  16. content: 'The QQ Bot that forwards twitters.',
  17. },
  18. {
  19. header: 'Synopsis',
  20. content: [
  21. '$ twitter-bot {underline config.json}',
  22. '$ twitter-bot {bold --help}',
  23. ],
  24. },
  25. {
  26. header: 'Documentation',
  27. content: [
  28. 'Project home: {underline https://github.com/CL-Jeremy/mirai-twitter-bot}',
  29. 'Example config: {underline https://git.io/JJ0jN}',
  30. ],
  31. },
  32. ];
  33. const usage = commandLineUsage(sections);
  34. const args = process.argv.slice(2);
  35. if (args.length === 0 || args[0] === 'help' || args[0] === '-h' || args[0] === '--help') {
  36. console.log(usage);
  37. process.exit(0);
  38. }
  39. const configPath = args[0];
  40. let config;
  41. try {
  42. config = JSON.parse(fs.readFileSync(path.resolve(configPath), 'utf8'));
  43. }
  44. catch (e) {
  45. console.log('Failed to parse config file: ', configPath);
  46. console.log(usage);
  47. process.exit(1);
  48. }
  49. const requiredFields = [
  50. 'twitter_consumer_key', 'twitter_consumer_secret', 'twitter_access_token_key', 'twitter_access_token_secret',
  51. 'cq_bot_qq',
  52. ];
  53. const warningFields = [
  54. 'cq_ws_host', 'cq_ws_port', 'cq_access_token',
  55. ];
  56. const optionalFields = [
  57. 'lockfile', 'work_interval', 'webshot_delay', 'loglevel', 'mode', 'resume_on_start',
  58. ].concat(warningFields);
  59. if (requiredFields.some((value) => config[value] === undefined)) {
  60. console.log(`${requiredFields.join(', ')} are required`);
  61. process.exit(1);
  62. }
  63. optionalFields.forEach(key => {
  64. if (config[key] === undefined || typeof (config[key]) !== typeof (exampleConfig[key])) {
  65. if (warningFields.includes(key))
  66. logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
  67. config[key] = exampleConfig[key];
  68. }
  69. });
  70. loggers_1.setLogLevels(config.loglevel);
  71. let lock;
  72. if (fs.existsSync(path.resolve(config.lockfile))) {
  73. try {
  74. lock = JSON.parse(fs.readFileSync(path.resolve(config.lockfile), 'utf8'));
  75. }
  76. catch (err) {
  77. logger.error(`Failed to parse lockfile ${config.lockfile}: `, err);
  78. lock = {
  79. workon: 0,
  80. feed: [],
  81. threads: {},
  82. };
  83. }
  84. fs.access(path.resolve(config.lockfile), fs.constants.W_OK, err => {
  85. if (err) {
  86. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  87. process.exit(1);
  88. }
  89. });
  90. }
  91. else {
  92. lock = {
  93. workon: 0,
  94. feed: [],
  95. threads: {},
  96. };
  97. try {
  98. fs.writeFileSync(path.resolve(config.lockfile), JSON.stringify(lock));
  99. }
  100. catch (err) {
  101. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  102. process.exit(1);
  103. }
  104. }
  105. if (!config.resume_on_start) {
  106. Object.keys(lock.threads).forEach(key => {
  107. lock.threads[key].offset = '-1';
  108. });
  109. }
  110. const qq = new koishi_1.default({
  111. access_token: config.cq_access_token,
  112. host: config.cq_ws_host,
  113. port: config.cq_ws_port,
  114. bot_id: config.cq_bot_qq,
  115. list: (c, a, cb) => command_1.list(c, a, cb, lock),
  116. sub: (c, a, cb) => command_1.sub(c, a, cb, lock, config.lockfile),
  117. unsub: (c, a, cb) => command_1.unsub(c, a, cb, lock, config.lockfile),
  118. });
  119. const worker = new twitter_1.default({
  120. consumerKey: config.twitter_consumer_key,
  121. consumerSecret: config.twitter_consumer_secret,
  122. accessTokenKey: config.twitter_access_token_key,
  123. accessTokenSecret: config.twitter_access_token_secret,
  124. privateAuthToken: config.twitter_private_auth_token,
  125. privateCsrfToken: config.twitter_private_csrf_token,
  126. lock,
  127. lockfile: config.lockfile,
  128. workInterval: config.work_interval,
  129. bot: qq,
  130. webshotDelay: config.webshot_delay,
  131. mode: config.mode,
  132. wsUrl: config.playwright_ws_spec_endpoint,
  133. });
  134. worker.launch();
  135. qq.connect();