-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (97 loc) · 3.39 KB
/
index.js
File metadata and controls
111 lines (97 loc) · 3.39 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
const { Client, GatewayIntentBits, Collection, EmbedBuilder } = require('discord.js');
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token } = require('./config');
const mysql = require('mysql2/promise');
const config = require('./config');
let leaderboardMessageId = config.leaderboard.MessageId;
const leaderboardChannelId = config.leaderboard.ChannelId;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
]
});
client.commands = new Collection();
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
console.log('🔄 Loading commands...');
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
console.log('⏩ Loading : /' + command.data.name);
commands.push(command.data.toJSON());
}
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
await rest.put(
Routes.applicationGuildCommands(config.APPLICATION_ID_BOT, config.GUILD_ID),
{ body: commands },
);
console.log('✅ Commands successfully loaded!');
} catch (error) {
console.error(error);
}
})();
client.once('ready', () => {
console.log('🤖 Bot started and running!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'Error 1', ephemeral: true });
}
});
async function updateLeaderboard(client, channelId) {
const connection = await mysql.createConnection(config.dbConfig);
const [rows] = await connection.execute('SELECT * FROM k4ranks ORDER BY points DESC LIMIT 5');
await connection.end();
function formatName(name) {
if (name.length > 10) {
return name.substring(0, 10);
} else {
return name;
}
}
let leaderboardDescription = rows.map((row, index) => {
let prefix = "";
if (index === 0) prefix = "🥇";
else if (index === 1) prefix = "🥈";
else if (index === 2) prefix = "🥉";
else if (index <= 3) prefix = "📀";
else if (index <= 4) prefix = "💿";
return `${prefix} **${formatName(row.name)}** \n${row.points} points\n`;
}).join('\n');
const embed = new EmbedBuilder()
.setTitle('🏆 Leaderboard')
.setDescription(leaderboardDescription)
.setColor('#0099ff')
.setTimestamp();
const channel = await client.channels.fetch(channelId);
if (leaderboardMessageId) {
try {
const message = await channel.messages.fetch(leaderboardMessageId);
await message.edit({ embeds: [embed] });
} catch (error) {
console.error("Error 2", error);
}
} else {
const message = await channel.send({ embeds: [embed] });
leaderboardMessageId = message.id;
}
}
client.once('ready', async () => {
await updateLeaderboard(client, leaderboardChannelId);
setInterval(async () => {
await updateLeaderboard(client, leaderboardChannelId);
}, 1800000);
});
client.login(token);