main.js 2.1 KB

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