Skip to content

Commit 67a54d2

Browse files
committed
fix(deploycommand): on start et on guild join
1 parent 0a965e7 commit 67a54d2

5 files changed

Lines changed: 81 additions & 53 deletions

File tree

worldboss-app/src/data/races.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const RACE_BONUSES = {
77
humain: { hpPct: 0.05, atkPct: 0.03, defPct: 0.03, spdPct: 0.03, critFlat: 0 },
88
elfe: { hpPct: 0, atkPct: 0.10, defPct: 0, spdPct: 0.10, critFlat: 8 },
99
nain: { hpPct: 0.20, atkPct: 0.05, defPct: 0.15, spdPct: 0, critFlat: 0 },
10-
orque: { hpPct: 0.08, atkPct: 0.15, defPct: 0, spdPct: 0, critFlat: 10 },
10+
orque: { hpPct: 0.08, atkPct: 0.15, defPct: 0, spdPct: 0, critFlat: 5 },
1111
halfelin: { hpPct: 0.10, atkPct: 0, defPct: 0.08, spdPct: 0.15, critFlat: 0 },
1212
};
1313

worldboss-app/src/deploy-commands.js

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,77 @@ require('dotenv').config();
44

55
const path = require('path');
66
const fs = require('fs');
7-
const { REST, Routes } = require('discord.js');
8-
9-
const commands = [];
10-
11-
const commandDirs = [
12-
path.join(__dirname, 'commands', 'player'),
13-
path.join(__dirname, 'commands', 'inventory'),
14-
path.join(__dirname, 'commands', 'dungeon'),
15-
path.join(__dirname, 'commands', 'admin'),
16-
];
17-
18-
for (const dir of commandDirs) {
19-
if (!fs.existsSync(dir)) continue;
20-
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.js'));
21-
for (const file of files) {
22-
const command = require(path.join(dir, file));
23-
if (command.data) {
24-
commands.push(command.data.toJSON());
25-
console.log(`[Deploy] Préparé: ${command.data.name}`);
7+
const { REST, Routes, Client, GatewayIntentBits } = require('discord.js');
8+
9+
function loadCommands() {
10+
const commands = [];
11+
const commandDirs = [
12+
path.join(__dirname, 'commands', 'player'),
13+
path.join(__dirname, 'commands', 'inventory'),
14+
path.join(__dirname, 'commands', 'dungeon'),
15+
path.join(__dirname, 'commands', 'admin'),
16+
];
17+
18+
for (const dir of commandDirs) {
19+
if (!fs.existsSync(dir)) continue;
20+
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.js'));
21+
for (const file of files) {
22+
const command = require(path.join(dir, file));
23+
if (command.data) {
24+
commands.push(command.data.toJSON());
25+
console.log(`[Deploy] Préparé: ${command.data.name}`);
26+
}
2627
}
2728
}
29+
return commands;
2830
}
2931

30-
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
31-
32-
// Guild IDs for instant deploy (development). Leave empty to deploy globally.
33-
const GUILD_IDS = (process.env.DEPLOY_GUILD_IDS || '').split(',').map((s) => s.trim()).filter(Boolean);
34-
35-
(async () => {
36-
try {
37-
if (GUILD_IDS.length > 0) {
38-
// Guild-specific deploy: instantaneous, perfect for development
39-
for (const guildId of GUILD_IDS) {
40-
console.log(`[Deploy] Publication sur le serveur ${guildId}...`);
41-
const data = await rest.put(
42-
Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, guildId),
43-
{ body: commands },
44-
);
45-
console.log(`[Deploy] ${data.length} commande(s) publiée(s) sur ${guildId}.`);
46-
}
47-
} else {
48-
// Global deploy: up to 1 hour to propagate — use for production
49-
console.log(`[Deploy] Publication globale de ${commands.length} commande(s)...`);
50-
const data = await rest.put(
51-
Routes.applicationCommands(process.env.DISCORD_CLIENT_ID),
52-
{ body: commands },
53-
);
54-
console.log(`[Deploy] ${data.length} commande(s) publiée(s) avec succès (propagation jusqu'à 1h).`);
55-
}
56-
} catch (error) {
57-
console.error('[Deploy] Erreur:', error);
58-
process.exit(1);
32+
async function deployCommands(client) {
33+
const commands = loadCommands();
34+
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
35+
const guildIds = client.guilds.cache.map((g) => g.id);
36+
37+
console.log(`[Deploy] Déploiement sur ${guildIds.length} serveur(s)...`);
38+
39+
for (const guildId of guildIds) {
40+
const data = await rest.put(
41+
Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, guildId),
42+
{ body: commands },
43+
);
44+
console.log(`[Deploy] ${data.length} commande(s) publiée(s) sur ${guildId}.`);
5945
}
60-
})();
46+
47+
console.log('[Deploy] Déploiement terminé sur tous les serveurs.');
48+
}
49+
50+
// Standalone mode: node src/deploy-commands.js
51+
if (require.main === module) {
52+
(async () => {
53+
try {
54+
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
55+
await new Promise((resolve, reject) => {
56+
client.once('ready', resolve);
57+
client.once('error', reject);
58+
client.login(process.env.DISCORD_TOKEN);
59+
});
60+
61+
await deployCommands(client);
62+
await client.destroy();
63+
} catch (error) {
64+
console.error('[Deploy] Erreur:', error);
65+
process.exit(1);
66+
}
67+
})();
68+
}
69+
70+
async function deployToGuild(guildId) {
71+
const commands = loadCommands();
72+
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
73+
const data = await rest.put(
74+
Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, guildId),
75+
{ body: commands },
76+
);
77+
console.log(`[Deploy] ${data.length} commande(s) publiée(s) sur ${guildId}.`);
78+
}
79+
80+
module.exports = { deployCommands, deployToGuild };

worldboss-app/src/events/guildCreate.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
initGuildChannels,
77
updateGuildChannels,
88
} = require('../services/guild.service');
9+
const { deployToGuild } = require('../deploy-commands');
910

1011
module.exports = {
1112
name: Events.GuildCreate,
@@ -15,6 +16,7 @@ module.exports = {
1516
console.log(`[Guild] Rejoint : ${guild.name} (${guild.id})`);
1617

1718
try {
19+
await deployToGuild(guild.id).catch((err) => console.error(`[Deploy] Erreur sur ${guild.name}:`, err.message));
1820
await ensureGuildInDb(guild);
1921
const channelIds = await initGuildChannels(guild);
2022
await updateGuildChannels(guild.id, channelIds);

worldboss-app/src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { Client, Collection, GatewayIntentBits } = require('discord.js');
88
const { redis } = require('./cache/redis');
99
const { startAuctionWorker } = require('./services/market.service');
1010
const { startMerchantWorker } = require('./services/merchant.service');
11+
const { deployCommands } = require('./deploy-commands');
1112

1213
// ── Discord client ───────────────────────────────────────────────────────────
1314
const client = new Client({
@@ -80,10 +81,15 @@ process.on('unhandledRejection', (err) => {
8081
console.error('[UnhandledRejection]', err?.message ?? err);
8182
});
8283

83-
client.login(token).then(() => {
84-
console.log('[Bot] Login en cours...');
84+
client.once('ready', async () => {
85+
console.log(`[Bot] Connecté en tant que ${client.user.tag}`);
86+
await deployCommands(client).catch((err) => console.error('[Deploy] Erreur au démarrage:', err.message));
8587
startAuctionWorker(client);
8688
startMerchantWorker(client);
89+
});
90+
91+
client.login(token).then(() => {
92+
console.log('[Bot] Login en cours...');
8793
}).catch((err) => {
8894
console.error('[Bot] Impossible de se connecter:', err.message);
8995
process.exit(1);

worldboss-app/src/services/player.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async function addXp(characterId, amount) {
125125
if (leveledUp) {
126126
const { AP_MAX } = require('./actionPoints.service');
127127
const { computeStats } = require('../utils/stats');
128-
const maxHp = computeStats({ level, rank }, character.loadout ?? {}).hp;
128+
const maxHp = computeStats({ level, rank, race: character.race, gender: character.gender }, character.loadout ?? {}).hp;
129129
updateData.hp = maxHp;
130130
updateData.hpUpdatedAt = new Date();
131131
updateData.actionPoints = AP_MAX;

0 commit comments

Comments
 (0)