main.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. const commandLineUsage = require("command-line-usage");
  5. const log4js = require("log4js");
  6. const path = require("path");
  7. const qq_1 = require("./qq");
  8. const logger = log4js.getLogger();
  9. logger.level = 'info';
  10. const sections = [
  11. {
  12. header: 'CQHTTP Twitter Bot',
  13. content: 'The QQ Bot that forwards twitters.'
  14. },
  15. {
  16. header: 'Synopsis',
  17. content: [
  18. '$ cqhttp-twitter-bot {underline config.json}',
  19. '$ cqhttp-twitter-bot {bold --help}'
  20. ]
  21. },
  22. {
  23. header: 'Documentation',
  24. content: [
  25. 'Project home: {underline https://github.com/rikakomoe/cqhttp-twitter-bot}',
  26. 'Example config: {underline https://qwqq.pw/b96yt}'
  27. ]
  28. }
  29. ];
  30. const usage = commandLineUsage(sections);
  31. const args = process.argv.slice(2);
  32. if (args.length === 0 || args[0] === 'help' || args[0] === '-h' || args[0] === '--help') {
  33. console.log(usage);
  34. process.exit(0);
  35. }
  36. const configPath = args[0];
  37. let config;
  38. try {
  39. config = require(path.resolve(configPath));
  40. }
  41. catch (e) {
  42. console.log("Failed to parse config file: ", configPath);
  43. console.log(usage);
  44. process.exit(1);
  45. }
  46. if (config.cq_ws_host === undefined) {
  47. config.cq_ws_host = '127.0.0.1';
  48. logger.warn('cq_ws_host is undefined, use 127.0.0.1 as default');
  49. }
  50. if (config.cq_ws_port === undefined) {
  51. config.cq_ws_port = 6700;
  52. logger.warn('cq_ws_port is undefined, use 6700 as default');
  53. }
  54. if (config.cq_access_token === undefined) {
  55. config.cq_access_token = '';
  56. logger.warn('cq_access_token is undefined, use empty string as default');
  57. }
  58. function handler(chat, args, bot) {
  59. const config = {
  60. message_type: chat.chatType,
  61. user_id: chat.chatID,
  62. group_id: chat.chatID,
  63. discuss_id: chat.chatID,
  64. message: JSON.stringify(args),
  65. };
  66. bot('send_msg', config);
  67. }
  68. const qq = new qq_1.default({
  69. access_token: config.cq_access_token,
  70. host: config.cq_ws_host,
  71. port: config.cq_ws_port,
  72. list: handler,
  73. sub: handler,
  74. unsub: handler,
  75. });
  76. qq.bot.connect();