Questa guida costruisce un bot con un comando slash, /ping. Prima ti servono un'applicazione Discord e un token del bot: Come creare un bot Discord e ottenere il suo token.
I file
Crea questi file sul tuo computer.
package.json
{
"name": "my-bot",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"discord.js": "^14.16.0"
}
}
index.js
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');
const token = process.env.DISCORD_TOKEN;
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [new SlashCommandBuilder().setName('ping').setDescription('Replies with pong')];
client.once('ready', async () => {
const rest = new REST({ version: '10' }).setToken(token);
await rest.put(Routes.applicationCommands(client.user.id), { body: commands.map(c => c.toJSON()) });
console.log(`Logged in as ${client.user.tag}`);
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') await interaction.reply('pong');
});
client.login(token);
Al bot serve solo l'intent Guilds, quindi non devi attivare nessun intent privilegiato.
Avvialo su PlayNHost
- Apri il tuo server bot nel pannello. Un nuovo server bot parte come Node.js Bot.
- In Files, carica
index.jsepackage.json. Non caricarenode_modules: i pacchetti li installa il server per te. - In Impostazioni, Impostazioni di gioco, controlla che Main file sia
index.jse incolla il token in Discord bot token. - Apri Console e avvia il server. Il primo avvio installa discord.js, quindi richiede un po' più di tempo. Quando vedi
Logged in as ..., il bot è online. - In Discord, scrivi
/ping. Se il comando non compare, vedi Bot Discord online ma non risponde.
Prossimi passi
- Tieni i segreti nel campo del pannello, non nel file: Come tenere al sicuro il token del tuo bot Discord.
- Salva i dati con un database: Come usare un database con il tuo bot Discord.
- Distribuisci da GitHub e aggiorna a ogni riavvio: Come distribuire e aggiornare il tuo bot Discord da GitHub.





