mirai.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const axios_1 = require("axios");
  13. const mirai_ts_1 = require("mirai-ts");
  14. const message_1 = require("mirai-ts/dist/message");
  15. const helper_1 = require("./helper");
  16. const loggers_1 = require("./loggers");
  17. const logger = loggers_1.getLogger('qqbot');
  18. const ChatTypeMap = {
  19. GroupMessage: "group" /* Group */,
  20. FriendMessage: "private" /* Private */,
  21. TempMessage: "temp" /* Temp */,
  22. };
  23. exports.Message = message_1.default;
  24. class default_1 {
  25. constructor(opt) {
  26. this.sendTo = (subscriber, msg, timeout = -1) => (() => {
  27. if (timeout)
  28. timeout = Math.floor(timeout);
  29. if (timeout === 0 || timeout < -1) {
  30. return Promise.reject('Error: timeout must be greater than 0ms');
  31. }
  32. try {
  33. this.bot.axios.defaults.timeout = timeout === -1 ? 0 : timeout;
  34. switch (subscriber.chatType) {
  35. case 'group':
  36. return this.bot.api.sendGroupMessage(msg, subscriber.chatID);
  37. case 'private':
  38. return this.bot.api.sendFriendMessage(msg, subscriber.chatID);
  39. }
  40. }
  41. finally {
  42. this.bot.axios.defaults.timeout = 0;
  43. }
  44. })()
  45. .then(response => {
  46. logger.info(`pushing data to ${subscriber.chatID} was successful, response:`);
  47. logger.info(response);
  48. })
  49. .catch(reason => {
  50. logger.error(`error pushing data to ${subscriber.chatID}, reason: ${reason}`);
  51. throw Error(reason);
  52. });
  53. this.initBot = () => {
  54. this.bot = new mirai_ts_1.default({
  55. authKey: this.botInfo.access_token,
  56. enableWebsocket: false,
  57. host: this.botInfo.host,
  58. port: this.botInfo.port,
  59. });
  60. this.bot.on('message', (msg) => {
  61. const chat = {
  62. chatType: ChatTypeMap[msg.type],
  63. chatID: 0,
  64. };
  65. if (msg.type === 'FriendMessage') {
  66. chat.chatID = msg.sender.id;
  67. }
  68. else if (msg.type === 'GroupMessage') {
  69. chat.chatID = msg.sender.group.id;
  70. }
  71. const cmdObj = helper_1.default(msg.plain);
  72. switch (cmdObj.cmd) {
  73. case 'twitter_sub':
  74. case 'twitter_subscribe':
  75. msg.reply(this.botInfo.sub(chat, cmdObj.args));
  76. break;
  77. case 'twitter_unsub':
  78. case 'twitter_unsubscribe':
  79. msg.reply(this.botInfo.unsub(chat, cmdObj.args));
  80. break;
  81. case 'ping':
  82. case 'twitter':
  83. msg.reply(this.botInfo.list(chat, cmdObj.args));
  84. break;
  85. case 'help':
  86. msg.reply(`推特搬运机器人:
  87. /twitter - 查询当前聊天中的订阅
  88. /twitter_subscribe [链接] - 订阅 Twitter 搬运
  89. /twitter_unsubscribe [链接] - 退订 Twitter 搬运`);
  90. }
  91. });
  92. };
  93. // TODO doesn't work if connection is dropped after connection
  94. this.listen = (logMsg) => {
  95. if (logMsg !== '') {
  96. logger.warn((logMsg !== null && logMsg !== void 0 ? logMsg : 'Listening...'));
  97. }
  98. axios_1.default.get(`http://${this.botInfo.host}:${this.botInfo.port}/about`)
  99. .then(() => __awaiter(this, void 0, void 0, function* () {
  100. if (logMsg !== '') {
  101. this.bot.listen();
  102. yield this.login();
  103. }
  104. setTimeout(() => this.listen(''), 5000);
  105. }))
  106. .catch(() => {
  107. logger.error(`Error connecting to bot provider at ${this.botInfo.host}:${this.botInfo.port}`);
  108. setTimeout(() => this.listen('Retry listening...'), 2500);
  109. });
  110. };
  111. this.login = (logMsg) => __awaiter(this, void 0, void 0, function* () {
  112. logger.warn((logMsg !== null && logMsg !== void 0 ? logMsg : 'Logging in...'));
  113. yield this.bot.login(this.botInfo.bot_id)
  114. .then(() => logger.warn(`Logged in as ${this.botInfo.bot_id}`))
  115. .catch(() => {
  116. logger.error(`Cannot log in. Do you have a bot logged in as ${this.botInfo.bot_id}?`);
  117. setTimeout(() => this.login('Retry logging in...'), 2500);
  118. });
  119. });
  120. this.connect = () => {
  121. this.initBot();
  122. this.listen();
  123. };
  124. logger.warn(`Initialized mirai-ts for ${opt.host}:${opt.port} with access_token ${opt.access_token}`);
  125. this.botInfo = opt;
  126. }
  127. }
  128. exports.default = default_1;