main.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = loggers_1.getLogger();
  12. const sections = [
  13. {
  14. header: 'GoCQHTTP Nana Bot',
  15. content: 'The QQ Bot that does stuff.',
  16. },
  17. {
  18. header: 'Synopsis',
  19. content: [
  20. '$ cq-nana-bot {underline config.json}',
  21. '$ cq-nana-bot {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_consumer_key', 'twitter_consumer_secret', 'twitter_access_token_key', 'twitter_access_token_secret',
  50. 'cq_bot_qq',
  51. ];
  52. const warningFields = [
  53. 'cq_ws_host', 'cq_ws_port', 'cq_access_token',
  54. ];
  55. const optionalFields = [
  56. 'loglevel',
  57. ].concat(warningFields);
  58. if (requiredFields.some((value) => config[value] === undefined)) {
  59. console.log(`${requiredFields.join(', ')} are required`);
  60. process.exit(1);
  61. }
  62. optionalFields.forEach(key => {
  63. if (config[key] === undefined || typeof (config[key]) !== typeof (exampleConfig[key])) {
  64. if (warningFields.includes(key))
  65. logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
  66. config[key] = exampleConfig[key];
  67. }
  68. });
  69. loggers_1.setLogLevels(config.loglevel);
  70. const qq = new koishi_1.default({
  71. access_token: config.cq_access_token,
  72. host: config.cq_ws_host,
  73. port: config.cq_ws_port,
  74. bot_id: config.cq_bot_qq,
  75. });
  76. new twitter_1.default({
  77. consumerKey: config.twitter_consumer_key,
  78. consumerSecret: config.twitter_consumer_secret,
  79. accessTokenKey: config.twitter_access_token_key,
  80. accessTokenSecret: config.twitter_access_token_secret,
  81. });
  82. qq.connect();