Add more support for custom punishments (#8215)

This commit is contained in:
Mia 2021-04-24 00:40:31 -05:00 committed by GitHub
parent 7154920b2c
commit d992b8f5fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 24 deletions

View File

@ -271,7 +271,7 @@ export const commands: ChatCommands = {
buf += punishments.map(([curRoom, curPunishment]) => {
const [punishType, punishUserid, expireTime, reason] = curPunishment;
let punishDesc = Punishments.roomPunishmentTypes.get(punishType);
let punishDesc = Punishments.roomPunishmentTypes.get(punishType)?.desc;
if (!punishDesc) punishDesc = `punished`;
if (punishUserid !== targetUser.id) punishDesc += ` as ${punishUserid}`;
const expiresIn = new Date(expireTime).getTime() - Date.now();
@ -329,7 +329,8 @@ export const commands: ChatCommands = {
const punishment = Punishments.userids.get(userid);
if (punishment) {
const [punishType, punishUserid, , reason] = punishment;
const punishName = (Punishments.punishmentTypes.get(punishType) || punishType).toUpperCase();
const punishData = (Punishments.punishmentTypes.get(punishType) || punishType);
const punishName = (Array.isArray(punishData) ? punishData[0] : punishData).toUpperCase();
buf += `${punishName}: ${punishUserid}`;
const expiresIn = Punishments.checkLockExpiration(userid);
if (expiresIn) buf += expiresIn;
@ -352,7 +353,7 @@ export const commands: ChatCommands = {
buf += punishments.map(([curRoom, curPunishment]) => {
const [punishType, punishUserid, expireTime, reason] = curPunishment;
let punishDesc = Punishments.roomPunishmentTypes.get(punishType);
let punishDesc = Punishments.roomPunishmentTypes.get(punishType)?.desc;
if (!punishDesc) punishDesc = `punished`;
if (punishUserid !== userid) punishDesc += ` as ${punishUserid}`;
const expiresIn = new Date(expireTime).getTime() - Date.now();

View File

@ -6,7 +6,7 @@ const TICKET_FILE = 'config/tickets.json';
const TICKET_CACHE_TIME = 24 * 60 * 60 * 1000; // 24 hours
const TICKET_BAN_DURATION = 48 * 60 * 60 * 1000; // 48 hours
Punishments.roomPunishmentTypes.set(`TICKETBAN`, 'banned from creating help tickets');
Punishments.addRoomPunishmentType('TICKETBAN', 'banned from creating help tickets');
interface TicketState {
creator: string;

View File

@ -100,8 +100,8 @@ const VALID_IMAGES = [
let MafiaData: MafiaData = Object.create(null);
let logs: MafiaLog = {leaderboard: {}, mvps: {}, hosts: {}, plays: {}, leavers: {}};
Punishments.roomPunishmentTypes.set('MAFIAGAMEBAN', 'banned from playing mafia games');
Punishments.roomPunishmentTypes.set('MAFIAHOSTBAN', 'banned from hosting mafia games');
Punishments.addRoomPunishmentType('MAFIAGAMEBAN', 'banned from playing mafia games');
Punishments.addRoomPunishmentType('MAFIAHOSTBAN', 'banned from hosting mafia games');
const hostQueue: ID[] = [];

View File

@ -6,7 +6,7 @@
import {FS, Utils} from '../../lib';
Punishments.roomPunishmentTypes.set('GIVEAWAYBAN', 'banned from giveaways');
Punishments.addRoomPunishmentType('GIVEAWAYBAN', 'banned from giveaways');
const BAN_DURATION = 7 * 24 * 60 * 60 * 1000;
const RECENT_THRESHOLD = 30 * 24 * 60 * 60 * 1000;

View File

@ -55,6 +55,18 @@ const GROUPCHAT_MONITOR_INTERVAL = 10 * 60 * 1000; // 10 minutes
* A punishment is an array: [punishType, userid | #punishmenttype, expireTime, reason]
*/
export type Punishment = [string, ID | PunishType, number, string];
/**
* Info on punishment types. Can be used for either room punishment types or global punishments
* extended by plugins. The `desc` is the /alts display, and the `callback` is what to do if the user
* _does_ have the punishment (can be used to add extra effects in lieu of something like a loginfilter.)
* Rooms will be specified for room punishments, otherwise it's null for global punishments
*/
export interface PunishInfo {
desc: string;
callback?: (user: User, punishment: Punishment, room: Room | null) => void;
}
interface PunishmentEntry {
ips: string[];
userids: ID[];
@ -182,14 +194,11 @@ export const Punishments = new class {
*
* This map can be extended with custom punishments by chat plugins.
*
* Keys in the map correspond to punishTypes, values signify the way
* they should be displayed in /alt
*
*/
readonly punishmentTypes = new Map<string, string>([
['LOCK', 'locked'],
['BAN', 'globally banned'],
['NAMELOCK', 'namelocked'],
* Keys in the map correspond to PunishInfo */
readonly punishmentTypes = new Map<string, PunishInfo>([
['LOCK', {desc: 'locked'}],
['BAN', {desc: 'globally banned'}],
['NAMELOCK', {desc: 'namelocked'}],
]);
/**
* For room punishments, they can be anything in the roomPunishmentTypes map.
@ -205,12 +214,12 @@ export const Punishments = new class {
* - 'MUTE' (used by getRoomPunishments)
*
*/
readonly roomPunishmentTypes = new Map<string, string>([
['ROOMBAN', 'banned'],
['BLACKLIST', 'blacklisted'],
['BATTLEBAN', 'battlebanned'],
['MUTE', 'muted'],
['GROUPCHATBAN', 'banned from using groupchats'],
readonly roomPunishmentTypes = new Map<string, PunishInfo>([
['ROOMBAN', {desc: 'banned'}],
['BLACKLIST', {desc: 'blacklisted'}],
['BATTLEBAN', {desc: 'battlebanned'}],
['MUTE', {desc: 'muted'}],
['GROUPCHATBAN', {desc: 'banned from using groupchats'}],
]);
constructor() {
setImmediate(() => {
@ -738,6 +747,13 @@ export const Punishments = new class {
return success;
}
addRoomPunishmentType(type: string, desc: string, callback?: PunishInfo['callback']) {
this.roomPunishmentTypes.set(type, {desc, callback});
}
addPunishmentType(type: string, desc: string, callback?: PunishInfo['callback']) {
this.punishmentTypes.set(type, {desc, callback});
}
/*********************************************************
* Specific punishments
*********************************************************/
@ -1387,6 +1403,7 @@ export const Punishments = new class {
if (!punishment) return;
const id = punishment[0];
const punishmentInfo = this.punishmentTypes.get(id);
const punishUserid = punishment[1];
const reason = punishment[3] ? Utils.html`\n\nReason: ${punishment[3]}` : '';
let appeal = ``;
@ -1425,7 +1442,7 @@ export const Punishments = new class {
user.namelocked = punishUserid;
user.resetName();
user.updateIdentity();
} else {
} else if (id === 'LOCK') {
if (punishUserid === '#hostfilter' || punishUserid === '#ipban') {
user.send(`|popup||html|Your IP (${user.latestIp}) is currently locked due to being a proxy. We automatically lock these connections since they are used to spam, hack, or otherwise attack our server. Disable any proxies you are using to connect to PS.\n\n<a href="view-help-request--appeal"><button class="button">Help me with a lock from a proxy</button></a>`);
} else if (user.latestHostType === 'proxy' && user.locked !== user.id) {
@ -1436,6 +1453,8 @@ export const Punishments = new class {
user.notified.lock = true;
user.locked = punishUserid;
user.updateIdentity();
} else if (punishmentInfo?.callback) {
punishmentInfo.callback.call(this, user, punishment, null);
}
Punishments.checkPunishmentTime(user, punishment);
}
@ -1531,6 +1550,11 @@ export const Punishments = new class {
}
}
if (punishment) {
const info = this.roomPunishmentTypes.get(punishment[0]);
if (info?.callback) {
info.callback.call(this, user, punishment, Rooms.get(roomid)!);
return punishment;
}
if (punishment[0] !== 'ROOMBAN' && punishment[0] !== 'BLACKLIST') return null;
const room = Rooms.get(roomid)!;
if (room.game && room.game.removeBannedUser) {
@ -1764,7 +1788,7 @@ export const Punishments = new class {
const punishmentText = punishments.map(([room, punishment]) => {
const [punishType, punishUserid, , reason] = punishment;
if (punishType in PUNISHMENT_POINT_VALUES) points += PUNISHMENT_POINT_VALUES[punishType];
let punishDesc = Punishments.roomPunishmentTypes.get(punishType);
let punishDesc = Punishments.roomPunishmentTypes.get(punishType)?.desc;
if (!punishDesc) punishDesc = `punished`;
if (punishUserid !== userid) punishDesc += ` as ${punishUserid}`;

View File

@ -24,7 +24,7 @@ const MAX_REASON_LENGTH = 300;
const MAX_CUSTOM_NAME_LENGTH = 100;
const TOURBAN_DURATION = 14 * 24 * 60 * 60 * 1000;
Punishments.roomPunishmentTypes.set('TOURBAN', 'banned from tournaments');
Punishments.addRoomPunishmentType('TOURBAN', 'banned from tournaments');
const TournamentGenerators = {
__proto__: null,