Files
bot.ts
MT-2026-0002_uzupelnione.pdf
package.json
tsconfig.json
Code
4 115
bot.ts
import {
ActionRowBuilder,
Client,
GatewayIntentBits,
SlashCommandBuilder,
StringSelectMenuBuilder,
} from 'discord.js';
import * as dotenv from 'dotenv';
dotenv.config();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const command = new SlashCommandBuilder()
.setName('wybor')
.setDescription('Utwórz menu wyboru')
.addStringOption(option => option
.setName('tytul')
.setDescription('Tekst wyświetlany nad menu')
.setMaxLength(200)
.setRequired(true))
.addStringOption(option => option
.setName('opcje')
.setDescription('Opcje oddzielone przecinkami, np. Jabłko, Gruszka')
.setMaxLength(2000)
.setRequired(true));
client.once('clientReady', async readyClient => {
console.log('KODARI_READY');
try {
await readyClient.application.commands.set([command.toJSON()]);
console.log(`Zalogowano jako ${readyClient.user.tag}.`);
} catch (error) {
console.error('Nie udało się zarejestrować komendy:', error);
}
});
client.on('interactionCreate', async interaction => {
try {
if (interaction.isChatInputCommand() && interaction.commandName === 'wybor') {
const title = interaction.options.getString('tytul', true).trim();
const options = interaction.options.getString('opcje', true).split(',').map(option => option.trim());
if (!title || options.length < 1 || options.length > 25 || options.some(option => !option || option.length > 100)) {
await interaction.reply({ content: 'Podaj tytuł i od 1 do 25 niepustych opcji oddzielonych przecinkami. Każda opcja może mieć maksymalnie 100 znaków.', ephemeral: true });
return;
}
const menu = new StringSelectMenuBuilder()
.setCustomId('polskie_menu_wyboru')
.setPlaceholder('Wybierz opcję')
.addOptions(options.map((label, index) => ({ label, value: String(index) })));
const row = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(menu);
await interaction.reply({ content: title, components: [row], allowedMentions: { parse: [] } });
return;
}
if (interaction.isStringSelectMenu() && interaction.customId === 'polskie_menu_wyboru') {
const selected = interaction.component.options.find(option => option.value === interaction.values[0]);
await interaction.reply({ content: selected ? `Wybrano: ${selected.label}` : 'Nie znaleziono wybranej opcji.', ephemeral: true, allowedMentions: { parse: [] } });
}
} catch (error) {
console.error('Błąd obsługi interakcji:', error);
if (interaction.isRepliable()) {
try {
if (interaction.deferred || interaction.replied) {
await interaction.followUp({ content: 'Wystąpił błąd. Spróbuj ponownie.', ephemeral: true });
} else {
await interaction.reply({ content: 'Wystąpił błąd. Spróbuj ponownie.', ephemeral: true });
}
} catch (replyError) {
console.error('Nie udało się wysłać informacji o błędzie:', replyError);
}
}
}
});
async function start(): Promise<void> {
if (!process.env.DISCORD_TOKEN || process.env.DISCORD_TOKEN === 'your_discord_bot_token_here') {
throw new Error('Uzupełnij DISCORD_TOKEN w pliku .env.');
}
await client.login(process.env.DISCORD_TOKEN);
}
start().catch(error => {
console.error('Nie udało się uruchomić bota:', error);
process.exitCode = 1;
});