fbp-language 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,7 +23,7 @@ Puis, dans le `package.json` de ton projet :
23
23
  "start": "fbp run lancer"
24
24
  },
25
25
  "dependencies": {
26
- "fbp-language": "^1.1.1"
26
+ "fbp-language": "^1.1.3"
27
27
  }
28
28
  }
29
29
  ```
@@ -104,17 +104,20 @@ Dans `scripts/main.fbp` :
104
104
 
105
105
  ```fbp
106
106
  discordConnexion()
107
- discordCommande("!ping", "Pong !")
108
- discordCommande("!bonjour", "Salut !")
107
+ discordCommande("ping", "Pong !")
108
+ discordCommande("bonjour", "Salut !")
109
+ discordCommandeEmbed("serveur-info", "Informations du serveur", "Voici les informations disponibles.", "#5865F2")
110
+ discordChamp("serveur-info", "Nom", "Mon serveur")
111
+ discordChamp("serveur-info", "Membres", "42")
109
112
  ```
110
113
 
111
- Associe `lancer` à `scripts/main.fbp` dans `commandes.fbp`, puis exécute `npx fbp run lancer`. Dans le portail développeur Discord, active **Message Content Intent**.
114
+ Associe `lancer` à `scripts/main.fbp` dans `commandes.fbp`, puis exécute `npx fbp run lancer`. Dans le portail développeur Discord, vérifie que le bot est invité sur ton serveur avec les scopes `bot` et `applications.commands`. Seul l'intent serveur (`Guilds`) est nécessaire.
112
115
 
113
- Les commandes texte exactes sont prises en charge pour l'instant. Les commandes slash ne le sont pas encore.
116
+ FBP enregistre les commandes déclarées comme commandes slash globales de l'application Discord. Leur apparition peut prendre un peu de temps après le premier enregistrement. Les noms doivent contenir de 1 à 32 caractères parmi les lettres minuscules, chiffres, tirets et underscores.
114
117
 
115
118
  ## Limites actuelles
116
119
 
117
- FBP est encore un interpréteur de base. Les paramètres de fonction, les conditions, les boucles, les valeurs de retour et les expressions arithmétiques ne sont pas encore implémentés. La connexion Discord utilise un token stocké dans `.env`. Les commandes slash, les paramètres et les conditions ne sont pas encore implémentés.
120
+ FBP est encore un interpréteur de base. Les paramètres de fonction, les conditions, les boucles, les valeurs de retour et les expressions arithmétiques ne sont pas encore implémentés. La connexion Discord utilise un token stocké dans `.env`. Les commandes slash peuvent répondre avec un texte fixe ou un embed défini avec `discordCommandeEmbed(nom, titre, description, couleur)`. Ajoute des champs avec `discordChamp(nomCommande, nomChamp, valeur)`. Les valeurs des embeds sont actuellement fixes : la récupération dynamique des informations du serveur, les options, paramètres, permissions avancées et conditions ne sont pas encore implémentés.
118
121
 
119
122
  ## Développement du dépôt
120
123
 
package/dist/cli.js CHANGED
@@ -8,7 +8,7 @@ const node_fs_1 = __importDefault(require("node:fs"));
8
8
  const node_path_1 = __importDefault(require("node:path"));
9
9
  const compiler_1 = require("./compiler");
10
10
  const errors_1 = require("./errors");
11
- const VERSION = "1.1.1";
11
+ const VERSION = "1.1.3";
12
12
  function help() {
13
13
  console.log(`FBP — French Bot Programming
14
14
 
package/dist/compiler.js CHANGED
@@ -184,15 +184,55 @@ class FbpCompiler {
184
184
  const token = process.env.DISCORD_TOKEN;
185
185
  if (!token)
186
186
  throw new errors_1.FbpError("Token Discord manquant. Ajoute DISCORD_TOKEN dans le fichier .env.", undefined, line);
187
- const client = new discord_js_1.Client({ intents: [discord_js_1.GatewayIntentBits.Guilds, discord_js_1.GatewayIntentBits.GuildMessages, discord_js_1.GatewayIntentBits.MessageContent] });
187
+ const client = new discord_js_1.Client({ intents: [discord_js_1.GatewayIntentBits.Guilds] });
188
188
  this.discordClient = client;
189
- client.once("ready", () => this.output("Connecté à Discord en tant que " + (client.user?.tag ?? "bot") + "."));
190
- client.on("messageCreate", (message) => {
191
- if (message.author.bot || !message.guild)
189
+ client.once("ready", async () => {
190
+ try {
191
+ const commands = [...this.discordCommands.entries()].map(([commandName]) => new discord_js_1.SlashCommandBuilder()
192
+ .setName(commandName)
193
+ .setDescription(`Commande ${commandName} du bot FBP`)
194
+ .toJSON());
195
+ const manager = client.application?.commands;
196
+ if (!manager)
197
+ throw new Error("L'application Discord n'est pas disponible.");
198
+ const existing = await manager.fetch();
199
+ for (const command of commands) {
200
+ const registered = existing.find(item => item.name === command.name);
201
+ if (registered)
202
+ await manager.edit(registered.id, command);
203
+ else
204
+ await manager.create(command);
205
+ }
206
+ this.output("Connecté à Discord en tant que " + (client.user?.tag ?? "bot") + ".");
207
+ this.output("Commandes slash FBP enregistrées : " + commands.length + ".");
208
+ }
209
+ catch (error) {
210
+ this.output("Enregistrement des commandes slash impossible : " + (error instanceof Error ? error.message : String(error)));
211
+ }
212
+ });
213
+ client.on("interactionCreate", async (interaction) => {
214
+ if (!interaction.isChatInputCommand())
215
+ return;
216
+ const response = this.discordCommands.get(interaction.commandName);
217
+ if (response === undefined)
192
218
  return;
193
- const response = this.discordCommands.get(message.content);
194
- if (response !== undefined)
195
- void message.reply(response).catch((error) => this.output("Erreur Discord : " + (error instanceof Error ? error.message : String(error))));
219
+ try {
220
+ if (response.type === "text") {
221
+ await interaction.reply(response.content);
222
+ }
223
+ else {
224
+ const embed = new discord_js_1.EmbedBuilder()
225
+ .setTitle(response.title)
226
+ .setDescription(response.description)
227
+ .setColor(Number.parseInt(response.color.slice(1), 16));
228
+ if (response.fields.length)
229
+ embed.addFields(response.fields.map(field => ({ name: field.name, value: field.value, inline: false })));
230
+ await interaction.reply({ embeds: [embed] });
231
+ }
232
+ }
233
+ catch (error) {
234
+ this.output("Erreur Discord : " + (error instanceof Error ? error.message : String(error)));
235
+ }
196
236
  });
197
237
  void client.login(token).catch((error) => this.output("Connexion Discord impossible : " + (error instanceof Error ? error.message : String(error))));
198
238
  return;
@@ -200,9 +240,43 @@ class FbpCompiler {
200
240
  if (name === "discordCommande") {
201
241
  if (!this.discordClient)
202
242
  throw new errors_1.FbpError("Appelle discordConnexion() avant de déclarer une commande.", undefined, line);
203
- if (values.length !== 2 || typeof values[0] !== "string" || typeof values[1] !== "string")
204
- throw new errors_1.FbpError("Utilisation : discordCommande(\"!ping\", \"Pong !\").", undefined, line);
205
- this.discordCommands.set(values[0], values[1]);
243
+ if (values.length !== 2 || typeof values[0] !== "string" || typeof values[1] !== "string") {
244
+ throw new errors_1.FbpError("Utilisation : discordCommande(\"ping\", \"Pong !\").", undefined, line);
245
+ }
246
+ const commandName = values[0];
247
+ if (!/^[a-z0-9_-]{1,32}$/.test(commandName)) {
248
+ throw new errors_1.FbpError("Le nom d'une commande slash doit contenir 1 à 32 caractères : lettres minuscules, chiffres, tirets ou underscores.", undefined, line);
249
+ }
250
+ this.discordCommands.set(commandName, { type: "text", content: values[1] });
251
+ return;
252
+ }
253
+ if (name === "discordCommandeEmbed") {
254
+ if (!this.discordClient)
255
+ throw new errors_1.FbpError("Appelle discordConnexion() avant de déclarer une commande.", undefined, line);
256
+ if (values.length !== 4 || values.some(value => typeof value !== "string")) {
257
+ throw new errors_1.FbpError("Utilisation : discordCommandeEmbed(nom, titre, description, couleur).", undefined, line);
258
+ }
259
+ const [commandName, title, description, color] = values;
260
+ if (!/^[a-z0-9_-]{1,32}$/.test(commandName))
261
+ throw new errors_1.FbpError("Le nom d'une commande slash doit contenir 1 à 32 caractères : lettres minuscules, chiffres, tirets ou underscores.", undefined, line);
262
+ if (!/^#[0-9a-fA-F]{6}$/.test(color))
263
+ throw new errors_1.FbpError("La couleur d'un embed doit être au format hexadécimal, par exemple #5865F2.", undefined, line);
264
+ this.discordCommands.set(commandName, { type: "embed", title, description, color, fields: [] });
265
+ return;
266
+ }
267
+ if (name === "discordChamp") {
268
+ if (values.length !== 3 || values.some(value => typeof value !== "string")) {
269
+ throw new errors_1.FbpError("Utilisation : discordChamp(nomCommande, nomChamp, valeur).", undefined, line);
270
+ }
271
+ const [commandName, fieldName, fieldValue] = values;
272
+ const command = this.discordCommands.get(commandName);
273
+ if (!command || command.type !== "embed")
274
+ throw new errors_1.FbpError("La commande « " + commandName + " » n'est pas un embed déclaré.", undefined, line);
275
+ if (command.fields.length >= 25)
276
+ throw new errors_1.FbpError("Un embed Discord ne peut pas contenir plus de 25 champs.", undefined, line);
277
+ if (!fieldName.trim() || !fieldValue.trim() || fieldName.length > 256 || fieldValue.length > 1024)
278
+ throw new errors_1.FbpError("Un champ doit avoir un nom (1 à 256 caractères) et une valeur (1 à 1024 caractères).", undefined, line);
279
+ command.fields.push({ name: fieldName, value: fieldValue });
206
280
  return;
207
281
  }
208
282
  if (name === "ping") {
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "fbp-language",
3
3
  "displayName": "FBP - French Bot Programming",
4
- "description": "Langage FBP avec prise en charge des bots Discord.",
5
- "version": "1.1.1",
4
+ "description": "Langage FBP avec prise en charge des commandes slash et des embeds Discord.",
5
+ "version": "1.1.3",
6
6
  "type": "commonjs",
7
7
  "main": "dist/compiler.js",
8
8
  "bin": {
@@ -25,7 +25,10 @@
25
25
  "language",
26
26
  "interpreter",
27
27
  "french",
28
- "cli"
28
+ "cli",
29
+ "discord",
30
+ "slash-commands",
31
+ "bot"
29
32
  ],
30
33
  "scripts": {
31
34
  "build": "tsc -p tsconfig.json",