How to shuffle my warnlist ?

Hello, i explain ( Im french).
31 Replies
d.js toolkit
d.js toolkit16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Looped
Looped16mo ago
Can you further explain your question?
Snokxx
SnokxxOP16mo ago
When i type this command
/warnlist
/warnlist
my bot respond this :
Snokxx
SnokxxOP16mo ago
I want the sanctions (notes / warns / bans) are shuffled and sorted by date actually my bot send me : notes (sorted by date) then warns (sorted by date) and then bans (sorted by date)
async run(bot, message, args) {
await message.deferReply()
let user = args.getUser("membre");
let member = message.guild.members.cache.get(user.id);

const db = await loadDatabase();
db.connect(() => {
console.log(`🔎 - Warnlist utilisé sur ${user.tag}. (${message.user.tag})`)
});

await db.query(`SELECT * FROM warns WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, warnings) => {
await db.query(`SELECT * FROM bans WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, bans) => {
await db.query(`SELECT * FROM note WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, notes) => {
await db.query(`SELECT * FROM timeoutauto WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, timeout) => {


try {

const embedwarnlist = new Discord.EmbedBuilder()
.setColor("#64FE2E")
.setTitle(`Historique des sanctions de : ${user.tag}`)
.setThumbnail(user.displayAvatarURL({ dynamic: true }))
.setTimestamp()
.addFields([{ name: `🔍 - Informations :`, value: `> **Pseudo** : <@!${user.id}>\n> **ID** : \`${user.id}\`\n> **Création compte** : ➡ <t:${parseInt(user.createdAt / 1000)}:R>\n> **Rejoint le serveur** : ➡ <t:${parseInt(member.joinedTimestamp / 1000)}:R>` }])
async run(bot, message, args) {
await message.deferReply()
let user = args.getUser("membre");
let member = message.guild.members.cache.get(user.id);

const db = await loadDatabase();
db.connect(() => {
console.log(`🔎 - Warnlist utilisé sur ${user.tag}. (${message.user.tag})`)
});

await db.query(`SELECT * FROM warns WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, warnings) => {
await db.query(`SELECT * FROM bans WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, bans) => {
await db.query(`SELECT * FROM note WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, notes) => {
await db.query(`SELECT * FROM timeoutauto WHERE serveurID = '${message.guildId}' AND membreID = '${user.id}'`, async (err, timeout) => {


try {

const embedwarnlist = new Discord.EmbedBuilder()
.setColor("#64FE2E")
.setTitle(`Historique des sanctions de : ${user.tag}`)
.setThumbnail(user.displayAvatarURL({ dynamic: true }))
.setTimestamp()
.addFields([{ name: `🔍 - Informations :`, value: `> **Pseudo** : <@!${user.id}>\n> **ID** : \`${user.id}\`\n> **Création compte** : ➡ <t:${parseInt(user.createdAt / 1000)}:R>\n> **Rejoint le serveur** : ➡ <t:${parseInt(member.joinedTimestamp / 1000)}:R>` }])
notes.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < notes.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `📄 - Note n° : ${i + 1}`, value: `> **Modérateur/Helper** : <@!${(await bot.users.fetch(notes[i].moderateurID)).id}>\n> **NoteID :** \`${notes[i].noteID}\`\n> **Raison** : \`${notes[i].raison}\`\n> **Date** : <t:${Math.floor(parseInt(notes[i].date) / 1000)}:F>` });
embedwarnlist.setColor("#F7FE2E");
}

warnings.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < warnings.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `⚠️ - Avertissement n° : ${i + 1}`, value: `> **Modérateur** : <@!${(await bot.users.fetch(warnings[i].moderateurID)).id}>\n> **Assistant** : \`${warnings[i].assistant}\`\n> **WarnID :** \`${warnings[i].warnID}\` **Gravité** : \`${warnings[i].gravite}\`\n> **Raison** : \`${warnings[i].raison}\`\n> **Date** : <t:${Math.floor(parseInt(warnings[i].date) / 1000)}:F>` });

notes.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < notes.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `📄 - Note n° : ${i + 1}`, value: `> **Modérateur/Helper** : <@!${(await bot.users.fetch(notes[i].moderateurID)).id}>\n> **NoteID :** \`${notes[i].noteID}\`\n> **Raison** : \`${notes[i].raison}\`\n> **Date** : <t:${Math.floor(parseInt(notes[i].date) / 1000)}:F>` });
embedwarnlist.setColor("#F7FE2E");
}

warnings.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < warnings.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `⚠️ - Avertissement n° : ${i + 1}`, value: `> **Modérateur** : <@!${(await bot.users.fetch(warnings[i].moderateurID)).id}>\n> **Assistant** : \`${warnings[i].assistant}\`\n> **WarnID :** \`${warnings[i].warnID}\` **Gravité** : \`${warnings[i].gravite}\`\n> **Raison** : \`${warnings[i].raison}\`\n> **Date** : <t:${Math.floor(parseInt(warnings[i].date) / 1000)}:F>` });

embedwarnlist.setColor("#FF8000");
}

timeout.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < timeout.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `🚫⏳ - Timeout auto n° : ${i + 1}`, value: `> **Raison** : \`${timeout[i].raison}\`\n> **Durée timeout** : \`${timeout[i].time}\`\n> **Date** : <t:${Math.floor(parseInt(timeout[i].date) / 1000)}:F>` });
embedwarnlist.setColor("#FE2E2E");
}

bans.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < bans.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `⛔️ - Bannissement n° : ${i + 1}`, value: `> **Modérateur** : <@!${(await bot.users.fetch(bans[i].moderateurID)).id}>\n> **Assistant** : \`${bans[i].assistant}\`\n> **Raison** : \`${bans[i].raison}\`\n> **Date** : <t:${Math.floor(parseInt(bans[i].date) / 1000)}:F>` });
embedwarnlist.setColor("#FE2E2E");
}


await message.followUp({ embeds: [embedwarnlist] });
embedwarnlist.setColor("#FF8000");
}

timeout.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < timeout.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `🚫⏳ - Timeout auto n° : ${i + 1}`, value: `> **Raison** : \`${timeout[i].raison}\`\n> **Durée timeout** : \`${timeout[i].time}\`\n> **Date** : <t:${Math.floor(parseInt(timeout[i].date) / 1000)}:F>` });
embedwarnlist.setColor("#FE2E2E");
}

bans.sort((a, b) => parseInt(a.date) - parseInt(b.date));
for (let i = 0; i < bans.length; i++) {
embedwarnlist.addFields({ name: " ", value: " " });
embedwarnlist.addFields({ name: `⛔️ - Bannissement n° : ${i + 1}`, value: `> **Modérateur** : <@!${(await bot.users.fetch(bans[i].moderateurID)).id}>\n> **Assistant** : \`${bans[i].assistant}\`\n> **Raison** : \`${bans[i].raison}\`\n> **Date** : <t:${Math.floor(parseInt(bans[i].date) / 1000)}:F>` });
embedwarnlist.setColor("#FE2E2E");
}


await message.followUp({ embeds: [embedwarnlist] });
U have an idea ? thank's ! 🙂
Snokxx
SnokxxOP16mo ago
my database is like this :
Snokxx
SnokxxOP16mo ago
what you're telling me is that I should put everything in the same table ?
Looped
Looped16mo ago
As they said, merging your data* would allow you to shuffle and sort by date It doesn't necessarily have to be in one table
Snokxx
SnokxxOP16mo ago
how to merging my data, if their on different tables ?
Looped
Looped16mo ago
You can get all the data from your database and extract it to an array which can then be sorted by date Instead of mapping through each table manually
Snokxx
SnokxxOP16mo ago
How i do this ? 😐
Looped
Looped16mo ago
You can spread ... each tables data into your array. Example:
const moderationActions = [...warnings, ...bans, ...notes]
const moderationActions = [...warnings, ...bans, ...notes]
Then you can sort that array by time using the sort method It's less messy since they'd be able to keep their tables intact and can track them in an organised matter
Snokxx
SnokxxOP16mo ago
Yes it's sure
Looped
Looped16mo ago
But that's just what I recommend, you can choose what method you want for this and I hope you can achieve it. The result should be the same either way
Snokxx
SnokxxOP16mo ago
yes the result will be the same, only putting everything back in a single table will be long (I have some mdoeration data) i want to try this Im here
Snokxx
SnokxxOP16mo ago
Snokxx
SnokxxOP16mo ago
how do you think i should do ?
Looped
Looped16mo ago
Now you can sort that array by time then map through it to create your fields assuming they have similar properties
Snokxx
SnokxxOP16mo ago
like this ?
Snokxx
SnokxxOP16mo ago
Looped
Looped16mo ago
Can you show me how an example of each can look like on a database? @snokxx I'd like to suggest another way of going through this as you don't see to fully understand the past method
Snokxx
SnokxxOP16mo ago
Snokxx
SnokxxOP16mo ago
Ok ^^
Snokxx
SnokxxOP16mo ago
Looped
Looped16mo ago
How about doing the same thing but with for loops and adding a type property for the embed, example:
const sanctions = [];

for (const warning of warnings) {
sanctions.push({
type: "warning",
moderator: warning.moderateurID,
reason: warning.raison,
date: warning.date,
});
}

for (const ban of bans) {
sanctions.push({
type: "ban",
moderator: ban.moderateurID,
reason: ban.raison,
date: ban.date,
});
}

for (const note of notes) {
sanctions.push({
type: "note",
moderator: note.moderateurID,
reason: note.raison,
date: note.date,
});
}
const sanctions = [];

for (const warning of warnings) {
sanctions.push({
type: "warning",
moderator: warning.moderateurID,
reason: warning.raison,
date: warning.date,
});
}

for (const ban of bans) {
sanctions.push({
type: "ban",
moderator: ban.moderateurID,
reason: ban.raison,
date: ban.date,
});
}

for (const note of notes) {
sanctions.push({
type: "note",
moderator: note.moderateurID,
reason: note.raison,
date: note.date,
});
}
Obviously these aren't all the properties but just an example Then you can:
sanctions.sort((a, b) => a.date - b.date);
sanctions.sort((a, b) => a.date - b.date);
Snokxx
SnokxxOP16mo ago
ok i begin to understand i try to buildthis on my code
Looped
Looped16mo ago
Great, then you can:
for (const sanction of sanctions) {
embed.addFields({
name: `${sanction.type} n° : ${sanctions.indexOf(sanction) + 1}`,
value: `> **Modérateur** : <@!${sanction.moderator}>\n> **Raison** : \`${sanction.reason}\`\n> **Date** : <t:${Math.floor(parseInt(sanction.date) / 1000)}:F>`,
});
}
for (const sanction of sanctions) {
embed.addFields({
name: `${sanction.type} n° : ${sanctions.indexOf(sanction) + 1}`,
value: `> **Modérateur** : <@!${sanction.moderator}>\n> **Raison** : \`${sanction.reason}\`\n> **Date** : <t:${Math.floor(parseInt(sanction.date) / 1000)}:F>`,
});
}
Loop through the sanctions and put them on the fields successfully Instead of looping through each table individually
Snokxx
SnokxxOP16mo ago
Looped
Looped16mo ago
Have you got any issues with that?
Snokxx
SnokxxOP16mo ago
i fix some issues the bot crash I think it's good, finally it's functional thank u so much
Looped
Looped16mo ago
No worries, lock this thread once you're done!
Snokxx
SnokxxOP16mo ago
I just have to aesthetically change 2-3 things u are the best thank u men ! 🙂
Want results from more Discord servers?
Add your server