fair-twitch
Version:
Fair's Twitch API and Chat bot library
113 lines (112 loc) • 4.22 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var ExpandedEventEmitter_1 = __importDefault(require("./ExpandedEventEmitter"));
var v4_1 = __importDefault(require("uuid/v4"));
var RoomTracker = /** @class */ (function (_super) {
__extends(RoomTracker, _super);
/**
* @param twitchIRC The Twitch IRC client
*/
function RoomTracker(twitchIRC) {
var _this = _super.call(this) || this;
_this.twitchIRC = twitchIRC;
_this.rooms = [];
_this.eventID = v4_1.default();
_this.twitchIRC.addListener('join#' + _this.eventID, function (channel) {
var index = _this._getRoomObjIndex(channel);
if (index === -1) {
var roomObj = {
channel: channel,
state: null
};
_this.rooms.push(roomObj);
_this.emit('join', channel);
_this.emit('change', channel);
}
});
_this.twitchIRC.addListener('part#' + _this.eventID, function (channel) {
var index = _this._getRoomObjIndex(channel);
if (index !== -1) {
_this.rooms.splice(index, 1);
_this.emit('part', channel);
_this.emit('change', channel);
}
});
_this.twitchIRC.addListener('roomstate#' + _this.eventID, function (channel, tags) {
var roomObj = _this._getRoomObj(channel);
if (roomObj !== null) {
roomObj.state = tags;
_this.emit('state', channel, tags);
_this.emit('change', channel);
}
});
return _this;
}
/**
* Removes all event listeners added from this tracker
*/
RoomTracker.prototype.dispose = function () {
this.twitchIRC.removeIDListeners(this.eventID);
};
/**
* @param channel The Twitch channel name
* @returns If in channel chat room or not
*/
RoomTracker.prototype.isInChannel = function (channel) {
return this._getRoomObjIndex(channel) !== -1;
};
/**
* Get the room state tags
* @param channel The Twitch channel name
* @returns The room state tags, or null if not in channel or not gotten the state yet
*/
RoomTracker.prototype.getChannelState = function (channel) {
var roomObj = this._getRoomObj(channel);
return roomObj !== null ? roomObj.state : null;
};
/**
* @returns An array of room objects with channel and state variables
*/
RoomTracker.prototype.getChannels = function () {
// Basically copy the rooms array
return this.rooms.map(function (e) { return e; });
};
/**
* @param channel The Twitch channel name
* @returns The internal room object, null if not found
*/
RoomTracker.prototype._getRoomObj = function (channel) {
var index = this._getRoomObjIndex(channel);
return index === -1 ? null : this.rooms[index];
};
/**
* @param channel The Twitch channel name
* @returns The internal room index, -1 if not found
*/
RoomTracker.prototype._getRoomObjIndex = function (channel) {
if (channel.startsWith('#'))
channel = channel.substring(1);
for (var i = 0; i < this.rooms.length; i++) {
if (this.rooms[i].channel === channel)
return i;
}
return -1;
};
return RoomTracker;
}(ExpandedEventEmitter_1.default));
module.exports = RoomTracker;