main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env node
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. const commandLineUsage = require("command-line-usage");
  5. const fs = require("fs");
  6. const path = require("path");
  7. const command_1 = require("./command");
  8. const loggers_1 = require("./loggers");
  9. const mirai_1 = require("./mirai");
  10. const twitter_1 = require("./twitter");
  11. const logger = loggers_1.getLogger();
  12. const sections = [
  13. {
  14. header: 'MiraiTS Twitter Bot',
  15. content: 'The QQ Bot that forwards twitters.',
  16. },
  17. {
  18. header: 'Synopsis',
  19. content: [
  20. '$ mirai-twitter-bot {underline config.json}',
  21. '$ mirai-twitter-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 = require(path.resolve(configPath));
  42. }
  43. catch (e) {
  44. console.log('Failed to parse config file: ', configPath);
  45. console.log(usage);
  46. process.exit(1);
  47. }
  48. if (config.twitter_consumer_key === undefined ||
  49. config.twitter_consumer_secret === undefined ||
  50. config.twitter_access_token_key === undefined ||
  51. config.twitter_access_token_secret === undefined) {
  52. console.log('twitter_consumer_key twitter_consumer_secret twitter_access_token_key twitter_access_token_secret are required');
  53. process.exit(1);
  54. }
  55. if (config.mirai_http_host === undefined) {
  56. config.mirai_http_host = '127.0.0.1';
  57. logger.warn('mirai_http_host is undefined, use 127.0.0.1 as default');
  58. }
  59. if (config.mirai_http_port === undefined) {
  60. config.mirai_http_port = 8080;
  61. logger.warn('mirai_http_port is undefined, use 8080 as default');
  62. }
  63. if (config.mirai_access_token === undefined) {
  64. config.mirai_access_token = '';
  65. logger.warn('mirai_access_token is undefined, use empty string as default');
  66. }
  67. if (config.lockfile === undefined) {
  68. config.lockfile = 'subscriber.lock';
  69. }
  70. if (config.work_interval === undefined) {
  71. config.work_interval = 60;
  72. }
  73. if (config.webshot_delay === undefined) {
  74. config.webshot_delay = 10000;
  75. }
  76. if (config.loglevel === undefined) {
  77. config.loglevel = 'info';
  78. }
  79. if (typeof config.mode !== 'number') {
  80. config.mode = 0;
  81. }
  82. if (typeof config.resume_on_start !== 'boolean') {
  83. config.resume_on_start = false;
  84. }
  85. loggers_1.setLogLevels(config.loglevel);
  86. let lock;
  87. if (fs.existsSync(path.resolve(config.lockfile))) {
  88. try {
  89. lock = JSON.parse(fs.readFileSync(path.resolve(config.lockfile), 'utf8'));
  90. }
  91. catch (err) {
  92. logger.error(`Failed to parse lockfile ${config.lockfile}: `, err);
  93. lock = {
  94. workon: 0,
  95. feed: [],
  96. threads: {},
  97. };
  98. }
  99. fs.access(path.resolve(config.lockfile), fs.constants.W_OK, err => {
  100. if (err) {
  101. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  102. process.exit(1);
  103. }
  104. });
  105. }
  106. else {
  107. lock = {
  108. workon: 0,
  109. feed: [],
  110. threads: {},
  111. };
  112. try {
  113. fs.writeFileSync(path.resolve(config.lockfile), JSON.stringify(lock));
  114. }
  115. catch (err) {
  116. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  117. process.exit(1);
  118. }
  119. }
  120. if (!config.resume_on_start) {
  121. Object.keys(lock.threads).forEach(key => {
  122. lock.threads[key].offset = '-1';
  123. });
  124. }
  125. const qq = new mirai_1.default({
  126. access_token: config.mirai_access_token,
  127. host: config.mirai_http_host,
  128. port: config.mirai_http_port,
  129. bot_id: config.mirai_bot_qq,
  130. list: (c, a, cb) => command_1.list(c, a, cb, lock),
  131. sub: (c, a, cb) => command_1.sub(c, a, cb, lock, config.lockfile),
  132. unsub: (c, a, cb) => command_1.unsub(c, a, cb, lock, config.lockfile),
  133. });
  134. const worker = new twitter_1.default({
  135. consumer_key: config.twitter_consumer_key,
  136. consumer_secret: config.twitter_consumer_secret,
  137. access_token_key: config.twitter_access_token_key,
  138. access_token_secret: config.twitter_access_token_secret,
  139. lock,
  140. lockfile: config.lockfile,
  141. workInterval: config.work_interval,
  142. bot: qq,
  143. webshotDelay: config.webshot_delay,
  144. mode: config.mode,
  145. });
  146. worker.launch();
  147. qq.connect();