Actualiser bot.js
This commit is contained in:
100
bot.js
100
bot.js
@ -1,36 +1,64 @@
|
|||||||
import { Client, GatewayIntentBits } from "discord.js";
|
import { Client, GatewayIntentBits } from "discord.js";
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
|
|
||||||
const client = new Client({
|
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
|
||||||
intents: [
|
const N8N_WEBHOOK = process.env.N8N_WEBHOOK;
|
||||||
GatewayIntentBits.Guilds,
|
|
||||||
GatewayIntentBits.GuildMessages,
|
if (!DISCORD_TOKEN) {
|
||||||
GatewayIntentBits.MessageContent
|
console.error("❌ DISCORD_TOKEN is missing. Set the env var DISCORD_TOKEN.");
|
||||||
]
|
process.exit(1);
|
||||||
});
|
}
|
||||||
|
if (!N8N_WEBHOOK) {
|
||||||
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
|
console.error("❌ N8N_WEBHOOK is missing. Set the env var N8N_WEBHOOK.");
|
||||||
const N8N_WEBHOOK = process.env.N8N_WEBHOOK;
|
process.exit(1);
|
||||||
|
}
|
||||||
client.on("messageCreate", async (message) => {
|
|
||||||
if (message.author.bot) return;
|
const client = new Client({
|
||||||
|
intents: [
|
||||||
try {
|
GatewayIntentBits.Guilds,
|
||||||
const response = await fetch(N8N_WEBHOOK, {
|
GatewayIntentBits.GuildMessages,
|
||||||
method: "POST",
|
GatewayIntentBits.MessageContent
|
||||||
headers: { "Content-Type": "application/json" },
|
]
|
||||||
body: JSON.stringify({ text: message.content, user: message.author.username })
|
});
|
||||||
});
|
|
||||||
|
client.once("ready", () => {
|
||||||
const data = await response.json();
|
console.log(`✅ Ready! Logged in as ${client.user.tag}`);
|
||||||
|
});
|
||||||
if (data.reply) {
|
|
||||||
await message.reply(data.reply);
|
client.on("messageCreate", async (message) => {
|
||||||
}
|
try {
|
||||||
} catch (err) {
|
if (message.author.bot) return;
|
||||||
console.error("Erreur:", err);
|
console.log(`📩 Message from ${message.author.tag} in ${message.guild ? message.guild.name : "DM"}: ${message.content}`);
|
||||||
message.reply("⚠️ Erreur avec le chatbot.");
|
|
||||||
}
|
const res = await fetch(N8N_WEBHOOK, {
|
||||||
});
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
client.login(DISCORD_TOKEN);
|
body: JSON.stringify({ text: message.content, user: message.author.username })
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await res.json().catch(() => null);
|
||||||
|
console.log("↩️ n8n response raw:", data);
|
||||||
|
|
||||||
|
if (data && data.reply) {
|
||||||
|
await message.reply(data.reply);
|
||||||
|
} else {
|
||||||
|
// fallback minimal log
|
||||||
|
console.log("⚠️ No 'reply' field in n8n response.");
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("🔴 Error in message handler:", err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("unhandledRejection", (r) => {
|
||||||
|
console.error("UnhandledRejection:", r);
|
||||||
|
});
|
||||||
|
process.on("uncaughtException", (err) => {
|
||||||
|
console.error("UncaughtException:", err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
client.login(DISCORD_TOKEN).catch(err => {
|
||||||
|
console.error("Failed to login to Discord:", err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user