UNPKG

bili-sender

Version:

It is used to automatically send barrages to the Bilibili live broadcast room

55 lines (54 loc) 1.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const node_console_1 = require("node:console"); const node_events_1 = require("node:events"); const axios_1 = require("axios"); const EncodeDataBody_1 = tslib_1.__importDefault(require("./helper/EncodeDataBody")); const config_1 = tslib_1.__importDefault(require("./unit/config")); class BiliSender extends node_events_1.EventEmitter { static CLIENT = (0, axios_1.create)({ baseURL: 'https://api.live.bilibili.com', method: 'POST' }); roomID; headers; csrf; constructor(roomID, config) { super(); this.roomID = roomID; this.headers = config_1.default.generateHeaders(config.Cookie); this.csrf = config_1.default.parseCookie(this.headers.get('Cookie'), 'bili_jct'); } addListener(callback) { return super.addListener('callback', callback); } async send(message) { try { const { data: { code, message: responseMessage } } = await BiliSender.CLIENT({ url: '/msg/send', headers: this.headers, data: (0, EncodeDataBody_1.default)({ wkfb: this.headers .get('Content-Type') ?.split('boundary=') .at(1), roomId: this.roomID, message, csrf: this.csrf, csrf_token: this.csrf }) }); super.emit('callback', { code, message: responseMessage }); return true; } catch (err) { (0, node_console_1.error)(new Error(err)); return false; } } } exports.default = BiliSender;