12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #!/usr/bin/env node
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const fs = require("fs");
- const path = require("path");
- const commandLineUsage = require("command-line-usage");
- const exampleConfig = require("../config.example.json");
- const loggers_1 = require("./loggers");
- const koishi_1 = require("./koishi");
- const twitter_1 = require("./twitter");
- const logger = loggers_1.getLogger();
- const sections = [
- {
- header: 'GoCQHTTP Nana Bot',
- content: 'The QQ Bot that does stuff.',
- },
- {
- header: 'Synopsis',
- content: [
- '$ cq-nana-bot {underline config.json}',
- '$ cq-nana-bot {bold --help}',
- ],
- },
- {
- header: 'Documentation',
- content: [
- 'Project home: {underline https://github.com/CL-Jeremy/mirai-twitter-bot}',
- 'Example config: {underline https://git.io/JJ0jN}',
- ],
- },
- ];
- const usage = commandLineUsage(sections);
- const args = process.argv.slice(2);
- if (args.length === 0 || args[0] === 'help' || args[0] === '-h' || args[0] === '--help') {
- console.log(usage);
- process.exit(0);
- }
- const configPath = args[0];
- let config;
- try {
- config = JSON.parse(fs.readFileSync(path.resolve(configPath), 'utf8'));
- }
- catch (e) {
- console.log('Failed to parse config file: ', configPath);
- console.log(usage);
- process.exit(1);
- }
- const requiredFields = [
- 'twitter_consumer_key', 'twitter_consumer_secret', 'twitter_access_token_key', 'twitter_access_token_secret',
- 'cq_bot_qq',
- ];
- const warningFields = [
- 'cq_ws_host', 'cq_ws_port', 'cq_access_token',
- ];
- const optionalFields = [
- 'loglevel',
- ].concat(warningFields);
- if (requiredFields.some((value) => config[value] === undefined)) {
- console.log(`${requiredFields.join(', ')} are required`);
- process.exit(1);
- }
- optionalFields.forEach(key => {
- if (config[key] === undefined || typeof (config[key]) !== typeof (exampleConfig[key])) {
- if (warningFields.includes(key))
- logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
- config[key] = exampleConfig[key];
- }
- });
- loggers_1.setLogLevels(config.loglevel);
- const qq = new koishi_1.default({
- access_token: config.cq_access_token,
- host: config.cq_ws_host,
- port: config.cq_ws_port,
- bot_id: config.cq_bot_qq,
- });
- new twitter_1.default({
- consumerKey: config.twitter_consumer_key,
- consumerSecret: config.twitter_consumer_secret,
- accessTokenKey: config.twitter_access_token_key,
- accessTokenSecret: config.twitter_access_token_secret,
- });
- qq.connect();
|