mirror of
https://github.com/arthur-pbty/LazyBot.git
synced 2026-06-03 23:36:37 +02:00
123 lines
3.9 KiB
JavaScript
123 lines
3.9 KiB
JavaScript
const addCommand = require("../../fonctions/addCommand");
|
|
const { EmbedBuilder, PermissionFlagsBits } = require("discord.js");
|
|
const db = require("../../db");
|
|
|
|
module.exports = addCommand({
|
|
name: "counting-add",
|
|
description: "Ajoute un nombre au compteur actuel.",
|
|
aliases: ["countadd", "addcount"],
|
|
permissions: [PermissionFlagsBits.ManageGuild],
|
|
botOwnerOnly: false,
|
|
dm: false,
|
|
scope: "guild",
|
|
|
|
guildCondition: async (guildId) => {
|
|
return new Promise((resolve) => {
|
|
db.get(
|
|
"SELECT enabled FROM counting_config WHERE guild_id = ?",
|
|
[guildId],
|
|
(err, row) => {
|
|
if (err) {
|
|
console.error(`DB error in guildCondition for guild ${guildId}`, err);
|
|
return resolve(false);
|
|
}
|
|
resolve(!!row?.enabled);
|
|
}
|
|
);
|
|
});
|
|
},
|
|
|
|
slashOptions: [
|
|
{
|
|
type: "INTEGER",
|
|
name: "nombre",
|
|
description: "Le nombre à ajouter au compteur",
|
|
required: true,
|
|
},
|
|
],
|
|
|
|
executePrefix: async (client, message, args) => {
|
|
const number = parseInt(args[0], 10);
|
|
if (isNaN(number) || number <= 0) {
|
|
return message.reply({
|
|
embeds: [new EmbedBuilder().setColor(0xED4245).setDescription("❌ Veuillez spécifier un nombre valide (> 0).")]
|
|
});
|
|
}
|
|
|
|
await addCount(message.guild, number, message.author, message);
|
|
},
|
|
|
|
executeSlash: async (client, interaction) => {
|
|
const number = interaction.options.getInteger("nombre");
|
|
if (number <= 0) {
|
|
return interaction.reply({
|
|
embeds: [new EmbedBuilder().setColor(0xED4245).setDescription("❌ Le nombre doit être supérieur à 0.")],
|
|
ephemeral: true
|
|
});
|
|
}
|
|
|
|
await addCount(interaction.guild, number, interaction.user, interaction);
|
|
},
|
|
});
|
|
|
|
async function addCount(guild, amount, moderator, context) {
|
|
try {
|
|
const config = await db.getAsync(
|
|
"SELECT enabled, channel_id, current_count FROM counting_config WHERE guild_id = ?",
|
|
[guild.id]
|
|
);
|
|
|
|
if (!config || !config.enabled) {
|
|
const embed = new EmbedBuilder()
|
|
.setColor(0xED4245)
|
|
.setDescription("❌ Le système de comptage n'est pas activé sur ce serveur.");
|
|
return context.reply({ embeds: [embed], ephemeral: true });
|
|
}
|
|
|
|
const oldCount = config.current_count || 0;
|
|
const newCount = oldCount + amount;
|
|
|
|
// Mettre à jour le compteur
|
|
await new Promise((resolve, reject) => {
|
|
db.run(
|
|
"UPDATE counting_config SET current_count = ?, last_user_id = NULL WHERE guild_id = ?",
|
|
[newCount, guild.id],
|
|
(err) => {
|
|
if (err) reject(err);
|
|
else resolve();
|
|
}
|
|
);
|
|
});
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setColor(0x57F287)
|
|
.setTitle("🔢 Compteur augmenté")
|
|
.setDescription(`**+${amount}** ajouté au compteur !`)
|
|
.addFields(
|
|
{ name: "📊 Ancien", value: `${oldCount}`, inline: true },
|
|
{ name: "📊 Nouveau", value: `${newCount}`, inline: true },
|
|
{ name: "➡️ Prochain", value: `${newCount + 1}`, inline: true }
|
|
)
|
|
.setFooter({ text: `Modifié par ${moderator.username}`, iconURL: moderator.displayAvatarURL({ dynamic: true }) })
|
|
.setTimestamp();
|
|
|
|
await context.reply({ embeds: [embed] });
|
|
|
|
// Envoyer un message dans le salon de comptage
|
|
if (config.channel_id) {
|
|
const channel = guild.channels.cache.get(config.channel_id);
|
|
if (channel) {
|
|
const announceEmbed = new EmbedBuilder()
|
|
.setColor(0x57F287)
|
|
.setDescription(`🔢 Un administrateur a ajouté **+${amount}** au compteur !\n\n📊 Compteur actuel : **${newCount}**\n➡️ Le prochain nombre est **${newCount + 1}**`);
|
|
|
|
await channel.send({ embeds: [announceEmbed] });
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
const errorEmbed = new EmbedBuilder().setColor(0xED4245).setDescription("❌ Une erreur est survenue.");
|
|
await context.reply({ embeds: [errorEmbed], ephemeral: true });
|
|
}
|
|
}
|