created defaultDateFormat

This commit is contained in:
Neko-Life 2021-07-24 08:21:15 +07:00
parent 4f7ba301af
commit e878fa5335
6 changed files with 32 additions and 31 deletions

View file

@ -1,7 +1,7 @@
'use strict';
const commando = require("@iceprod/discord.js-commando");
const { trySend, findMemberRegEx, cleanMentionID, findChannelRegEx, findRoleRegEx, defaultImageEmbed, parseDoubleDash, parseComa, getRole, defaultEventLogEmbed } = require("../../resources/functions");
const { trySend, findMemberRegEx, cleanMentionID, findChannelRegEx, findRoleRegEx, defaultImageEmbed, parseDoubleDash, parseComa, getRole, defaultEventLogEmbed, defaultDateFormat } = require("../../resources/functions");
const { database } = require("../../database/mongo");
const col = database.collection("Guild");
const schedule = database.collection("Schedule");
@ -113,7 +113,8 @@ module.exports = class mute extends commando.Command {
} else return trySend(this.client, msg, "Args: `<[user_[mention|ID|name]]> -- [reason] -- [duration]`. Use `,` to provide multiple user. `--s` to view settings.\nExample:```js\n" + `${msg.guild.commandPrefix + this.name} 580703409934696449, @Shasha#1234, ur mom,#6969,^yuck\\s(ur)?\\s.{5}#\\d+69$--69y69mo69w420d420h420m420s -- Saying "joe"\`\`\``);
if (targetUsers.length > 0) {
let muted = [], cant = [], already = [], infractionN = [];
let muted = [], cant = [], already = [], infractionN = [],
infractionToDoc = createInfraction(msg, targetUsers, "mute", reason);
for (const EXEC of targetUsers) {
try {
@ -127,14 +128,12 @@ module.exports = class mute extends commando.Command {
const emb = defaultEventLogEmbed(msg.guild);
emb.setTitle("You have been muted")
.setDescription("**Reason**\n" + reason)
.addField("At", duration.invoked.toFormat(fn.DT_PRINT_FORMAT), true)
.addField("At", defaultDateFormat(duration.invoked), true)
.addField("For", duration.duration?.strings.join(" ") || "Indefinite", true)
.addField("Until", duration.until?.toFormat(fn.DT_PRINT_FORMAT) || "Never", true);
.addField("Until", duration.until ? defaultDateFormat(duration.until) : "Never", true);
EXEC.createDM().then(r => trySend(msg.client, r, emb));
}
let infractionToDoc = createInfraction(msg, targetUsers, "mute", reason);
infractionToDoc.executed = muted;
infractionToDoc.aborted = already;
infractionToDoc.failed = cant;
@ -163,10 +162,9 @@ module.exports = class mute extends commando.Command {
if (mutedArr.length > 0) mutedStr += `and ${mutedArr.length} more...`;
emb.setDescription("**Reason**\n" + reason)
.addField("Muted", mutedStr || "`[NONE]`")
.addField("At", duration.invoked.toFormat(fn.DT_PRINT_FORMAT), true)
.addField("At", defaultDateFormat(duration.invoked), true)
.addField("For", duration.duration?.strings.join(" ") || "Indefinite", true)
.addField("Until", duration.until?.toFormat(fn.DT_PRINT_FORMAT) || "Never", true)
.addField("Reason", reason);
.addField("Until", duration.until ? defaultDateFormat(duration.until) : "Never", true);
return trySend(msg.client, msg, { content: resultMsg, embed: emb });
}

View file

@ -3,7 +3,7 @@
const commando = require("@iceprod/discord.js-commando");
const { MessageEmbed, User, Message } = require("discord.js");
const { DateTime, Interval } = require("luxon");
const { errLog, trySend, getUser, defaultImageEmbed, splitOnLength } = require("../../resources/functions");
const { errLog, trySend, getUser, defaultImageEmbed, splitOnLength, defaultDateFormat } = require("../../resources/functions");
const getColor = require("../../resources/getColor");
const { DT_PRINT_FORMAT, intervalToDuration } = require("../moderation/src/duration");
@ -32,12 +32,12 @@ module.exports = class profile extends commando.Command {
emb
.setThumbnail(TM.displayAvatarURL({ format: "png", size: 4096, dynamic: true }))
.addField("ID", TM.id)
.addField("Registered", "<t:" + (Math.floor(TM.createdAt.valueOf() / 1000)) + ":F>");
.addField("Registered", defaultDateFormat(TM.createdAt));
if (TM.description) emb.setDescription(TM.description);
if (MEM) {
const RI = MEM.roles.cache.sort((a, b) => b.position - a.position).map(r => r.id).slice(0, -1),
RFS = splitOnLength(RI, 1010, ">, <@&"), INT = Interval.fromDateTimes(DateTime.fromJSDate(MEM.joinedAt), DateTime.now());
emb.addField("Joined", "<t:" + (Math.floor(MEM.joinedAt.valueOf() / 1000)) + `:F>\n(${intervalToDuration(INT).strings.join(" ")} ago)`)
emb.addField("Joined", defaultDateFormat(MEM.joinedAt) + `\n(${intervalToDuration(INT).strings.join(" ")} ago)`)
.addField("Nick", `\`${MEM.displayName}\``);
if (RFS[0]?.length > 0) {
for (const p of RFS) {

View file

@ -2,7 +2,7 @@
const { GuildMember } = require("discord.js"),
{ DateTime } = require("luxon"),
{ getChannel, defaultEventLogEmbed, trySend } = require("../functions"),
{ getChannel, defaultEventLogEmbed, trySend, defaultDateFormat } = require("../functions"),
getColor = require("../getColor"),
{ DT_PRINT_FORMAT } = require("../../cmds/moderation/src/duration");
@ -20,7 +20,7 @@ module.exports = (member) => {
.setTitle("`" + member.user.tag + "` joined")
.setThumbnail(member.user.displayAvatarURL({ format: "png", size: 4096, dynamic: true }))
.setColor(getColor("cyan"))
.addField("Registered", "<t:" + (Math.floor(member.user.createdAt.valueOf() / 1000)) + ":F>", true)
.addField("Registered", defaultDateFormat(member.user.createdAt), true)
.setDescription(`<@!${member.id}> (${member.id}) just joined.\nWe have ${member.guild.memberCount} total members now.`);
return trySend(member.client, log, emb);
}

View file

@ -3,7 +3,7 @@
const { GuildMember } = require("discord.js");
const { DateTime, Interval, Settings } = require("luxon");
const { DT_PRINT_FORMAT, intervalToDuration } = require("../../cmds/moderation/src/duration");
const { getChannel, defaultEventLogEmbed, trySend, splitOnLength } = require("../functions");
const { getChannel, defaultEventLogEmbed, trySend, splitOnLength, defaultDateFormat } = require("../functions");
const getColor = require("../getColor");
Settings.defaultZone = "utc";
@ -24,8 +24,8 @@ module.exports = (member) => {
.setTitle("`" + member.user.tag + "` left")
.setThumbnail(member.user.displayAvatarURL({ format: "png", size: 4096, dynamic: true }))
.setColor(getColor("yellow"))
.addField("Registered", "<t:" + (Math.floor(member.user.createdAt.valueOf() / 1000)) + ":F>", true)
.addField("Joined", "<t:" + (Math.floor(member.joinedAt.valueOf() / 1000)) + `:F>\n(${intervalToDuration(INT).strings.join(" ")} ago)`, true)
.addField("Registered", defaultDateFormat(member.user.createdAt), true)
.addField("Joined", defaultDateFormat(member.joinedAt) + `\n(${intervalToDuration(INT).strings.join(" ")} ago)`, true)
.addField("Nick", "`" + member.displayName + "`")
.setDescription(`<@!${member.id}> (${member.id}) just left.\nWe have ${member.guild.memberCount} total members now.`);
for (const U of RU) {

View file

@ -6,6 +6,7 @@ const { timestampAt } = require('./debug');
const getColor = require('./getColor');
const { randomColors } = require("../config.json");
const { CommandoMessage, CommandoClient } = require('@iceprod/discord.js-commando');
const { DateTime } = require('luxon');
/**
* Log an error. Second or third argument is required
@ -519,6 +520,18 @@ function getRole(guild, key) {
function wait(ms) { return new Promise(r => setTimeout(() => r(), ms)) }
/**
* @param {Date|DateTime|number|string} date - Number | String must be in miliseconds
* @returns {string} - Discord format ready to use
*/
function defaultDateFormat(date) {
let use;
if ((date instanceof Date) || date instanceof DateTime) use = date.valueOf();
else if (typeof date === "string") use = parseInt(date, 10);
else use = date;
return "<t:" + (Math.floor(use / 1000)) + ":F>";
}
const defaultSplitMessage = { maxLength: 2000, char: ",", append: ',```', prepend: '```js\n' };
module.exports = {
cleanMentionID, defaultEventLogEmbed,
@ -526,7 +539,7 @@ module.exports = {
findMemberRegEx, findChannelRegEx, findRoleRegEx,
getChannelMessage, errLog,
execCB, ranLog, noPerm, getUTCComparison,
trySend, tryDelete, tryReact,
trySend, tryDelete, tryReact, defaultDateFormat,
adCheck, defaultImageEmbed, getChannel,
splitOnLength, parseComa, parseDoubleDash, getMember,
parseDash, reValidURL, getUser, getRole, wait, defaultSplitMessage

View file

@ -231,7 +231,7 @@ Structures.extend("User", u => {
const MEM = guild.member(this);
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>");
await MEM.unmute(reason)
await MEM.unmute(reason);
}
return guild.removeTimedPunishment(this.id, "mute");
}
@ -350,18 +350,11 @@ Structures.extend("GuildMember", u => {
try {
if (ROLES?.length > 0) await this.roles.remove(ROLES, reason);
await this.roles.add(this.DB.muteRole, reason);
const ret = this.user.pushMutedIn(this.guild.id, {
state: true,
duration: data.duration,
infraction: data.infraction
});
this.setDb(this.DB);
this.user.setDb(this.user.DB);
return ret;
return true;
} catch (e) {
if (this.DB.takenRoles?.length > 0) await this.roles.add(this.DB.takenRoles, reason).catch(() => { });
if (this.DB.muteRole) await this.roles.remove(this.DB.muteRole, reason).catch(() => { });
this.user.removeMutedIn(this.guild.id);
console.log("clear takenRoles M");
this.DB.takenRoles = [];
this.DB.muteRole = undefined;
@ -371,17 +364,14 @@ Structures.extend("GuildMember", u => {
async unmute(reason) {
if (!this.DB) await this.dbLoad();
if (!this.user.DB) await this.user.dbLoad();
try {
if (this.DB.takenRoles.length > 0) await this.roles.add(this.DB.takenRoles, reason);
if (this.DB.muteRole) await this.roles.remove(this.DB.muteRole, reason);
const ret = this.user.removeMutedIn(this.guild.id);
console.log("clear takenRoles UM");
this.DB.takenRoles = [];
this.DB.muteRole = undefined;
this.setDb(this.DB);
this.user.setDb(this.user.DB)
return ret;
return true;
} catch (e) {
throw e;
}