Repository navigation
Expand file tree
/
Copy pathleadApo.js
More file actions
124 lines (107 loc) · 4.17 KB
/
Copy pathleadApo.js
File metadata and controls
124 lines (107 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Colors, EmbedBuilder, MessageFlags } from "discord.js";
import { SlashCommandBuilder } from "@discordjs/builders";
import { channelSend, fetchMember, interactionReply } from "ewilib";
import { interactionEditReply } from "./polls/pollsUtils.js";
import { isAdmin, removeApologyCount } from "../helpers/index.js";
import { PERSONALITY } from "../classes/personality.js";
const command = new SlashCommandBuilder()
.setName(PERSONALITY.getPersonality().leaderboardApology.name)
.setDescription(PERSONALITY.getPersonality().leaderboardApology.description)
.setDefaultMemberPermissions(0x0000010000000000);
const action = async (interaction) => {
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
const perso = PERSONALITY.getPersonality().leaderboardApology;
if (!isAdmin(interaction.user.id)) {
console.log(`${interaction.user.id} tryed to use /leadApo`);
interactionEditReply(interaction, perso.errorNotAllowed);
return;
}
const client = interaction.client;
const db = client.db;
const dbData = db.data.apologiesCounting; //array of {userId, counter}
interaction.channel.sendTyping();
const sorted = dbData.sort((a, b) => a.counter - b.counter); // sort users by counters
console.log("sorted", sorted);
const guildMembers = interaction.guild.members;
const baseValue = "```\n";
let fields = [
{ name: "10-75", value: baseValue, max: 75, min: 10 },
{ name: "76-150", value: baseValue, max: 150, min: 76 },
{ name: ">150", value: baseValue, max: -1, min: 151 },
{ name: "top 3", value: baseValue },
]; //initiate future embed fields
let count = 0;
for (const cur of sorted) {
//for every user, write in the rigth place
let guildMember;
try {
guildMember = await fetchMember(guildMembers, cur.userId); //get guildMember
} catch {
//if not found, not in serveur anymore => remove from db
removeApologyCount(db, cur.userId);
}
if (guildMember && cur.counter >= 10) {
//if found && enough apologies
console.log(
"nickname",
guildMember.nickname,
"username",
guildMember.user.username,
"counter",
cur.counter,
);
const userNickname = guildMember.nickname
? guildMember.nickname
: guildMember.user.username; //get nickname
const nickSliced = userNickname.slice(0, 25).padEnd(25, " ");
const line = `${nickSliced}: ${cur.counter}`; // add count to the line
//separate data
if (count >= sorted.length - 3) {
//if top3
fields[fields.length - 1].value = `${
fields[fields.length - 1].value
}${line}\n`;
} else {
const isInRange = fields
.slice(0, fields.length - 1)
.reduce((acc, fld) => {
if (fld.max === -1) return [...acc, fld.min <= cur.counter];
else
return [...acc, fld.min <= cur.counter && cur.counter <= fld.max];
}, []); //find which field where counter is in range, return bool
const idx = isInRange.findIndex((bool) => bool); //get field index where value is true
if (idx !== -1) fields[idx].value = `${fields[idx].value}${line}\n`; //update field for any idx correct value
}
}
count = count + 1; //for find top3
}
fields.forEach((cur) => (cur.value = cur.value + "```"));
//build embed
const ePerso = perso.embed; //get embed personality
const embed = new EmbedBuilder() //create embed
.setColor(Colors.Orange)
.setTimestamp()
.setTitle(ePerso.title)
.setDescription(ePerso.description)
.addFields(fields.slice(0, 3))
.addFields({
name: fields[3].name,
value: `${ePerso.top3} ${fields[3].value}`,
});
const message = await channelSend(interaction.channel, { embeds: [embed] });
if (message) interactionEditReply(interaction, perso.sent);
else interactionEditReply(interaction, perso.errorNotSent);
};
const leaderboardApology = {
command,
name: "leadApo",
action,
help: (interaction) => {
const perso = PERSONALITY.getPersonality().leaderboardApology;
interactionReply(interaction, perso.help);
},
admin: true,
releaseDate: null,
sentinelle: true,
};
export default leaderboardApology;