Files
src
.gitignore
astro-ai-banner.svg
package.json
README.md
tsconfig.json
Code
6 335
src/bot.ts
import 'dotenv/config';
import {
Client,
Events,
GatewayIntentBits,
REST,
Routes,
SlashCommandBuilder
} from 'discord.js';
const token = process.env.DISCORD_TOKEN;
const clientId = process.env.CLIENT_ID;
const guildId = process.env.GUILD_ID;
if (!token || !clientId) {
console.error('Lipsesc DISCORD_TOKEN sau CLIENT_ID din fișierul .env.');
process.exit(1);
}
const commands = [
new SlashCommandBuilder()
.setName('ping')
.setDescription('Verifică timpul de răspuns al botului.')
.toJSON()
];
const client = new Client({
intents: [GatewayIntentBits.Guilds]
});
async function registerCommands(): Promise<void> {
const rest = new REST({ version: '10' }).setToken(token);
if (guildId) {
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands }
);
console.log(`Comenzile au fost înregistrate pe serverul ${guildId}.`);
return;
}
await rest.put(
Routes.applicationCommands(clientId),
{ body: commands }
);
console.log('Comenzile globale au fost înregistrate.');
}
client.once(Events.ClientReady, async (readyClient) => {
console.log(`Botul este conectat ca ${readyClient.user.tag}.`);
try {
await registerCommands();
} catch (error) {
console.error('Comenzile nu au putut fi înregistrate:', error);
}
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) {
return;
}
try {
if (interaction.commandName === 'ping') {
await interaction.reply(`Pong! Latența botului este de ${client.ws.ping}ms.`);
}
} catch (error) {
console.error('Eroare la procesarea interacțiunii:', error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'A apărut o eroare la procesarea comenzii.',
ephemeral: true
});
} else {
await interaction.reply({
content: 'A apărut o eroare la procesarea comenzii.',
ephemeral: true
});
}
}
});
client.on(Events.Error, (error) => {
console.error('Eroare Discord:', error);
});
process.on('unhandledRejection', (error) => {
console.error('Promise respinsă fără tratare:', error);
});
client.login(token).catch((error: unknown) => {
console.error('Conectarea botului a eșuat:', error);
process.exit(1);
});