Un bot ticket dà ai membri un pulsante. Premendolo si apre un canale privato che possono vedere solo quel membro e il tuo staff. Questa guida costruisce il nucleo di un bot così. Prima ti serve un token del bot: Come creare un bot Discord e ottenere il suo token.
Cosa ti serve
- Solo l'intent
Guilds. Non serve nessun intent privilegiato. - Permessi per il bot: Manage Channels, View Channels, Send Messages.
- L'ID del tuo ruolo staff. Attiva Modalità sviluppatore in Discord, clicca con il tasto destro sul ruolo e scegli Copia ID ruolo.
Il codice
index.js
const {
Client, GatewayIntentBits, ChannelType, PermissionFlagsBits,
ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder, REST, Routes,
} = require('discord.js');
const STAFF_ROLE_ID = process.env.STAFF_ROLE_ID;
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', async () => {
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
const cmd = new SlashCommandBuilder().setName('ticketpanel').setDescription('Post the ticket button')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);
await rest.put(Routes.applicationCommands(client.user.id), { body: [cmd.toJSON()] });
console.log(`Logged in as ${client.user.tag}`);
});
client.on('interactionCreate', async (i) => {
if (i.isChatInputCommand() && i.commandName === 'ticketpanel') {
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId('open_ticket').setLabel('Open a ticket').setStyle(ButtonStyle.Primary));
await i.channel.send({ content: 'Need help? Press the button to open a private ticket.', components: [row] });
return i.reply({ content: 'Posted.', ephemeral: true });
}
if (i.isButton() && i.customId === 'open_ticket') {
const channel = await i.guild.channels.create({
name: `ticket-${i.user.username}`,
type: ChannelType.GuildText,
permissionOverwrites: [
{ id: i.guild.roles.everyone.id, deny: [PermissionFlagsBits.ViewChannel] },
{ id: i.user.id, allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.SendMessages] },
{ id: STAFF_ROLE_ID, allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.SendMessages] },
{ id: client.user.id, allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.SendMessages, PermissionFlagsBits.ManageChannels] },
],
});
await channel.send(`Hello ${i.user}, a member of staff will be with you soon.`);
return i.reply({ content: `Your ticket: ${channel}`, ephemeral: true });
}
});
client.login(process.env.DISCORD_TOKEN);
Aggiungi "discord.js": "^14.16.0" a package.json. L'ID del ruolo staff viene letto da STAFF_ROLE_ID: mettilo in un file .env e aggiungi il pacchetto dotenv, oppure scrivi l'ID nel codice mentre fai i test.
Avvialo
Carica i file sul tuo server bot e avvialo: Come ospitare un bot Discord Node.js. Nel tuo server scrivi /ticketpanel nel canale in cui i membri devono aprire i ticket.
Prossimi passi
- Un pulsante Close ticket che elimina il canale dopo aver salvato una trascrizione.
- Limita ogni membro a un solo ticket aperto controllando se esiste già un canale con quel nome.
- Salva i ticket in un database: Come usare un database con il tuo bot Discord.





