First step

This commit is contained in:
Neko-Life 2021-07-14 13:05:48 +07:00
parent 304e21f377
commit 702c719833
2 changed files with 92 additions and 96 deletions

View file

@ -57,8 +57,8 @@ module.exports = class mute extends commando.Command {
*/ */
async run(msg, arg) { async run(msg, arg) {
if (!msg.guild.dbLoaded) msg.guild.dbLoad(); if (!msg.guild.dbLoaded) msg.guild.dbLoad();
const muteSettingsDoc = msg.guild.moderation.mute, const MOD = msg.guild.moderation,
defaultDurationDoc = muteSettingsDoc.defaultDuration, MUTE = MOD.mute || { defaultDuration: {} },
args = parseDoubleDash(arg), args = parseDoubleDash(arg),
mentions = parseComa(args.shift()), mentions = parseComa(args.shift()),
durationRegExp = /\d+(?![^ymwdhs])[ymwdhs]?o?/gi, durationRegExp = /\d+(?![^ymwdhs])[ymwdhs]?o?/gi,
@ -71,14 +71,7 @@ module.exports = class mute extends commando.Command {
minute: invokedAt.getMinutes(), minute: invokedAt.getMinutes(),
second: invokedAt.getSeconds() second: invokedAt.getSeconds()
}; };
let theSettingUp = { let durationHasSet = false,
role: undefined,
defaultDuration: {
date: undefined,
string: undefined
}
},
durationHasSet = false,
settingUp = false, settingUp = false,
settingRole = false, settingRole = false,
settingRoleHasSet = false, settingRoleHasSet = false,
@ -141,7 +134,7 @@ module.exports = class mute extends commando.Command {
const key = cleanMentionID(argument); const key = cleanMentionID(argument);
let role = getRole(msg.guild, key)?.id; let role = getRole(msg.guild, key)?.id;
if (role || /^none$/i.test(key)) { if (role || /^none$/i.test(key)) {
theSettingUp.role = role; MUTE.role = role;
} else { } else {
resultMsg += `No role found for: **${argument}**\n`; resultMsg += `No role found for: **${argument}**\n`;
} }
@ -149,15 +142,15 @@ module.exports = class mute extends commando.Command {
} }
} }
} }
const roleConfCheck = msg.guild.roles.cache.get(muteSettingsDoc?.role); const roleConfCheck = msg.guild.roles.cache.get(MUTE?.role);
if (!roleConfCheck && !settingUp) { if (!roleConfCheck && !settingUp) {
resultMsg += `No mute role configured! Run \`${msg.guild.commandPrefix}${this.name} --settings <--role --<role_[name | ID]>> [--duration --<duration>\` to set it up.`; resultMsg += `No mute role configured! Run \`${msg.guild.commandPrefix}${this.name} --settings <--role --<role_[name | ID]>> [--duration --<duration>\` to set it up.`;
} }
let untilDate = new Date(String(duration.year), String(duration.month), String(duration.date), String(duration.hour), String(duration.minute), String(duration.second)); let untilDate = new Date(String(duration.year), String(duration.month), String(duration.date), String(duration.hour), String(duration.minute), String(duration.second));
if (untilDate.toString() === "Invalid Date") untilDate = "Indefinite"; if (untilDate.toString() === "Invalid Date") untilDate = "Indefinite";
if (untilDate?.toUTCString() === invokedAt.toUTCString() && !settingDuration) { if (untilDate?.toUTCString?.() === invokedAt.toUTCString() && !settingDuration) {
if (defaultDurationDoc?.date?.valueOf() > 0) { if (MUTE.defaultDuration.date?.valueOf() > 0) {
untilDate = new Date(invokedAt.valueOf() + defaultDurationDoc.date.valueOf() - 1000); untilDate = new Date(invokedAt.valueOf() + MUTE.defaultDuration.date.valueOf() - 1000);
} else { } else {
untilDate = "Indefinite"; untilDate = "Indefinite";
} }
@ -196,19 +189,14 @@ module.exports = class mute extends commando.Command {
} }
if (settingDuration && !settingDurationHasSet && timeForMessage.length > 0) { if (settingDuration && !settingDurationHasSet && timeForMessage.length > 0) {
settingDurationHasSet = true; settingDurationHasSet = true;
theSettingUp.defaultDuration.date = elapsedTime, MUTE.defaultDuration.date = elapsedTime,
theSettingUp.defaultDuration.string = timeForMessage.join(" "); MUTE.defaultDuration.string = timeForMessage.join(" ");
} }
} }
if (settingUp || !roleConfCheck && !settingUp) { if (settingUp || !roleConfCheck && !settingUp) {
if (settingRoleHasSet) { if (settingDurationHasSet || settingRoleHasSet) {
await col.updateOne({ document: msg.guild.id }, { $set: { "moderation.settings.mute.role": theSettingUp.role } }, { upsert: true }).catch(e => { return trySend(this.client, msg, "```js\n" + e.stack + "```") }); MOD.mute = MUTE;
} }
if (durationHasSet) {
await col.updateOne({ document: msg.guild.id }, { $set: { "moderation.settings.mute.defaultDuration": theSettingUp.defaultDuration } }, { upsert: true }).catch(e => { return trySend(this.client, msg, "```js\n" + e.stack + "```") });
}
const defaultDurationDoc = muteSettingsDoc?.defaultDuration,
roleDoc = muteSettingsDoc?.role;
let settings = defaultImageEmbed(msg); let settings = defaultImageEmbed(msg);
settings settings
.setTitle("Mute Configuration") .setTitle("Mute Configuration")

View file

@ -8,22 +8,25 @@ Structures.extend("Guild", g => {
return class Guild extends g { return class Guild extends g {
constructor(client, data) { constructor(client, data) {
super(client, data); super(client, data);
this.dbLoaded = false;
} }
async dbLoad() { async dbLoad() {
return database.collection("Guild").findOne({ document: this.id }).then((r, e) => { return database.collection("Guild").findOne({ document: this.id }, (e, r) => {
if (e) return errLog(e, null, this.client); if (e) return errLog(e, null, this.client);
this.infractions = r?.moderation?.infractions || []; return this.DB = r || {};
this.moderation = r?.moderation?.settings || {};
this.defaultEmbed = r?.settings?.defaultEmbed || {};
this.quoteOTD = r?.settings?.quoteOTD || {};
this.eventChannels = r?.settings?.eventChannels || {};
return this.dbLoaded = true;
}); });
} }
async setDb(Db, empty = false) {
if (typeof Db !== "object") throw new TypeError("Expected 'object'; Got '" + typeof Db + "'");
if (Db === {} && !empty) throw new Error("Empty!");
return database.collection("Guild").updateOne({ document: this.id }, { $set: Db, $setOnInsert: { document: this.id } },
{ upsert: true }, (e) => {
if (e) return errLog(e, null, this.client);
return this.DB = Db;
});
}
/** /**
* Get user infractions * Get user infractions
* @param {String} get - User ID * @param {String} get - User ID
@ -31,11 +34,10 @@ Structures.extend("Guild", g => {
*/ */
async getInfractions(get) { async getInfractions(get) {
try { try {
const r = await database.collection("Guild").findOne({ document: this.id }); if (!this.DB) await this.dbLoad();
this.infractions = r?.moderation?.infractions;
let found = []; let found = [];
if (this.infractions.length > 0) { if (this.DB.moderation?.infractions?.length > 0) {
for (const inf of this.infractions) { for (const inf of this.DB.moderation.infractions) {
for (const user of inf.by) { for (const user of inf.by) {
if (user.id === get) { if (user.id === get) {
found.push(inf); found.push(inf);
@ -50,46 +52,35 @@ Structures.extend("Guild", g => {
async addInfraction(add) { async addInfraction(add) {
try { try {
const r = await database.collection("Guild").findOne({ document: this.id }); if (!this.DB) await this.dbLoad();
this.infractions = r?.moderation?.infractions; if (!this.DB.moderation?.infractions) this.DB.moderation.infractions = [];
return database.collection("Guild").updateOne({ document: this.id }, { $push: { "moderation.infractions": add } }, (e) => { this.DB.moderation.infractions.push(add);
if (e) return errLog(e, null, this.client); return this.setDb(this.DB);
this.infractions.push(add);
return true;
});
} catch (e) { } } catch (e) { }
} }
async setQuoteOTD(set) { async setQuoteOTD(set) {
return database.collection("Guild").updateOne({ document: this.id }, { $set: { "settings.quoteOTD": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { if (!this.DB) await this.dbLoad();
if (e) return errLog(e, null, this.client); this.DB.settings.quoteOTD = set;
this.quoteOTD = set; return this.setDb(this.DB);
return true;
});
} }
async setEventChannels(set) { async setEventChannels(set) {
return database.collection("Guild").updateOne({ document: this.id }, { $set: { "settings.eventChannels": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { if (!this.DB) await this.dbLoad();
if (e) return errLog(e, null, this.client); this.DB.settings.eventChannels = set;
this.eventChannels = set; return this.setDb(this.DB);
return true;
});
} }
async setDefaultEmbed(set) { async setDefaultEmbed(set) {
return database.collection("Guild").updateOne({ document: this.id }, { $set: { "settings.defaultEmbed": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { if (!this.DB) await this.dbLoad();
if (e) return errLog(e, null, this.client); this.DB.settings.defaultEmbed = set;
this.defaultEmbed = set; return this.setDb(this.DB);
return true;
});
} }
async setModerationSettings(set) { async setModerationSettings(set) {
return database.collection("Guild").updateOne({ document: this.id }, { $set: { "moderation.settings": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { if (!this.DB) await this.dbLoad();
if (e) return errLog(e, null, this.client); this.DB.moderation.settings = set;
this.moderation = set; return this.setDb(this.DB);
return true;
});
} }
} }
}); });
@ -98,53 +89,49 @@ Structures.extend("User", u => {
return class User extends u { return class User extends u {
constructor(client, data) { constructor(client, data) {
super(client, data); super(client, data);
this.dbLoaded = false;
this.cutie = true; this.cutie = true;
this.F = "F";
}
async setF(string) {
return database.collection("User").updateOne({ document: this.id }, { $set: { F: string }, $setOnInsert: { document: this.id } }, { upsert: true }, (e, r) => {
if (e) return errLog(e, null, this.client);
this.F = string;
return true;
});
} }
async dbLoad() { async dbLoad() {
return database.collection("User").findOne({ document: this.id }).then((r, e) => { return database.collection("User").findOne({ document: this.id }, (e, r) => {
if (e) return errLog(e, null, this.client); if (e) return errLog(e, null, this.client);
this.defaultEmbed = r?.settings?.defaultEmbed || {}; if (!r.F) r.F = "F";
this.cachedAvatarURL = this.displayAvatarURL({ format: "png", size: 4096, dynamic: true }); return this.DB = r || {};
this.interactions = r?.interactions || {};
this.description = r?.description;
this.F = r?.F;
return this.dbLoaded = true;
}); });
} }
async setDb(Db, empty = false) {
if (typeof Db !== "object") throw new TypeError("Expected 'object'; Got '" + typeof Db + "'");
if (Db === {} && !empty) throw new Error("Empty!");
return database.collection("User").updateOne({ document: this.id }, { $set: Db, $setOnInsert: { document: this.id } },
{ upsert: true }, (e) => {
if (e) return errLog(e, null, this.client);
return this.DB = Db;
});
}
async setF(string) {
if (!this.DB) await this.dbLoad();
this.DB.F = string;
return this.setDb(this.DB);
}
async setInteractions(count) { async setInteractions(count) {
return database.collection("User").updateOne({ document: this.id }, { $set: { interactions: count }, $setOnInsert: { document: this.id } }, { upsert: true }, (e, r) => { if (!this.DB) await this.dbLoad();
if (e) return errLog(e, null, this.client); this.DB.interactions = count;
this.interactions = count; return this.setDb(this.DB);
return true;
});
} }
async setDescription(set) { async setDescription(set) {
return database.collection("User").updateOne({ document: this.id }, { $set: { description: set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e, r) => { if (!this.DB) await this.dbLoad();
if (e) return errLog(e, null, this.client); this.DB.description = set;
this.description = set; return this.setDb(this.DB);
return true;
});
} }
async setDefaultEmbed(set) { async setDefaultEmbed(set) {
return database.collection("User").updateOne({ document: this.id }, { $set: { "settings.defaultEmbed": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { if (!this.DB) await this.dbLoad();
if (e) return errLog(e, null, this.client); this.DB.defaultEmbed = set;
this.defaultEmbed = set; return this.setDb(this.DB);
return true;
});
} }
} }
}); });
@ -200,8 +187,29 @@ Structures.extend("GuildMember", e => {
super(client, data, guild); super(client, data, guild);
} }
async dbLoad() {
return database.collection("GuildMember").findOne({ document: this.id }, (e, r) => {
if (e) return errLog(e, null, this.client);
return this.DB = r || {};
});
}
async setDb(Db, empty = false) {
if (typeof Db !== "object") throw new TypeError("Expected 'object'; Got '" + typeof Db + "'");
if (Db === {} && !empty) throw new Error("Empty!");
return database.collection("GuildMember").updateOne({ document: this.id }, { $set: Db, $setOnInsert: { document: this.id } },
{ upsert: true }, (e) => {
if (e) return errLog(e, null, this.client);
return this.DB = Db;
});
}
async infractions() { async infractions() {
return this.guild.getInfractions(this.id); return this.guild.getInfractions(this.id);
} }
async mute() {
}
} }
}); });