bili-sender
Version:
It is used to automatically send barrages to the Bilibili live broadcast room
58 lines (57 loc) • 1.96 kB
JavaScript
;
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;
}
}
static createTable(roomID, config) {
return roomID.map((room) => new BiliSender(room, config));
}
}
exports.default = BiliSender;