moving progress

This commit is contained in:
Neko-Life 2021-07-24 00:21:09 +07:00
parent 2bd1774e4f
commit 4f7ba301af
5 changed files with 161 additions and 112 deletions

View file

@ -5,7 +5,7 @@ const { Message, User } = require("discord.js");
/** /**
* @param {Message} msg * @param {Message} msg
* @param {User[]} targetUsers * @param {User[]} targetUsers
* @param {["ban"|"mute"|"kick"|"strike"]} punishment * @param {"ban"|"mute"|"kick"|"strike"} punishment
* @param {string} reason * @param {string} reason
*/ */
module.exports = (msg, targetUsers, punishment, reason) => { module.exports = (msg, targetUsers, punishment, reason) => {

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { DateTime, Settings, Interval } = require("luxon"), const { DateTime, Settings, Interval, DurationObject } = require("luxon"),
DURATION_REGEXP = /[\-]?\d+(?![^ymwdhs])[ymwdhs]?o?/gi, DURATION_REGEXP = /[\-]?\d+(?![^ymwdhs])[ymwdhs]?o?/gi,
DT_PRINT_FORMAT = "DDD'\n'cccc',' tt", DT_PRINT_FORMAT = "DDD'\n'cccc',' tt",
CHECK_FOR_DURATION_REGEXP = /^[\-\+]?\d{1,16}(?![^ymwdhs])[ymwdhs]?o?/i; CHECK_FOR_DURATION_REGEXP = /^[\-\+]?\d{1,16}(?![^ymwdhs])[ymwdhs]?o?/i;
@ -9,7 +9,7 @@ Settings.defaultZone = "utc";
/** /**
* *
* @param {Interval} interval * @param {Interval} interval
* @returns {{ "object": {years: number, months: number,days: number,hours: number,minutes: number,seconds: number}, strings: string[] }} * @returns {{ "object": DurationObject, strings: string[] }}
*/ */
function intervalToDuration(interval) { function intervalToDuration(interval) {
if (!(interval instanceof Interval)) return; if (!(interval instanceof Interval)) return;

View file

@ -32,7 +32,7 @@ module.exports = class lookup extends commando.Command {
const args = arg.split(/ +/); const args = arg.split(/ +/);
let [fetchedMember, fetchedRoles, fetchedChannels, memMes] = [[], [], [], ""]; let [fetchedMember, fetchedRoles, fetchedChannels, memMes] = [[], [], [], ""];
const lowCaseArg0 = args[0]; const lowCaseArg0 = args[0];
if (lowCaseArg0.startsWith("--r ")) { if (lowCaseArg0.startsWith("--r")) {
if (args[1]) { if (args[1]) {
const cleanRoleID = cleanMentionID(arg.slice("--r ".length).trim()); const cleanRoleID = cleanMentionID(arg.slice("--r ".length).trim());
if (/^\d{17,19}$/.test(cleanRoleID)) { if (/^\d{17,19}$/.test(cleanRoleID)) {
@ -50,7 +50,7 @@ module.exports = class lookup extends commando.Command {
} }
} }
} else { } else {
if (lowCaseArg0.startsWith("--c ")) { if (lowCaseArg0.startsWith("--c")) {
if (args[1]) { if (args[1]) {
const cleanChannelID = cleanMentionID(arg.slice("--c ".length).trim()); const cleanChannelID = cleanMentionID(arg.slice("--c ".length).trim());
if (/^\d{17,19}$/.test(cleanChannelID)) { if (/^\d{17,19}$/.test(cleanChannelID)) {
@ -68,7 +68,7 @@ module.exports = class lookup extends commando.Command {
} }
} }
} else { } else {
if (arg.startsWith("--m ")) { if (arg.startsWith("--m")) {
arg = arg.slice("--m ".length).trim(); arg = arg.slice("--m ".length).trim();
} }
arg = cleanMentionID(arg); arg = cleanMentionID(arg);

82
resources/classes.js Normal file
View file

@ -0,0 +1,82 @@
'use strict';
const { intervalToDuration } = require("../cmds/moderation/src/duration");
const { DateTime, Interval, Settings } = require("luxon");
Settings.defaultZone = "utc";
class TimedPunishment {
/**
* @param {{userID: string, duration: { invoked: DateTime, interval: Interval, until: DateTime, duration: { "object": DurationObject, strings: string[] } }, infraction: number, type: "ban" | "mute"}} data
*/
constructor(data = {}) {
this.userID = data.userID;
this.duration = data.duration || {};
this.infraction = data.infraction;
this.type = data.type;
}
setUserID(ID) {
this.userID = ID;
return this;
}
/**
* @param {Date} date
*/
setInvoked(date) {
this.duration.invoked = DateTime.fromJSDate(date);
return this;
}
/**
* @param {Date} starts
* @param {Date} ends
*/
setInterval(starts, ends) {
this.duration.interval = Interval.fromDateTimes(DateTime.fromJSDate(starts), DateTime.fromJSDate(ends));
return this;
}
/**
* @param {Date} date
*/
setUntil(date) {
this.duration.until = DateTime.fromJSDate(date);
return this;
}
/**
* @param {Interval} interval
*/
setDuration(interval) {
this.duration.duration = intervalToDuration(interval);
return this;
}
/**
* @param {Date} starts
* @param {Date} ends
*/
setDataDuration(starts, ends) {
this.setInvoked(starts);
this.setInterval(starts, ends);
this.setUntil(ends);
this.setDuration(this.duration.interval);
return this;
}
setInfraction(number) {
this.infraction = number;
return this;
}
/**
* @param {"ban" | "mute"} type
*/
setPunishment(type) {
this.type = type;
return this;
}
}
module.exports = { TimedPunishment }

View file

@ -3,7 +3,7 @@
const { Structures, Guild, GuildMember, BanOptions } = require("discord.js"), const { Structures, Guild, GuildMember, BanOptions } = require("discord.js"),
{ database } = require("../database/mongo"), { database } = require("../database/mongo"),
{ errLog } = require("./functions"); { errLog } = require("./functions");
const { DateTime, Duration } = require("luxon"); const { TimedPunishment } = require("./classes");
Structures.extend("Guild", u => { Structures.extend("Guild", u => {
return class Guild extends u { return class Guild extends u {
@ -20,8 +20,16 @@ Structures.extend("Guild", u => {
if (!r.moderation) r.moderation = {}; if (!r.moderation) r.moderation = {};
if (!r.settings.eventChannels) r.settings.eventChannels = {}; if (!r.settings.eventChannels) r.settings.eventChannels = {};
if (!r.moderation.settings) r.moderation.settings = {}; if (!r.moderation.settings) r.moderation.settings = {};
if (!r.moderation.infractions) r.moderation.infractions = new Map(); let infractions = new Map(),
if (!r.moderation.timedPunishments) r.moderation.timedPunishments = new Map(); timedPunishments = new Map();
if (r.moderation.infractions)
for (const U in r.moderation.infractions)
infractions.set(U, r.moderation.infractions[U]);
if (r.moderation.timedPunishments)
for (const U in r.moderation.timedPunishments)
timedPunishments.set(U, new TimedPunishment(r.moderation.timedPunishments[U]));
r.moderation.infractions = infractions;
r.moderation.timedPunishments = timedPunishments;
return this.DB = r; return this.DB = r;
}); });
} }
@ -48,20 +56,20 @@ Structures.extend("Guild", u => {
/** /**
* Get user infractions * Get user infractions
* @param {String} get - User ID * @param {string} userID - User ID
* @returns {Promise<Map>} Array of infractions objects * @returns {object[]} Array of infractions objects
*/ */
async getInfractions(get) { async getInfractions(userID) {
try { let ret = []
if (!this.DB) await this.dbLoad(); for (const [k, v] of this.DB.moderation.infractions)
return this.DB.moderation.infractions.filter(r => r.map(u => u.id).includes(get)); if (v.by.map(r => r.id).includes(userID)) ret.push(v);
} catch (e) { } return ret;
} }
async addInfraction(add) { async addInfraction(add) {
try { try {
if (!this.DB) await this.dbLoad(); if (!this.DB) await this.dbLoad();
this.DB.moderation.infractions.push(add); this.DB.moderation.infractions.set(add.infraction, add);
return this.setDb(this.DB); return this.setDb(this.DB);
} catch (e) { } } catch (e) { }
} }
@ -89,6 +97,47 @@ Structures.extend("Guild", u => {
this.DB.moderation.settings = set; this.DB.moderation.settings = set;
return this.setDb(this.DB); return this.setDb(this.DB);
} }
/**
* @param {TimedPunishment} Punishment
* @returns {Map}
*/
setTimedPunishment(Punishment) {
const ret = this.DB.moderation.timedPunishments.set(Punishment.userID + "/" + Punishment.type, Punishment);
this.setDb(this.DB);
return ret;
}
/**
* @param {string} userID
* @param {"mute"|"ban"} type
* @returns
*/
getTimedPunishment(userID, type) {
return this.DB.moderation.timedPunishments.get(userID + "/" + type);
}
/**
* @param {string} userID
* @returns {object[]}
*/
searchTimedPunishment(userID) {
let ret = [];
for (const [k, v] of this.DB.moderation.timedPunishments) if (v.userID === userID) ret.push(v);
return ret;
}
/**
*
* @param {string} userID
* @param {"mute"|"ban"} type
* @returns {boolean}
*/
removeTimedPunishment(userID, type) {
const ret = this.DB.moderation.timedPunishments.delete(userID + "/" + type)
this.setDb(this.DB);
return ret;
}
} }
}); });
@ -106,7 +155,6 @@ Structures.extend("User", u => {
if (!r) r = {}; if (!r) r = {};
if (!r.F) r.F = "<:pepewhysobLife:853237646666891274>"; if (!r.F) r.F = "<:pepewhysobLife:853237646666891274>";
if (!r.cachedAvatarURL) r.cachedAvatarURL = this.displayAvatarURL({ format: "png", size: 4096, dynamic: true }); if (!r.cachedAvatarURL) r.cachedAvatarURL = this.displayAvatarURL({ format: "png", size: 4096, dynamic: true });
if (!r.mutedIn) r.mutedIn = [];
if (!r.interactions) r.interactions = {}; if (!r.interactions) r.interactions = {};
return this.DB = r; return this.DB = r;
}); });
@ -156,104 +204,36 @@ Structures.extend("User", u => {
return this.setDb(this.DB); return this.setDb(this.DB);
} }
/**
* @param {string} guildID
* @param {{state: boolean, duration: object, infraction: number}} state
* @returns {number}
*/
pushMutedIn(guildID, { state: state = true, duration: duration, infraction: infraction }) {
const push = {
guildID: guildID,
state: state,
duration: duration,
infraction: infraction
}
return this.DB.mutedIn.push(push);
}
removeMutedIn(guildID) {
return this.DB.mutedIn = this.DB.mutedIn.filter((r) => r.guildID !== guildID);
}
/**
* @param {string} guildID
* @returns {{data: {state: boolean, duration: duration, infraction: number, guildID: string}, index: number, count: number}}
*/
getMutedIn(guildID) {
let index = -1;
const hmm = this.DB.mutedIn.filter((r, i) => {
if (r.guildID === guildID) {
index = i;
return true;
} else return false;
}),
data = hmm?.[0],
count = hmm?.length || 0;
const D = { data, index, count };
console.log(D);
return D;
}
/**
* @param {string} guildID
* @param {{state: boolean, duration: object, infraction: number}} state
* @returns {{state: boolean, duration: object, infraction: number, guildID: string}}
*/
updateMutedIn(guildID, { state: state = true, duration: duration, infraction: infraction }) {
const I = this.getMutedIn(guildID)?.index;
if (I === -1) return false;
const push = {
guildID: guildID,
state: state,
duration: duration,
infraction: infraction
};
this.DB.mutedIn[I] = push;
return this.DB.mutedIn[I];
}
/** /**
* @param {Guild} guild * @param {Guild} guild
* @param {string} reason * @param {string} reason
* @param {{duration: object, saveTakenRoles: boolean, infraction: number, moderator: GuildMember}} data * @param {{duration: object, saveTakenRoles: boolean, infraction: number, moderator: User}} data
*/ */
async mute(guild, data, reason) { async mute(guild, data, reason) {
if (!guild || !(guild instanceof Guild)) throw new TypeError("Guild is " + typeof guild); if (!guild || !(guild instanceof Guild)) throw new TypeError("Guild is " + typeof guild);
if (!data?.infraction) throw new Error("Missing infraction id"); if (!data?.infraction) throw new Error("Missing infraction id");
if (!this.DB) await this.dbLoad();
if (!guild.DB) await guild.dbLoad(); if (!guild.DB) await guild.dbLoad();
const MEM = guild.member(this); const MEM = guild.member(this);
if (MEM) { if (MEM) {
if (data.moderator.roles.highest.position < MEM.roles.highest.position) throw new Error("You can't mute someone with higher position than you <:nekokekLife:852865942530949160>"); if (data.moderator.roles.highest.position < MEM.roles.highest.position) throw new Error("You can't mute someone with higher position than you <:nekokekLife:852865942530949160>");
return MEM.mute(data, reason) await MEM.mute(data, reason);
} else { }
const MC = this.getMutedIn(guild.id); const MC = guild.getTimedPunishment(this.id, "mute"),
if (MC?.index > -1) { TP = new TimedPunishment({ userID: this.id, duration: data.duration, infraction: data.infraction, type: "mute" });
if (data.duration && MC.data) { return { set: guild.setTimedPunishment(TP), existing: MC }
const ret = this.updateMutedIn(guild.id, { state: true, duration: data.duration, infraction: MC.data.infraction });
this.setDb(this.DB);
return ret;
};
throw new Error("This member is already muted. Provide `[duration]` to set new duration.");
}
const ret = this.pushMutedIn(guild.id, { state: true, duration: data.duration, infraction: data.infraction });
this.setDb(this.DB);
return ret;
};
} }
async unmute(guild, moderator, reason) { async unmute(guild, moderator, reason) {
if (!guild || !(guild instanceof Guild)) throw new TypeError("Guild is " + typeof guild); if (!guild || !(guild instanceof Guild)) throw new TypeError("Guild is " + typeof guild);
if (!this.DB) await this.dbLoad(); if (!guild.DB) await guild.dbLoad();
const MC = suild.getTimedPunishment(this.id, "mute");
if (!MC) throw new Error(this.tag + " isn't muted in " + guild.name);
const MEM = guild.member(this); const MEM = guild.member(this);
if (MEM) { if (MEM) {
if (moderator.roles.highest.position < MEM.roles.highest.position) throw new Error("You can't mute someone with higher position than you <:nekokekLife:852865942530949160>"); if (moderator.roles.highest.position < MEM.roles.highest.position) throw new Error("You can't mute someone with higher position than you <:nekokekLife:852865942530949160>");
return MEM.unmute(reason) await MEM.unmute(reason)
} else {
const ret = this.removeMutedIn(guild.id);
this.setDb(this.DB);
return ret;
} }
return guild.removeTimedPunishment(this.id, "mute");
} }
/** /**
@ -357,22 +337,9 @@ Structures.extend("GuildMember", u => {
*/ */
async mute(data, reason) { async mute(data, reason) {
if (!this.DB) await this.dbLoad(); if (!this.DB) await this.dbLoad();
if (!this.user.DB) await this.user.dbLoad(); if (!data || !data.infraction) throw new Error("Missing infraction id");
if (!this.guild.DB) await this.guild.dbLoad();
if (!data) throw new Error("Missing infraction id");
const MC = this.user.getMutedIn(this.guild.id);
if (!data.infraction && !MC?.data) throw new Error("Missing infraction id");
if (MC?.index > -1) {
if (data.duration && MC.data) {
const ret = this.user.updateMutedIn(this.guild.id, { state: true, duration: data.duration, infraction: MC.data.infraction });
this.user.setDb(this.user.DB);
return ret;
}
throw new Error("This member is already muted. Provide `[duration]` to set new duration.");
}
if (data.saveTakenRoles === undefined) data.saveTakenRoles = true; if (data.saveTakenRoles === undefined) data.saveTakenRoles = true;
const ROLES = this.roles.cache.filter((r) => !r.managed).map(r => r.id); const ROLES = this.roles.cache.filter((r) => !r.managed).map(r => r.id);
if (data.saveTakenRoles && ROLES?.length > 0) { if (data.saveTakenRoles && ROLES?.length > 0) {
console.log("populating takenRoles M"); console.log("populating takenRoles M");