mirai.js 5.1 KB

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