main.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 loggers_1 = require("./loggers");
  9. const koishi_1 = require("./koishi");
  10. const twitter_1 = require("./twitter");
  11. const logger = (0, loggers_1.getLogger)();
  12. const sections = [
  13. {
  14. header: 'GoCQHTTP Twitter Search Bot',
  15. content: 'The QQ Bot that search through tweets.',
  16. },
  17. {
  18. header: 'Synopsis',
  19. content: [
  20. '$ twitter-searchbot {underline config.json}',
  21. '$ twitter-searchbot {bold --help}',
  22. ],
  23. },
  24. {
  25. header: 'Documentation',
  26. content: [
  27. 'Project home: {underline https://github.com/CL-Jeremy/mirai-twitter-bot}',
  28. 'Example config: {underline https://git.io/JJ0jN}',
  29. ],
  30. },
  31. ];
  32. const usage = commandLineUsage(sections);
  33. const args = process.argv.slice(2);
  34. if (args.length === 0 || args[0] === 'help' || args[0] === '-h' || args[0] === '--help') {
  35. console.log(usage);
  36. process.exit(0);
  37. }
  38. const configPath = args[0];
  39. let config;
  40. try {
  41. config = JSON.parse(fs.readFileSync(path.resolve(configPath), 'utf8'));
  42. }
  43. catch (e) {
  44. console.log('Failed to parse config file: ', configPath);
  45. console.log(usage);
  46. process.exit(1);
  47. }
  48. const requiredFields = [
  49. 'twitter_private_auth_token', 'twitter_private_csrf_token'
  50. ];
  51. const warningFields = [
  52. 'cq_ws_host', 'cq_ws_port', 'cq_access_token',
  53. ];
  54. const optionalFields = [
  55. 'loglevel'
  56. ].concat(warningFields);
  57. if (requiredFields.some((value) => config[value] === undefined)) {
  58. console.log(`${requiredFields.join(', ')} are required`);
  59. process.exit(1);
  60. }
  61. optionalFields.forEach(key => {
  62. if (config[key] === undefined || typeof (config[key]) !== typeof (exampleConfig[key])) {
  63. if (warningFields.includes(key))
  64. logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
  65. config[key] = exampleConfig[key];
  66. }
  67. });
  68. (0, loggers_1.setLogLevels)(config.loglevel);
  69. const qq = new koishi_1.default({
  70. access_token: config.cq_access_token,
  71. host: config.cq_ws_host,
  72. port: config.cq_ws_port,
  73. bot_id: config.cq_bot_qq,
  74. });
  75. new twitter_1.default({
  76. privateAuthToken: config.twitter_private_auth_token,
  77. privateCsrfToken: config.twitter_private_csrf_token,
  78. bot: qq,
  79. });
  80. qq.connect();