UNPKG

hackmud-chat

Version:
60 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Debug = require("debug"); const channel_message_1 = require("./channel.message"); const private_message_1 = require("./private.message"); const debug = Debug("hackmud-chat:account"); /** * Represents a hackmud account with it's users. */ class Account { constructor(api, users) { this.api = api; this.users = users; } /** * Returns the account user from the given name. * @param name The user name */ getUserByName(name) { return this.users.filter((x) => x.name === name)[0]; } /** * Get the new messages every interval specified by `frequency`. * @param cb * @param frequency */ poll(cb, frequency = 1200) { // It looks like its ignoring the interval let time = (Date.now() / 1000); return setInterval(async () => { const messages = await this.api.getChats(this.users.map((x) => x.name), time); let msgArr = []; for (const user in messages.chats) { if (messages.chats[user]) { for (const msg in messages.chats[user]) { if (messages.chats[user][msg] && messages.chats[user][msg].channel) { const msgInfo = messages.chats[user][msg]; const channel = this.users.filter((x) => x.name === user)[0] .channels.filter((x) => x.name === messages.chats[user][msg].channel)[0]; msgArr.push(new channel_message_1.ChannelMessage(this.users.filter((x) => x.name === user)[0], msgInfo.from_user, msgInfo.msg, msgInfo.t, msgInfo.id, channel, msgInfo.is_join || false, msgInfo.is_leave || false)); } else if (messages.chats[user][msg]) { const msgInfo = messages.chats[user][msg]; msgArr.push(new private_message_1.PrivateMessage(this.users.filter((x) => x.name === user)[0], msgInfo.from_user, msgInfo.msg, msgInfo.t, msgInfo.id, msgInfo.to_user)); } } } } msgArr = msgArr.sort((a, b) => a.t - b.t); if (msgArr[msgArr.length - 1] && msgArr[msgArr.length - 1].t) { time = msgArr[msgArr.length - 1].t + 0.01; } if (cb) { cb(msgArr); } }, frequency); } } exports.Account = Account; //# sourceMappingURL=account.js.map