mirai.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.MiraiMessage = message_1.default;
  24. class default_1 {
  25. constructor(opt) {
  26. this.sendTo = (subscriber, msg, timeout) => new Promise((resolve, reject) => {
  27. if (timeout === 0 || timeout < -1)
  28. reject('Error: timeout must be greater than 0 ms');
  29. (() => {
  30. switch (subscriber.chatType) {
  31. case 'group':
  32. return this.bot.api.sendGroupMessage(msg, subscriber.chatID);
  33. case 'private':
  34. return this.bot.api.sendFriendMessage(msg, subscriber.chatID);
  35. }
  36. })().then(resolve).catch(reject);
  37. setTimeout(() => reject('Error: request timed out'), timeout);
  38. })
  39. .then(response => {
  40. logger.info(`pushing data to ${subscriber.chatID} was successful, response:`);
  41. logger.info(response);
  42. })
  43. .catch(reason => {
  44. logger.error(`error pushing data to ${subscriber.chatID}, reason: ${reason}`);
  45. throw Error(reason);
  46. });
  47. this.initBot = () => {
  48. this.bot = new mirai_ts_1.default({
  49. authKey: this.botInfo.access_token,
  50. enableWebsocket: false,
  51. host: this.botInfo.host,
  52. port: this.botInfo.port,
  53. });
  54. this.bot.on('message', (msg) => {
  55. const chat = {
  56. chatType: ChatTypeMap[msg.type],
  57. chatID: 0,
  58. };
  59. if (msg.type === 'FriendMessage') {
  60. chat.chatID = msg.sender.id;
  61. }
  62. else if (msg.type === 'GroupMessage') {
  63. chat.chatID = msg.sender.group.id;
  64. }
  65. const cmdObj = helper_1.default(msg.plain);
  66. switch (cmdObj.cmd) {
  67. case 'twitter_sub':
  68. case 'twitter_subscribe':
  69. msg.reply(this.botInfo.sub(chat, cmdObj.args));
  70. break;
  71. case 'twitter_unsub':
  72. case 'twitter_unsubscribe':
  73. msg.reply(this.botInfo.unsub(chat, cmdObj.args));
  74. break;
  75. case 'ping':
  76. case 'twitter':
  77. msg.reply(this.botInfo.list(chat, cmdObj.args));
  78. break;
  79. case 'help':
  80. msg.reply(`推特搬运机器人:
  81. /twitter - 查询当前聊天中的订阅
  82. /twitter_subscribe [链接] - 订阅 Twitter 搬运
  83. /twitter_unsubscribe [链接] - 退订 Twitter 搬运`);
  84. }
  85. });
  86. };
  87. // TODO doesn't work if connection is dropped after connection
  88. this.listen = (logMsg) => {
  89. if (logMsg !== '') {
  90. logger.warn((logMsg !== null && logMsg !== void 0 ? logMsg : 'Listening...'));
  91. }
  92. axios_1.default.get(`http://${this.botInfo.host}:${this.botInfo.port}/about`)
  93. .then(() => __awaiter(this, void 0, void 0, function* () {
  94. if (logMsg !== '') {
  95. this.bot.listen();
  96. yield this.login();
  97. }
  98. setTimeout(() => this.listen(''), 5000);
  99. }))
  100. .catch(() => {
  101. logger.error(`Error connecting to bot provider at ${this.botInfo.host}:${this.botInfo.port}`);
  102. setTimeout(() => this.listen('Retry listening...'), 2500);
  103. });
  104. };
  105. this.login = (logMsg) => __awaiter(this, void 0, void 0, function* () {
  106. logger.warn((logMsg !== null && logMsg !== void 0 ? logMsg : 'Logging in...'));
  107. yield this.bot.login(this.botInfo.bot_id)
  108. .then(() => logger.warn(`Logged in as ${this.botInfo.bot_id}`))
  109. .catch(() => {
  110. logger.error(`Cannot log in. Do you have a bot logged in as ${this.botInfo.bot_id}?`);
  111. setTimeout(() => this.login('Retry logging in...'), 2500);
  112. });
  113. });
  114. this.connect = () => {
  115. this.initBot();
  116. this.listen();
  117. };
  118. logger.warn(`Initialized mirai-ts for ${opt.host}:${opt.port} with access_token ${opt.access_token}`);
  119. this.botInfo = opt;
  120. }
  121. }
  122. exports.default = default_1;