main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 command_1 = require("./command");
  12. const logger = (0, loggers_1.getLogger)();
  13. const sections = [
  14. {
  15. header: 'GoCQHTTP IPWIKI Bot',
  16. content: 'The QQ Bot that updates Idoly Pride BWIKI.',
  17. },
  18. {
  19. header: 'Synopsis',
  20. content: [
  21. '$ cq-ipwiki-bot {underline config.json}',
  22. '$ cq-ipwiki-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', 'bilibili_cookie_sessdata'
  52. ];
  53. const warningFields = [
  54. 'cq_ws_host', 'cq_ws_port', 'cq_access_token',
  55. ];
  56. const optionalFields = [
  57. 'lockfile', 'work_interval', 'loglevel',
  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. (0, 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. offset: '0',
  80. lastActions: [],
  81. subscribers: [],
  82. updatedAt: undefined,
  83. };
  84. }
  85. fs.access(path.resolve(config.lockfile), fs.constants.W_OK, err => {
  86. if (err) {
  87. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  88. process.exit(1);
  89. }
  90. });
  91. }
  92. else {
  93. lock = {
  94. offset: '0',
  95. lastActions: [],
  96. subscribers: [],
  97. updatedAt: undefined,
  98. };
  99. try {
  100. fs.writeFileSync(path.resolve(config.lockfile), JSON.stringify(lock));
  101. }
  102. catch (err) {
  103. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  104. process.exit(1);
  105. }
  106. }
  107. const qq = new koishi_1.default({
  108. access_token: config.cq_access_token,
  109. host: config.cq_ws_host,
  110. port: config.cq_ws_port,
  111. bot_id: config.cq_bot_qq,
  112. status: (c, a, cb) => (0, command_1.status)(c, a, cb, lock),
  113. sub: (c, a, cb) => (0, command_1.sub)(c, a, cb, lock, config.lockfile),
  114. unsub: (c, a, cb) => (0, command_1.unsub)(c, a, cb, lock, config.lockfile),
  115. });
  116. const worker = new twitter_1.default({
  117. consumerKey: config.twitter_consumer_key,
  118. consumerSecret: config.twitter_consumer_secret,
  119. accessTokenKey: config.twitter_access_token_key,
  120. accessTokenSecret: config.twitter_access_token_secret,
  121. wikiSessionCookie: config.bilibili_cookie_sessdata,
  122. lock,
  123. lockfile: config.lockfile,
  124. workInterval: config.work_interval,
  125. bot: qq,
  126. });
  127. worker.launch();
  128. qq.connect();