-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
329 lines (322 loc) · 15.2 KB
/
index.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const Discord = require('discord.js');
const { readFileSync, writeFileSync } = require('fs');
database = JSON.parse(readFileSync('./data.db'));
const { token, prefix, owners, color } = require('./config.json');
const client = new Discord.Client({
intents: 3276799
});
client.login(token);
client.on('ready', () => {
console.log(`[!] — Logged in as ${client.user.tag} (${client.user.id})`);
setInterval(() => {
const guilds = database.guilds || {};
for (const guildId in guilds) {
const guild = client.guilds.cache.get(guildId);
guilds[guildId].forEach(async counterData => {
const channel = guild.channels.cache.get(counterData.id);
if (!channel) return;
let str = await replace(counterData.name, guild, counterData.type === 'role', counterData.roleId);
channel.setName(str);
})
}
}, 30000);
});
function writeDatabase() {
writeFileSync('./data.db', JSON.stringify(database));
}
/**
* @param {String} str
* @param {Guild} guild
*/
async function replace(str, guild, role = false, roleId) {
const members = await guild.members.fetch();
if (role) {
const role = guild.roles.cache.get(roleId);
let roleMembers = members.filter(m => m.roles.cache.has(role.id));
str = str.replace('{count}', roleMembers.size.toString());
return str;
}
str = str
.replace('{members}', guild.memberCount)
.replace('{bots}', members.filter(m => m.user.bot).size)
.replace('{humans}', members.filter(m => !m.user.bot).size)
.replace('{online}', members.filter(m => ['idle', 'dnd', 'online'].includes(m.presence?.status)).size)
.replace('{boosts}', guild.premiumSubscriptionCount)
.replace('{channels}', guild.channels.cache.size)
.replace('{voice}', guild.voiceStates.cache.size)
return str;
}
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g),
command = args.shift().toLowerCase();
if (['counters', 'counter', 'compteurs'].includes(command)) {
if (!owners.includes(message.author.id)) {
const embed = new Discord.EmbedBuilder()
.setTitle('`❌` ▸ Unauthorized user')
.setDescription('*You are not authorized to use this command.*')
.setFooter({ text: message.author.username, iconURL: message.author.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return message.reply({ embeds: [embed], allowedMentions: { repliedUser: false } });
}
if (!database.guilds) database.guilds = {};
const guildData = database.guilds[message.guild.id] || [];
const msg = await message.channel.send({ embeds: [embed()], components: [components()] });
const collector = msg.createMessageComponentCollector({
filter: (i) => {
if (i.user.id !== message.author.id) {
const embed = new Discord.EmbedBuilder()
.setTitle('`❌` ▸ Unauthorized Interaction')
.setDescription('*You are not authorized to use this interaction.*')
.setFooter({ text: message.author.username, iconURL: message.author.displayAvatarURL() })
.setColor(color)
.setTimestamp();
i.reply({ embeds: [embed], ephemeral: true });
return false;
}
return true;
},
time: 120000
});
collector.on('end', () => {
msg.edit({ components: [] });
});
collector.on('collect', async (interaction) => {
const { customId } = interaction;
if (customId === 'add') {
const _embed = new Discord.EmbedBuilder()
.setTitle('`🪄` ▸ Type of counter')
.setDescription('*What is the type of counter?*')
.setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setColor(color)
.setTimestamp();
let _components = {
type: 1,
components: [
{
type: 2,
label: 'Classic Counter',
style: 2,
custom_id: 'classic'
},
{
type: 2,
label: 'Role Counter',
style: 2,
custom_id: 'role'
}
]
}
let reply = await interaction.reply({ embeds: [_embed], components: [_components], fetchReply: true });
const r = await reply.awaitMessageComponent({ time: 30000, filter: (i) => i.user.id === message.author.id });
r.deferUpdate();
let type = r.customId;
let role;
if (type === 'role') {
let roleSelector = {
type: 1,
components: [{
type: 6,
custom_id: 'role-selector',
placeholder: 'Select a role.',
}]
}
reply.edit({ components: [roleSelector], fetchReply: true, embeds: [] });
const _r = await reply.awaitMessageComponent({ time: 30000, filter: (i) => i.user.id === message.author.id });
role = _r.roles.first();
_r.deferUpdate();
}
let channelSelector = {
type: 1,
components: [
{
type: 8,
custom_id: 'channel-selector',
placeholder: 'Select a channel.',
}
]
}
reply.edit({ components: [channelSelector], fetchReply: true, embeds: [] });
const _collector = reply.createMessageComponentCollector({
time: 30000,
filter: (i) => i.user.id === message.author.id
})
_collector.on('end', () => {
reply.delete().catch(() => { });
})
_collector.on('collect', async (_interaction) => {
let channel = _interaction.channels.first();
if (!channel) return;
let modal = {
title: 'Counter name.',
custom_id: 'counter-name-modal',
components: [
{
type: 1,
components: [
{
type: 4,
custom_id: 'counter-name',
placeholder: 'Provide the counter name',
min_length: 1,
max_length: 100,
required: true,
label: `${channel.name}`,
style: 1
}
]
}
]
}
await _interaction.showModal(modal);
const response = await _interaction.awaitModalSubmit({ time: 30000 });
response.deferUpdate();
const name = response.fields.getTextInputValue('counter-name');
if (!name) return;
let data = {
name,
id: channel.id
}
if (type === 'role') {
data.roleId = role.id;
data.type = 'role';
}
guildData.push(data);
database.guilds[message.guild.id] = guildData;
writeDatabase();
msg.edit({ embeds: [embed()], components: [components()] });
_collector.stop();
reply.delete().catch(() => { });
})
} else if (customId === 'remove') {
let menu = {
type: 1,
components: [
{
type: 3,
custom_id: 'counter-selector',
placeholder: 'Select one or more counter(s)',
options: guildData.map((counter, i) => {
return {
label: counter.name,
value: i.toString(),
description: `Type: ${counter.type === 'role' ? 'Role' : 'Classic'}`
}
}),
min_values: 1,
max_values: guildData.length,
}
]
}
let reply = await interaction.reply({ components: [menu], fetchReply: true });
const r = await reply.awaitMessageComponent({ time: 30000, filter: (i) => i.user.id === message.author.id });
r.deferUpdate();
let values = r.values;
values.forEach(value => {
guildData.splice(parseInt(value), 1);
})
database.guilds[message.guild.id] = guildData;
writeDatabase();
msg.edit({ embeds: [embed()], components: [components()] });
reply.delete().catch(() => { });
} else if (customId === 'reset') {
let _c = {
type: 1,
components: [
{
type: 2,
emoji: { name: '✅' },
style: 3,
custom_id: 'confirm'
},
{
type: 2,
emoji: { name: '❌' },
style: 4,
custom_id: 'cancel'
}
]
}
const _e = new Discord.EmbedBuilder()
.setTitle('`🪄` ▸ Reset counters')
.setDescription('*You are going to reset all counters on this server, are you sure ?*')
.setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setColor(color)
.setTimestamp();
const reply = await interaction.reply({ embeds: [_e], components: [_c], fetchReply: true });
const r = await reply.awaitMessageComponent({ time: 30000, filter: (i) => i.user.id === message.author.id });
r.deferUpdate();
database.guilds[message.guild.id] = [];
writeDatabase();
reply.delete().catch(() => { });
msg.edit({ embeds: [embed()], components: [components()] });
}
})
function components() {
return {
type: 1,
components: [
{
type: 2,
emoji: { name: '➕' },
style: 2,
custom_id: 'add'
},
{
type: 2,
emoji: { name: '➖' },
style: 2,
custom_id: 'remove',
disabled: guildData.length === 0
},
{
type: 2,
emoji: { name: '🔄' },
style: 2,
custom_id: 'reset'
}
]
}
}
function embed() {
let counters = guildData.map(counter => `<${counter.type === 'role' ? '@&' : '#'}${counter.type === 'role' ? counter.roleId : counter.id}>${counter.type === 'role' ? `<#${counter.id}>` : ''}: \`${counter.name}\``)
const _e = new Discord.EmbedBuilder()
.setTitle('`🪄` ▸ Counters')
.setDescription(`*Here are the counters available on this server:\n ${counters.join('\n') || '\`None\`'}*`)
.setFooter({ text: 'Use the variables command to see the available variables.', iconURL: message.author.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return _e;
}
} else if (['var', 'variable', 'variables'].includes(command)) {
const embed = new Discord.EmbedBuilder()
.setTitle('`🪄` ▸ Counter variables')
.setDescription('*\`{members}\`: Number of members on the server.\n\`{bots}\`: Number of bots on the server.\n\`{humans}\`: Number of humans on the server.\n\`{online}\`: Number of members online on the server.\n\`{boosts}\`: Number of boosts on the server.\n\`{channels}\`: Number of channels on the server.\n\`{voice}\`: Number of voice members on the server.\n\`{count}\`: To use only for role counters, displays the number of members with the role.*')
.setFooter({ text: message.author.username, iconURL: message.author.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return message.reply({ embeds: [embed], allowedMentions: { repliedUser: false } });
} else if (['help', 'h'].includes(command)) {
const embed = new Discord.EmbedBuilder()
.setTitle('`🪄` ▸ Help Menu')
.setDescription(`*\`${prefix}help\` — Displays the help menu.\n\`${prefix}counters\` — Displays the panel of counters.\n\`${prefix}variables\` — Displays the variables.*`)
.setFooter({ text: message.author.username, iconURL: message.author.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return message.reply({ embeds: [embed], allowedMentions: { repliedUser: false } });
}
})
process.on('unhandledRejection', (reason, p) => {
console.log(' [antiCrash] :: Unhandled Rejection/Catch');
console.log(reason, p);
});
process.on('uncaughtException', (err, origin) => {
console.log(' [antiCrash] :: Uncaught Exception/Catch');
console.log(err, origin);
})
process.on('uncaughtExceptionMonitor', (err, origin) => {
console.log(' [antiCrash] :: Uncaught Exception/Catch (MONITOR)');
console.log(err, origin);
});