"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var sample_teams_exports = {};
__export(sample_teams_exports, {
SampleTeams: () => SampleTeams,
commands: () => commands,
destroy: () => destroy,
handlers: () => handlers,
pages: () => pages,
teamData: () => teamData
});
module.exports = __toCommonJS(sample_teams_exports);
var import_lib = require("../../lib");
const SAMPLE_TEAMS = "config/chat-plugins/sample-teams.json";
const teamData = (() => {
try {
return JSON.parse((0, import_lib.FS)(SAMPLE_TEAMS).readIfExistsSync());
} catch {
return {
whitelist: {},
teams: {}
};
}
})();
function save() {
(0, import_lib.FS)(SAMPLE_TEAMS).writeUpdate(() => JSON.stringify(teamData));
}
for (const formatid in teamData.teams) {
if (!teamData.teams[formatid].uncategorized)
teamData.teams[formatid].uncategorized = {};
}
save();
const SampleTeams = new class SampleTeams2 {
isRoomStaff(user, roomids) {
let matched = false;
if (!roomids?.length)
return false;
for (const roomid of roomids) {
const room = Rooms.search(roomid);
if (!room)
continue;
matched = room.auth.isStaff(user.id);
if (matched)
break;
}
return matched;
}
isDevMod(user) {
return !!Rooms.get("development")?.auth.atLeast(user, "@");
}
checkPermissions(user, roomids) {
return this.isRoomStaff(user, roomids) || user.can("bypassall") || this.isDevMod(user);
}
whitelistedRooms(formatid, names = false) {
formatid = this.sanitizeFormat(formatid);
if (!teamData.whitelist[formatid]?.length)
return null;
return import_lib.Utils.sortBy(teamData.whitelist[formatid], (x) => {
if (!names)
return x;
const room = Rooms.search(x);
if (!room)
return x;
return room.title;
});
}
whitelistRooms(formatids, roomids) {
for (const unsanitizedFormatid of formatids) {
const formatid = this.sanitizeFormat(unsanitizedFormatid);
if (!teamData.whitelist[formatid])
teamData.whitelist[formatid] = [];
for (const roomid of roomids) {
const targetRoom = Rooms.search(roomid);
if (!targetRoom?.persist) {
throw new Chat.ErrorMessage(`Room ${roomid} not found. Check spelling?`);
}
if (teamData.whitelist[formatid].includes(targetRoom.roomid)) {
throw new Chat.ErrorMessage(`Room ${targetRoom.title} is already added.`);
}
teamData.whitelist[formatid].push(targetRoom.roomid);
save();
}
}
}
unwhitelistRoom(formatid, roomid) {
formatid = this.sanitizeFormat(formatid, false);
const targetRoom = Rooms.search(roomid);
if (!targetRoom?.persist)
throw new Chat.ErrorMessage(`Room ${roomid} not found. Check spelling?`);
if (!teamData.whitelist[formatid]?.length)
throw new Chat.ErrorMessage(`No rooms are whitelisted for ${formatid}.`);
if (!teamData.whitelist[formatid].includes(targetRoom.roomid)) {
throw new Chat.ErrorMessage(`Room ${targetRoom.title} isn't whitelisted.`);
}
const index = teamData.whitelist[formatid].indexOf(targetRoom.roomid);
teamData.whitelist[formatid].splice(index, 1);
if (!teamData.whitelist[formatid].length)
delete teamData.whitelist[formatid];
save();
}
sanitizeFormat(formatid, checkExists = false) {
const format = Dex.formats.get(formatid);
if (checkExists && !format.exists) {
throw new Chat.ErrorMessage(`Format "${formatid.trim()}" not found. Check spelling?`);
}
if (format.team) {
throw new Chat.ErrorMessage(`Formats with computer-generated teams can't have team storage.`);
}
return format.id;
}
initializeFormat(formatid) {
if (!teamData.teams[formatid]) {
teamData.teams[formatid] = { uncategorized: {} };
save();
}
}
addCategory(user, formatid, category) {
formatid = this.sanitizeFormat(formatid);
if (!this.checkPermissions(user, teamData.whitelist[formatid])) {
let rankNeeded = `a global administrator`;
if (teamData.whitelist[formatid]) {
rankNeeded = `staff in ${Chat.toListString(teamData.whitelist[formatid], "or")}`;
}
throw new Chat.ErrorMessage(`Access denied. You need to be ${rankNeeded} to add teams for ${formatid}`);
}
category = category.trim();
this.initializeFormat(formatid);
if (this.findCategory(formatid, category)) {
throw new Chat.ErrorMessage(`The category named ${category} already exists.`);
}
teamData.teams[formatid][category] = {};
save();
}
removeCategory(user, formatid, category) {
formatid = this.sanitizeFormat(formatid, false);
if (!this.checkPermissions(user, teamData.whitelist[formatid])) {
let rankNeeded = `a global administrator`;
if (teamData.whitelist[formatid]) {
rankNeeded = `staff in ${Chat.toListString(teamData.whitelist[formatid], "or")}`;
}
throw new Chat.ErrorMessage(`Access denied. You need to be ${rankNeeded} to add teams for ${formatid}`);
}
const categoryName = this.findCategory(formatid, category);
if (!categoryName) {
throw new Chat.ErrorMessage(`There's no category named "${category.trim()}" for the format ${formatid}.`);
}
delete teamData.teams[formatid][categoryName];
save();
}
/**
* @param user
* @param formatid
* @param teamName
* @param team - Can be a team in the packed, JSON, or exported format
* @param category - Category the team will go in, defaults to uncategorized
*/
addTeam(user, formatid, teamName, team, category = "uncategorized") {
formatid = this.sanitizeFormat(formatid);
if (!this.checkPermissions(user, teamData.whitelist[formatid])) {
let rankNeeded = `a global administrator`;
if (teamData.whitelist[formatid]?.length) {
rankNeeded = `staff in ${Chat.toListString(teamData.whitelist[formatid], "or")}`;
}
throw new Chat.ErrorMessage(`Access denied. You need to be ${rankNeeded} to add teams for ${formatid}`);
}
teamName = teamName.trim();
category = category.trim();
this.initializeFormat(formatid);
if (this.findTeamName(formatid, category, teamName)) {
throw new Chat.ErrorMessage(`There is already a team for ${formatid} with the name ${teamName} in the ${category} category.`);
}
if (!teamData.teams[formatid][category])
this.addCategory(user, formatid, category);
teamData.teams[formatid][category][teamName] = Teams.pack(Teams.import(team.trim()));
save();
return teamData.teams[formatid][category][teamName];
}
removeTeam(user, formatid, teamid, category) {
formatid = this.sanitizeFormat(formatid, false);
category = category.trim();
if (!this.checkPermissions(user, teamData.whitelist[formatid])) {
let required = `an administrator`;
if (teamData.whitelist[formatid]) {
required = `staff in ${Chat.toListString(teamData.whitelist[formatid], "or")}`;
}
throw new Chat.ErrorMessage(`Access denied. You need to be ${required} to add teams for ${formatid}`);
}
const categoryName = this.findCategory(formatid, category);
if (!categoryName) {
throw new Chat.ErrorMessage(`There are no teams for ${formatid} under the category ${category.trim()}. Check spelling?`);
}
const teamName = this.findTeamName(formatid, category, teamid);
if (!teamName) {
throw new Chat.ErrorMessage(`There is no team for ${formatid} with the name of "${teamid}". Check spelling?`);
}
const oldTeam = teamData.teams[formatid][categoryName][teamName];
delete teamData.teams[formatid][categoryName][teamName];
if (!Object.keys(teamData.teams[formatid][categoryName]).length)
delete teamData.teams[formatid][categoryName];
if (!Object.keys(teamData.teams[formatid]).filter((x) => x !== "uncategorized").length)
delete teamData.teams[formatid];
save();
return oldTeam;
}
formatTeam(teamName, teamStr, broadcasting = false) {
const team = Teams.unpack(teamStr);
if (!team)
return `Team is not correctly formatted. PM room staff to fix the formatting.`;
let buf = ``;
if (!broadcasting) {
buf += `