mirai.js 4.9 KB

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