hackmud-chat
Version:
A hackmud chat api wrapper for nodejs.
62 lines • 1.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const Debug = require("debug");
const debug = Debug("hackmud-chat:channel");
function random(low, high) {
return Math.random() * (high - low) + low;
}
/**
* Represents a hackmud channel.
*/
class Channel {
constructor(api, name, users, ownUser) {
this.name = name;
this.users = users;
this.ownUser = ownUser;
this.api = api;
}
/**
* Send a message to the channel with ownUser
* @param msg The message
*/
async send(msg) {
return await this.api.sendChannel(this.name, this.ownUser, msg);
}
/**
* Send a message every x ms.
* @param msg The message
* @param interval Interval in ms
*/
sendInterval(msg, interval) {
return setInterval(async () => {
await this.send(msg);
}, interval);
}
/**
* Sends the messages in the array in the order given separated by the specified time.
* @param msgs The messages
* @param interval Interval in ms
*/
sendIntervalSequence(msgs, interval) {
let index = 0;
return setInterval(async () => {
await this.send(msgs[index]);
index++;
if (index >= msgs.length) {
index = 0;
}
}, interval);
}
/**
* Every interval ms send a random message from the given array.
* @param msgs The messages
* @param interval Interval in ms
*/
sendIntervalRandom(msgs, interval) {
return setInterval(async () => {
await this.send(msgs[random(0, msgs.length - 1)]);
}, interval);
}
}
exports.Channel = Channel;
//# sourceMappingURL=channel.js.map
;