fix internal bot, log channel

This commit is contained in:
Hri7566 2022-09-07 03:10:07 -04:00
parent dc02521c68
commit 5c9f2421c3
5 changed files with 163 additions and 32 deletions

@ -1 +1 @@
Subproject commit 91ff8343d35176eb8bcfb4c51396871a7ddac5b6 Subproject commit b1dc90c9b890b6c0d2446ebf5d6c419cb75336a6

View File

@ -10,8 +10,24 @@ const Color = require('./Color');
const { getTimeColor } = require('./ColorEncoder.js'); const { getTimeColor } = require('./ColorEncoder.js');
const { InternalBot } = require('./InternalBot'); const { InternalBot } = require('./InternalBot');
function ansiRegex({onlyFirst = false} = {}) {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))'
].join('|');
return new RegExp(pattern, onlyFirst ? undefined : 'g');
}
const LOGGER_PARTICIPANT = {
name: 'Logger',
color: '#72f1b8',
_id: 'logger',
id: 'logger'
}
class Channel extends EventEmitter { class Channel extends EventEmitter {
constructor(server, _id, settings) { constructor(server, _id, settings, cl) {
super(); super();
this.logger = new Logger(`Room - ${_id}`); this.logger = new Logger(`Room - ${_id}`);
this._id = _id; this._id = _id;
@ -39,21 +55,53 @@ class Channel extends EventEmitter {
this.logger.log('Created'); this.logger.log('Created');
Database.getRoomSettings(this._id, (err, set) => { if (this._id == 'supersecretsettings') {
if (err) { if (cl.user.hasFlag('admin')) {
return; delete this.crown;
}
this.settings = RoomSettings.changeSettings(this.settings, true); Logger.buffer.forEach(str => {
this.chatmsgs = set.chat; this.chatmsgs.push({
this.connections.forEach(cl => { m: 'a',
cl.sendArray([{ p: LOGGER_PARTICIPANT,
m: 'c', a: str.replace(ansiRegex(), '')
c: this.chatmsgs.slice(-1 * 32) });
}]); });
Logger.on('buffer update', (str) => {
this.chatmsgs.push({
m: 'a',
p: LOGGER_PARTICIPANT,
a: str.replace(ansiRegex(), '')
});
this.sendChatArray();
});
this.emit('update');
let c = new Color(LOGGER_PARTICIPANT.color);
c.add(-0x40, -0x40, -0x40);
this.settings = RoomSettings.changeSettings({
color: c.toHexa(),
chat: true,
crownsolo: true,
lobby: false,
owner_id: LOGGER_PARTICIPANT._id
}, true);
} else {
cl.setChannel('test/awkward');
}
} else {
Database.getRoomSettings(this._id, (err, set) => {
if (err) {
return;
}
this.settings = RoomSettings.changeSettings(this.settings, true);
this.chatmsgs = set.chat;
this.sendChatArray();
this.setData();
}); });
this.setData(); }
});
if (this.isLobby(this._id)) { if (this.isLobby(this._id)) {
this.colorInterval = setInterval(() => { this.colorInterval = setInterval(() => {
@ -72,6 +120,15 @@ class Channel extends EventEmitter {
this.setData(); this.setData();
} }
sendChatArray() {
this.connections.forEach(cl => {
cl.sendArray([{
m: 'c',
c: this.chatmsgs.slice(-1 * 32)
}]);
});
}
setDefaultLobbyColorBasedOnDate() { setDefaultLobbyColorBasedOnDate() {
let col = getTimeColor(); let col = getTimeColor();
let col2 = new Color(col.r - 0x40, col.g - 0x40, col.b - 0x40); let col2 = new Color(col.r - 0x40, col.g - 0x40, col.b - 0x40);
@ -103,7 +160,7 @@ class Channel extends EventEmitter {
cl.initParticipantQuotas(); cl.initParticipantQuotas();
// no users / already had crown? give crown // no users / already had crown? give crown
if (((this.connections.length == 0 && Array.from(this.ppl.values()).length == 0) && this.isLobby(this._id) == false) || this.crown && (this.crown.userId == cl.user._id)) { if (((this.connections.length == 0 && Array.from(this.ppl.values()).length == 0) && this.isLobby(this._id) == false) || this.crown && (this.crown.userId == cl.user._id || this.settings['owner_id'] == cl.user._id)) {
// user owns the room // user owns the room
// we need to switch the crown to them // we need to switch the crown to them
//cl.quotas.a.setParams(Quota.PARAMS_A_CROWNED); //cl.quotas.a.setParams(Quota.PARAMS_A_CROWNED);
@ -278,7 +335,7 @@ class Channel extends EventEmitter {
if (this._id == "lobby") return; if (this._id == "lobby") return;
this.destroyed = true; this.destroyed = true;
this._id; this._id;
console.log(`Deleted room ${this._id}`); this.logger.log(`Deleted room ${this._id}`);
this.settings = undefined; this.settings = undefined;
this.ppl; this.ppl;
this.connnections; this.connnections;

View File

@ -7,6 +7,12 @@ const Database = require("./Database.js");
const { EventEmitter } = require('events'); const { EventEmitter } = require('events');
class Client extends EventEmitter { class Client extends EventEmitter {
/**
* Server-side client representation
* @param {*} ws WebSocket object
* @param {*} req WebSocket request
* @param {*} server Server
*/
constructor(ws, req, server) { constructor(ws, req, server) {
super(); super();
EventEmitter.call(this); EventEmitter.call(this);
@ -35,14 +41,28 @@ class Client extends EventEmitter {
}); });
} }
/**
* Check if user is connected
* @returns boolean
*/
isConnected() { isConnected() {
return this.ws && this.ws.readyState === WebSocket.OPEN; return this.ws && this.ws.readyState === WebSocket.OPEN;
} }
/**
* Check if user is connecting
* @returns boolean
*/
isConnecting() { isConnecting() {
return this.ws && this.ws.readyState === WebSocket.CONNECTING; return this.ws && this.ws.readyState === WebSocket.CONNECTING;
} }
/**
* Move user to channel
* @param {string} _id User ID
* @param {*} settings Settings object
* @returns undefined
*/
setChannel(_id, settings) { setChannel(_id, settings) {
if (this.channel && this.channel._id == _id) return; if (this.channel && this.channel._id == _id) return;
if (this.server.rooms.get(_id)) { if (this.server.rooms.get(_id)) {
@ -74,7 +94,7 @@ class Client extends EventEmitter {
this.channel = this.server.rooms.get(_id); this.channel = this.server.rooms.get(_id);
this.channel.join(this); this.channel.join(this);
} else { } else {
let room = new Channel(this.server, _id, settings); let room = new Channel(this.server, _id, settings, this);
this.server.rooms.set(_id, room); this.server.rooms.set(_id, room);
if (this.channel) this.channel.emit("bye", this); if (this.channel) this.channel.emit("bye", this);
this.channel = this.server.rooms.get(_id); this.channel = this.server.rooms.get(_id);
@ -82,6 +102,10 @@ class Client extends EventEmitter {
} }
} }
/**
* Send data to client
* @param {any[]} arr Array of messages
*/
sendArray(arr) { sendArray(arr) {
if (this.isConnected()) { if (this.isConnected()) {
//console.log(`SEND: `, JSON.colorStringify(arr)); //console.log(`SEND: `, JSON.colorStringify(arr));
@ -89,6 +113,12 @@ class Client extends EventEmitter {
} }
} }
/**
* Set username in database
* @param {string} name Username
* @param {boolean} admin Is admin?
* @returns undefined
*/
userset(name, admin) { userset(name, admin) {
if (name.length > 40 && !admin) return; if (name.length > 40 && !admin) return;
if (!this.quotas.userset.attempt()) return; if (!this.quotas.userset.attempt()) return;
@ -106,6 +136,9 @@ class Client extends EventEmitter {
}); });
} }
/**
* Set rate limits
*/
initParticipantQuotas() { initParticipantQuotas() {
this.quotas = { this.quotas = {
//"chat": new Quota(Quota.PARAMS_A_NORMAL), //"chat": new Quota(Quota.PARAMS_A_NORMAL),
@ -125,6 +158,9 @@ class Client extends EventEmitter {
} }
} }
/**
* Stop the client
*/
destroy() { destroy() {
this.user.stopFlagEvents(); this.user.stopFlagEvents();
this.ws.close(); this.ws.close();
@ -140,6 +176,9 @@ class Client extends EventEmitter {
this.destroied = true; this.destroied = true;
} }
/**
* Internal
*/
bindEventListeners() { bindEventListeners() {
this.ws.on("message", (evt, admin) => { this.ws.on("message", (evt, admin) => {
try { try {
@ -170,6 +209,9 @@ class Client extends EventEmitter {
}); });
} }
/**
* Send admin data bus message
*/
sendAdminData() { sendAdminData() {
let data = {}; let data = {};
data.m = "data"; data.m = "data";

View File

@ -1,5 +1,6 @@
const { EventEmitter } = require('events'); const { EventEmitter } = require('events');
const { Command } = require('./Command'); const { Command } = require('./Command');
const Color = require('../Color');
class InternalBot { class InternalBot {
static on = EventEmitter.prototype.on; static on = EventEmitter.prototype.on;
@ -26,15 +27,18 @@ class InternalBot {
} }
let args = msg.a.split(' '); let args = msg.a.split(' ');
let cmd = args[0].toLowerCase(); let cmd = args[0].toLowerCase().substring(this.prefix.length);
let argcat = msg.a.substring(args[0].length).trim(); let argcat = msg.a.substring(args[0].length).trim();
let p = cl;
if (!args[0].startsWith(this.prefix)) return;
switch (cmd) { switch (cmd) {
case "!ping": case "ping":
ch.adminChat("pong"); ch.adminChat('pong');
break; break;
case "!setcolor": case "setcolor":
case "!color": case "color":
if (!isAdmin) { if (!isAdmin) {
ch.adminChat("You do not have permission to use this command."); ch.adminChat("You do not have permission to use this command.");
return; return;
@ -65,10 +69,10 @@ class InternalBot {
} }
ch.updateCh(); ch.updateCh();
break; break;
case "!users": case "users":
ch.adminChat(`There are ${ch.server.connections.size} users online.`); ch.adminChat(`There are ${ch.server.connections.size} users online.`);
break; break;
case "!chown": case "chown":
if (!isAdmin) return; if (!isAdmin) return;
let id = p.participantId; let id = p.participantId;
if (args[1]) { if (args[1]) {
@ -78,14 +82,22 @@ class InternalBot {
ch.chown(id); ch.chown(id);
} }
break; break;
case "!chlist": case "chlist":
case "!channellist": case "channellist":
if (!isAdmin) return; if (!isAdmin) return;
ch.adminChat("Channels:"); ch.adminChat("Channels:");
for (let [_id] of ch.server.rooms) { for (let [_id] of ch.server.rooms) {
ch.adminChat(`- ${_id}`); ch.adminChat(`- ${_id}`);
} }
break; break;
case "restart":
if (!isAdmin) return;
cl.server.restart();
break;
case "eval":
if (!isAdmin) return;
cl.server.eval(argcat);
break;
} }
}); });
} }

View File

@ -1,28 +1,48 @@
const chalk = require('chalk'); const chalk = require('chalk');
const { EventEmitter } = require('events');
class Logger { class Logger {
static buffer = [];
static on = EventEmitter.prototype.on;
static off = EventEmitter.prototype.off;
static once = EventEmitter.prototype.once;
static emit = EventEmitter.prototype.emit;
constructor (context) { constructor (context) {
this.context = context; this.context = context;
} }
log(args) { log(args) {
console.log(chalk.green(`[`) + chalk.green(`${this.context}`) + chalk.green(`]`), args); let str = chalk.green(`[`) + chalk.green(`${this.context}`) + chalk.green(`]`) + ' ' + args
console.log(str);
this.buffer(str);
} }
warn(args) { warn(args) {
console.warn(chalk.yellow(`[WARN] [`) + chalk.yellow(`${this.context}`) + chalk.yellow(`]`), args); let str = chalk.yellow(`[WARN] [`) + chalk.yellow(`${this.context}`) + chalk.yellow(`]`) + ' ' + args;
console.warn(str);
this.buffer(str);
} }
error(args) { error(args) {
console.error(chalk.red(`[ERR] [`) + chalk.red(`${this.context}`) + chalk.red(`]`), args); let str = chalk.red(`[ERR] [`) + chalk.red(`${this.context}`) + chalk.red(`]`) + ' ' + args;
console.error(str);
this.buffer(str);
} }
debug(args) { debug(args) {
if (process.env.DEBUG_ENABLED) { if (process.env.DEBUG_ENABLED) {
console.log(chalk.blue(`[DEBUG] [`) + chalk.blue(`${this.context}`) + chalk.blue(`]`), args); let str = chalk.blue(`[DEBUG] [`) + chalk.blue(`${this.context}`) + chalk.blue(`]`) + ' ' + args;
console.debug(str);
this.buffer(str);
} }
} }
buffer(str) {
Logger.buffer.push(str);
Logger.emit('buffer update', str);
}
} }
module.exports = Logger; module.exports = Logger;