mirror of
https://github.com/arthur-pbty/LazyBot.git
synced 2026-06-20 13:50:22 +02:00
finish organization event/command & add guild command in folder commands
This commit is contained in:
+39
-39
@@ -1,11 +1,10 @@
|
||||
const { REST, Routes } = require('discord.js');
|
||||
const db = require('./db');
|
||||
const { REST, Routes } = require("discord.js");
|
||||
|
||||
module.exports = async (client, guildId = null) => {
|
||||
module.exports = async function loadSlashCommands(client, guildId = null) {
|
||||
const TOKEN = process.env.BOT_TOKEN;
|
||||
const CLIENT_ID = process.env.CLIENT_ID;
|
||||
|
||||
const rest = new REST({ version: '10' }).setToken(TOKEN);
|
||||
const rest = new REST({ version: "10" }).setToken(TOKEN);
|
||||
|
||||
/* =========================
|
||||
1️⃣ COMMANDES GLOBALES
|
||||
@@ -14,19 +13,21 @@ module.exports = async (client, guildId = null) => {
|
||||
|
||||
if (!guildId) {
|
||||
const globalCommands = [];
|
||||
client.commands.forEach((command) => {
|
||||
|
||||
for (const command of client.commands.values()) {
|
||||
if (command.scope === "guild") continue;
|
||||
globalCommands.push(command.data.toJSON());
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('Refreshing GLOBAL slash commands...');
|
||||
console.log("Refreshing GLOBAL slash commands...");
|
||||
await rest.put(
|
||||
Routes.applicationCommands(client.user.id),
|
||||
Routes.applicationCommands(CLIENT_ID),
|
||||
{ body: globalCommands }
|
||||
);
|
||||
console.log('Global slash commands loaded');
|
||||
console.log("Global slash commands loaded");
|
||||
} catch (err) {
|
||||
console.error('Global commands error', err);
|
||||
console.error("Global commands error", err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,45 +37,44 @@ module.exports = async (client, guildId = null) => {
|
||||
|
||||
const guildsToProcess = guildId
|
||||
? [client.guilds.cache.get(guildId)]
|
||||
: client.guilds.cache.values();
|
||||
: [...client.guilds.cache.values()];
|
||||
|
||||
for (const guild of guildsToProcess) {
|
||||
if (!guild) continue;
|
||||
|
||||
db.get(
|
||||
"SELECT enabled FROM levels_config WHERE guild_id = ?",
|
||||
[guild.id],
|
||||
async (err, row) => {
|
||||
if (err) {
|
||||
console.error(`DB error ${guild.name}`, err);
|
||||
return;
|
||||
}
|
||||
const guildCommands = [];
|
||||
|
||||
const guildCommands = [];
|
||||
|
||||
if (row?.enabled) {
|
||||
guildCommands.push(
|
||||
{
|
||||
name: 'level',
|
||||
description: 'Check your level and XP',
|
||||
},
|
||||
{
|
||||
name: 'leveltop',
|
||||
description: 'Show the top levels',
|
||||
}
|
||||
);
|
||||
}
|
||||
for (const command of client.commands.values()) {
|
||||
if (command.scope === "global") continue;
|
||||
|
||||
if (command.guildCondition) {
|
||||
let conditionMet = false;
|
||||
try {
|
||||
await rest.put(
|
||||
Routes.applicationGuildCommands(client.user.id, guild.id),
|
||||
{ body: guildCommands }
|
||||
);
|
||||
console.log(`Guild commands updated for ${guild.name}`);
|
||||
conditionMet = await command.guildCondition(guild.id);
|
||||
} catch (err) {
|
||||
console.error(`Guild commands error ${guild.name}`, err);
|
||||
console.error(
|
||||
`Guild condition error for command ${command.name} in guild ${guild.name}`,
|
||||
err
|
||||
);
|
||||
}
|
||||
if (!conditionMet) continue;
|
||||
}
|
||||
|
||||
guildCommands.push(command.data.toJSON());
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Refreshing GUILD slash commands for ${guild.name} (${guildCommands.length})`
|
||||
);
|
||||
|
||||
try {
|
||||
await rest.put(
|
||||
Routes.applicationGuildCommands(CLIENT_ID, guild.id),
|
||||
{ body: guildCommands }
|
||||
);
|
||||
console.log(`Guild commands updated for ${guild.name}`);
|
||||
} catch (err) {
|
||||
console.error(`Guild commands error ${guild.name}`, err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user