mirror of
https://github.com/arthur-pbty/LazyBot.git
synced 2026-06-03 23:36:37 +02:00
finish of level system
This commit is contained in:
@@ -12,7 +12,7 @@ Bot Discord configurable via un dashboard web pour gérer :
|
|||||||
|
|
||||||
1. Cloner le projet :
|
1. Cloner le projet :
|
||||||
```bash
|
```bash
|
||||||
git clone <URL_DU_REPO>
|
git clone https://github.com/arthur-pbty/LazyBot.git
|
||||||
cd mon-bot-discord
|
cd mon-bot-discord
|
||||||
````
|
````
|
||||||
|
|
||||||
|
|||||||
+185
-7
@@ -1,5 +1,4 @@
|
|||||||
const loadSlashCommands = require('./slash_commands.js');
|
const loadSlashCommands = require('./slash_commands.js');
|
||||||
loadSlashCommands();
|
|
||||||
|
|
||||||
const db = require("./db");
|
const db = require("./db");
|
||||||
|
|
||||||
@@ -8,8 +7,9 @@ const e = require('express');
|
|||||||
|
|
||||||
const client = new Client({ intents: Object.values(GatewayIntentBits) });
|
const client = new Client({ intents: Object.values(GatewayIntentBits) });
|
||||||
|
|
||||||
client.once(Events.ClientReady, () => {
|
client.once(Events.ClientReady, async () => {
|
||||||
console.log(`Bot connecté en tant que ${client.user.tag}`);
|
console.log(`Bot connecté en tant que ${client.user.tag}`);
|
||||||
|
await loadSlashCommands(client);
|
||||||
client.user.setActivity("LazyBot à votre service !", { type: ActivityType.Custom });
|
client.user.setActivity("LazyBot à votre service !", { type: ActivityType.Custom });
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -20,6 +20,52 @@ client.on(Events.InteractionCreate, async interaction => {
|
|||||||
if (interaction.commandName === 'ping') {
|
if (interaction.commandName === 'ping') {
|
||||||
await interaction.reply('Pong!');
|
await interaction.reply('Pong!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (interaction.commandName === 'level') {
|
||||||
|
const guildId = interaction.guild.id;
|
||||||
|
const userId = interaction.user.id;
|
||||||
|
|
||||||
|
db.get(
|
||||||
|
`SELECT xp, level FROM user_levels WHERE guild_id = ? AND user_id = ?`,
|
||||||
|
[guildId, userId],
|
||||||
|
(err, row) => {
|
||||||
|
if (err) {
|
||||||
|
console.error("DB error fetching user level", err);
|
||||||
|
return interaction.reply("Une erreur est survenue en récupérant votre niveau.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!row) {
|
||||||
|
return interaction.reply("Vous n'avez pas encore de niveau dans ce serveur.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return interaction.reply(`Vous êtes au niveau ${row.level} avec ${row.xp} XP.`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else if (interaction.commandName === 'leveltop') {
|
||||||
|
const guildId = interaction.guild.id;
|
||||||
|
|
||||||
|
db.all(
|
||||||
|
`SELECT user_id, xp, level FROM user_levels WHERE guild_id = ? ORDER BY level DESC, xp DESC LIMIT 10`,
|
||||||
|
[guildId],
|
||||||
|
(err, rows) => {
|
||||||
|
if (err) {
|
||||||
|
console.error("DB error fetching level top", err);
|
||||||
|
return interaction.reply("Une erreur est survenue en récupérant le classement des niveaux.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rows.length === 0) {
|
||||||
|
return interaction.reply("Aucun utilisateur n'a encore de niveau dans ce serveur.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let replyMessage = "🏆 **Top 10 des niveaux :**\n";
|
||||||
|
rows.forEach((row, index) => {
|
||||||
|
replyMessage += `${index + 1}. <@${row.user_id}> - Niveau ${row.level} (${row.xp} XP)\n`;
|
||||||
|
});
|
||||||
|
|
||||||
|
return interaction.reply(replyMessage);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@@ -198,18 +244,17 @@ client.on(Events.MessageCreate, message => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Level up logic based on xp_courbe_type and multiplier goes here
|
// Level up logic based on xp_courbe_type and multiplier goes here
|
||||||
const firstLevelXp = 100; // Example base XP for first level ----------------------------------------
|
|
||||||
const multiplier = row.multiplier_courbe_for_level;
|
const multiplier = row.multiplier_courbe_for_level;
|
||||||
let fonction_courbe;
|
let fonction_courbe;
|
||||||
|
|
||||||
if (row.xp_courbe_type === "constante") {
|
if (row.xp_courbe_type === "constante") {
|
||||||
fonction_courbe = (level) => firstLevelXp * multiplier;
|
fonction_courbe = (level) => multiplier;
|
||||||
} else if (row.xp_courbe_type === "linear") {
|
} else if (row.xp_courbe_type === "linear") {
|
||||||
fonction_courbe = (level) => firstLevelXp * (level) * multiplier;
|
fonction_courbe = (level) => (level) * multiplier;
|
||||||
} else if (row.xp_courbe_type === "quadratic") {
|
} else if (row.xp_courbe_type === "quadratic") {
|
||||||
fonction_courbe = (level) => firstLevelXp * (level) * (level) * multiplier;
|
fonction_courbe = (level) => (level) * (level) * multiplier;
|
||||||
} else if (row.xp_courbe_type === "exponential") {
|
} else if (row.xp_courbe_type === "exponential") {
|
||||||
fonction_courbe = (level) => firstLevelXp * Math.pow(2, (level - 1)) * multiplier;
|
fonction_courbe = (level) => Math.pow(2, (level - 1)) * multiplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
let xpForNextLevel = fonction_courbe(newLevel);
|
let xpForNextLevel = fonction_courbe(newLevel);
|
||||||
@@ -249,6 +294,139 @@ client.on(Events.MessageCreate, message => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
// vérification des membres vocaux pour leur faire gagner de l'xp
|
||||||
|
client.guilds.cache.forEach(guild => {
|
||||||
|
guild.members.cache.forEach(member => {
|
||||||
|
if (member.user.bot) return;
|
||||||
|
|
||||||
|
const voiceState = member.voice;
|
||||||
|
if (!voiceState.channelId) return;
|
||||||
|
|
||||||
|
const guildId = guild.id;
|
||||||
|
|
||||||
|
db.get(
|
||||||
|
`SELECT
|
||||||
|
enabled,
|
||||||
|
level_announcements_enabled,
|
||||||
|
level_announcements_channel_id,
|
||||||
|
level_announcements_message,
|
||||||
|
xp_courbe_type,
|
||||||
|
multiplier_courbe_for_level,
|
||||||
|
level_annoncement_every_level,
|
||||||
|
level_max,
|
||||||
|
role_with_without_type,
|
||||||
|
role_with_without_xp,
|
||||||
|
salon_with_without_type,
|
||||||
|
salon_with_without_xp,
|
||||||
|
gain_xp_on_voice,
|
||||||
|
gain_voice_xp_lower_bound,
|
||||||
|
gain_voice_xp_upper_bound
|
||||||
|
FROM levels_config WHERE guild_id = ?`,
|
||||||
|
[guildId],
|
||||||
|
(err, row) => {
|
||||||
|
if (err || !row || !row.enabled || !row.gain_xp_on_voice) return;
|
||||||
|
if (row.role_with_without_type === "with") {
|
||||||
|
const userRoles = member.roles.cache;
|
||||||
|
const requiredRoles = JSON.parse(row.role_with_without_xp || "[]");
|
||||||
|
if (!requiredRoles.some(roleId => userRoles.has(roleId))) {
|
||||||
|
return; // User has an excluded role
|
||||||
|
}
|
||||||
|
} else if (row.role_with_without_type === "without") {
|
||||||
|
const userRoles = member.roles.cache;
|
||||||
|
const excludedRoles = JSON.parse(row.role_with_without_xp || "[]");
|
||||||
|
if (excludedRoles.some(roleId => userRoles.has(roleId))) {
|
||||||
|
return; // User does not have any of the required roles
|
||||||
|
}
|
||||||
|
} else if (row.salon_with_without_type === "with") {
|
||||||
|
const channelId = voiceState.channelId;
|
||||||
|
const requiredChannels = JSON.parse(row.salon_with_without_xp || "[]");
|
||||||
|
if (!requiredChannels.includes(channelId)) {
|
||||||
|
return; // Not in a required channel
|
||||||
|
}
|
||||||
|
} else if (row.salon_with_without_type === "without") {
|
||||||
|
const channelId = voiceState.channelId;
|
||||||
|
const excludedChannels = JSON.parse(row.salon_with_without_xp || "[]");
|
||||||
|
if (excludedChannels.includes(channelId)) {
|
||||||
|
return; // In an excluded channel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const minXp = row.gain_voice_xp_lower_bound;
|
||||||
|
const maxXp = row.gain_voice_xp_upper_bound;
|
||||||
|
const xpToAdd = Math.floor(Math.random() * (maxXp - minXp + 1)) + minXp;
|
||||||
|
|
||||||
|
db.get(
|
||||||
|
`SELECT xp, level FROM user_levels WHERE guild_id = ? AND user_id = ?`,
|
||||||
|
[guildId, member.id],
|
||||||
|
(err, userRow) => {
|
||||||
|
if (err) return;
|
||||||
|
|
||||||
|
let newXp;
|
||||||
|
let newLevel;
|
||||||
|
|
||||||
|
if (userRow) {
|
||||||
|
newXp = userRow.xp + xpToAdd;
|
||||||
|
newLevel = userRow.level;
|
||||||
|
} else {
|
||||||
|
newXp = xpToAdd;
|
||||||
|
newLevel = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Level up logic
|
||||||
|
const multiplier = row.multiplier_courbe_for_level;
|
||||||
|
let fonction_courbe;
|
||||||
|
|
||||||
|
if (row.xp_courbe_type === "constante") {
|
||||||
|
fonction_courbe = (level) => multiplier;
|
||||||
|
} else if (row.xp_courbe_type === "linear") {
|
||||||
|
fonction_courbe = (level) => (level) * multiplier;
|
||||||
|
} else if (row.xp_courbe_type === "quadratic") {
|
||||||
|
fonction_courbe = (level) => (level) * (level) * multiplier;
|
||||||
|
} else if (row.xp_courbe_type === "exponential") {
|
||||||
|
fonction_courbe = (level) => Math.pow(2, (level - 1)) * multiplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
let xpForNextLevel = fonction_courbe(newLevel);
|
||||||
|
while (newXp >= xpForNextLevel && (row.level_max === 0 || newLevel < row.level_max)) {
|
||||||
|
newXp -= xpForNextLevel;
|
||||||
|
newLevel += 1;
|
||||||
|
xpForNextLevel = fonction_courbe(newLevel);
|
||||||
|
|
||||||
|
// Announce level up if enabled and meets the criteria
|
||||||
|
if (row.level_announcements_enabled && (newLevel % row.level_annoncement_every_level === 0)) {
|
||||||
|
const channel = guild.channels.cache.get(row.level_announcements_channel_id);
|
||||||
|
console.log("Channel for level announcement:", channel);
|
||||||
|
if (channel) {
|
||||||
|
let announcementMsg = row.level_announcements_message;
|
||||||
|
announcementMsg = announcementMsg
|
||||||
|
.replace("{user}", member.user.username)
|
||||||
|
.replace("{mention}", `<@${member.id}>`)
|
||||||
|
.replace("{level}", newLevel)
|
||||||
|
.replace("{level-xp}", xpForNextLevel);
|
||||||
|
channel.send(announcementMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
db.run(
|
||||||
|
`INSERT INTO user_levels (guild_id, user_id, xp, level)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
ON CONFLICT(guild_id, user_id) DO UPDATE SET
|
||||||
|
xp = excluded.xp,
|
||||||
|
level = excluded.level`,
|
||||||
|
[guildId, member.id, newXp, newLevel]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, 60 * 1000); // Toutes les minutes
|
||||||
|
|
||||||
|
|
||||||
client.login(process.env.BOT_TOKEN);
|
client.login(process.env.BOT_TOKEN);
|
||||||
|
|
||||||
module.exports = client;
|
module.exports = client;
|
||||||
|
|||||||
@@ -183,9 +183,9 @@
|
|||||||
</label>
|
</label>
|
||||||
|
|
||||||
<label>
|
<label>
|
||||||
Multiplicateur de points d'expérience par niveau :
|
Nombre d'expérience du premier niveau :
|
||||||
<br />
|
<br />
|
||||||
<input type="number" id="level-xp-multiplier" min="1" value="1" />
|
<input type="number" id="level-xp-multiplier" min="1" value="500" />
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
<label>
|
<label>
|
||||||
|
|||||||
+4
-1
@@ -1,5 +1,6 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
const loadSlashCommands = require("../slash_commands");
|
||||||
|
|
||||||
module.exports = (app, db, client) => {
|
module.exports = (app, db, client) => {
|
||||||
|
|
||||||
@@ -371,7 +372,7 @@ module.exports = (app, db, client) => {
|
|||||||
levelAnnouncementsChannelId: null,
|
levelAnnouncementsChannelId: null,
|
||||||
levelAnnouncementsMessage: "",
|
levelAnnouncementsMessage: "",
|
||||||
xpCourbeType: "exponential",
|
xpCourbeType: "exponential",
|
||||||
multiplierCourbeForLevel: 1,
|
multiplierCourbeForLevel: 500,
|
||||||
levelAnnouncementEveryLevel: 1,
|
levelAnnouncementEveryLevel: 1,
|
||||||
levelMax: 0,
|
levelMax: 0,
|
||||||
roleWithWithoutType: "with",
|
roleWithWithoutType: "with",
|
||||||
@@ -540,6 +541,8 @@ module.exports = (app, db, client) => {
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).json({ success: false });
|
return res.status(500).json({ success: false });
|
||||||
}
|
}
|
||||||
|
// Reload slash commands for this guild
|
||||||
|
loadSlashCommands(client, guildId);
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
+71
-17
@@ -1,28 +1,82 @@
|
|||||||
const { REST, Routes } = require('discord.js');
|
const { REST, Routes } = require('discord.js');
|
||||||
|
const db = require('./db');
|
||||||
|
|
||||||
module.exports = async () => {
|
module.exports = async (client, guildId = null) => {
|
||||||
const TOKEN = process.env.BOT_TOKEN;
|
const TOKEN = process.env.BOT_TOKEN;
|
||||||
const CLIENT_ID = process.env.CLIENT_ID;
|
const CLIENT_ID = process.env.CLIENT_ID;
|
||||||
|
|
||||||
const commands = [
|
|
||||||
{
|
|
||||||
name: 'ping',
|
|
||||||
description: 'Replies with Pong!',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const rest = new REST({ version: '10' }).setToken(TOKEN);
|
const rest = new REST({ version: '10' }).setToken(TOKEN);
|
||||||
|
|
||||||
try {
|
/* =========================
|
||||||
console.log('Started refreshing application (/) commands.');
|
1️⃣ COMMANDES GLOBALES
|
||||||
|
(uniquement si pas de guildId)
|
||||||
|
========================= */
|
||||||
|
|
||||||
await rest.put(
|
if (!guildId) {
|
||||||
Routes.applicationCommands(CLIENT_ID),
|
const globalCommands = [
|
||||||
{ body: commands }
|
{
|
||||||
|
name: 'ping',
|
||||||
|
description: 'Replies with Pong!',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log('Refreshing GLOBAL slash commands...');
|
||||||
|
await rest.put(
|
||||||
|
Routes.applicationCommands(CLIENT_ID),
|
||||||
|
{ body: globalCommands }
|
||||||
|
);
|
||||||
|
console.log('Global slash commands loaded');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Global commands error', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================
|
||||||
|
2️⃣ COMMANDES PAR SERVEUR
|
||||||
|
========================= */
|
||||||
|
|
||||||
|
const guildsToProcess = guildId
|
||||||
|
? [client.guilds.cache.get(guildId)]
|
||||||
|
: 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 = [];
|
||||||
|
|
||||||
|
if (row?.enabled) {
|
||||||
|
guildCommands.push(
|
||||||
|
{
|
||||||
|
name: 'level',
|
||||||
|
description: 'Check your level and XP',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'leveltop',
|
||||||
|
description: 'Show the top levels',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log('Successfully reloaded application (/) commands.');
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user