Skip to content

Commit

Permalink
Info: Add /showpunishments command (#5223)
Browse files Browse the repository at this point in the history
* Info: Add /roomstatus command

https://www.smogon.com/forums/threads/3534365/page-81#post-7977912
I'm not a big fan of the approach used here, but I didn't have any better ideas that worked.

* Fix build errors

* Remove response function

* Change command name

* Rewrite

* Use HTML pages

* Reword entries

* Check username map instead of IP map

* Prevent duplicate punishments from appearing.

* Use this.title

* Remove debug code

* Add a check if punishments exists

* Show ip and alts

* Pluralize IP

* Slight refactor in punishDesc

* Add check for Punishments.roomIps existence

* Return an error message for rooms without '-'

More specifically, rooms that aren't public, hidden or private. They couldn't use them anyway. This just handles them correctly.

* Join ip

* Use room.chatRoomData instead of...

checking for '-' in roomids.

* Update help command

* Clarify error message

* Simplify permission check

* Remove muteQueue variable

* Fix punishDesc

Alts would overwrite reason if it existed and punishDesc would always contain undefined due to not being defined empty. This commit fixes both bugs.

* Allow roomstaff to use /sp

* Refactor store iteration to for...of

As per CONTRIBUTING.md
  • Loading branch information
thejetou authored and Bär Halberkamp committed Mar 9, 2019
1 parent 9e7ba68 commit 708f656
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions server/chat-plugins/info.js
Expand Up @@ -299,6 +299,13 @@ const commands = {
this.sendReplyBox(buf);
},

sp: 'showpunishments',
showpunishments(target, room, user) {
if (!room.chatRoomData) return this.errorReply("This command is unavailable in temporary rooms.");
return this.parse(`/join view-punishments-${room}`);
},
showpunishmentshelp: [`/showpunishments - Shows the current punishments in the room. Requires: % @ # & ~`],

'!host': true,
host(target, room, user, connection, cmd) {
if (!target) return this.parse('/help host');
Expand Down Expand Up @@ -2383,6 +2390,67 @@ const commands = {
],
};

/** @type {PageTable} */
const pages = {
punishments(query, user, connection) {
this.title = 'Punishments';
let buf = "";
this.extractRoom();
if (!user.named) return Rooms.RETRY_AFTER_LOGIN;
buf += `<div class="pad"><h2>List of active punishments:</h2>`;
if (!this.can('mute', null, this.room)) return;
if (!this.room.chatRoomData) {
return buf + `<div class="notice message-error">This page is unavailable in temporary rooms / non-existent rooms.</div>`;
}
const store = new Map();
const possessive = (word) => {
const suffix = word.endsWith('s') ? `'` : `'s`;
return `${word}${suffix}`;
};

if (Punishments.roomUserids.get(this.room.id)) {
for (let [key, value] of Punishments.roomUserids.get(this.room.id)) {
if (!store.has(value)) store.set(value, [new Set([value.id]), new Set()]);
store.get(value)[0].add(key);
}
}

if (Punishments.roomIps.get(this.room.id)) {
for (let [key, value] of Punishments.roomIps.get(this.room.id)) {
if (!store.has(value)) store.set(value, [new Set([value.id]), new Set()]);
store.get(value)[1].add(key);
}
}

for (const [punishment, data] of store) {
let [punishType, id, expireTime, reason] = punishment;
let alts = [...data[0]].filter(user => user !== id);
let ip = [...data[1]];
let expiresIn = new Date(expireTime).getTime() - Date.now();
let expireString = Chat.toDurationString(expiresIn, {precision: 1});
let punishDesc = "";
if (reason) punishDesc += ` Reason: ${reason}.`;
if (alts.length) punishDesc += ` Alts: ${alts.join(", ")}.`;
if (user.can('ban', null, this.room) && ip.length) {
punishDesc += ` IPs: ${ip.join(", ")}.`;
}
buf += `<p>- ${possessive(id)} ${punishType.toLowerCase()} expires in ${expireString}.${punishDesc}</p>`;
}

if (this.room.muteQueue) {
for (const entry of this.room.muteQueue) {
let expiresIn = new Date(entry.time).getTime() - Date.now();
if (expiresIn < 0) continue;
let expireString = Chat.toDurationString(expiresIn, {precision: 1});
buf += `<p>- ${possessive(entry.userid)} mute expires in ${expireString}.</p>`;
}
}
buf += `</div>`;
return buf;
},
};

exports.pages = pages;
exports.commands = commands;

process.nextTick(() => {
Expand Down

0 comments on commit 708f656

Please sign in to comment.