Files
bot.js
package.json
Code
2 160
bot.js
require('dotenv').config();
const {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
Client,
EmbedBuilder,
GatewayIntentBits,
REST,
Routes
} = require('discord.js');
const token = process.env.DISCORD_TOKEN;
const verificationRoleId = process.env.VERIFICATION_ROLE_ID;
const bannerUrl = process.env.VERIFICATION_BANNER_URL?.trim();
const verificationButtonId = 'verification-panel:verify';
if (!token || token === 'your_discord_bot_token_here') {
throw new Error('Set DISCORD_TOKEN in the .env file before starting the bot.');
}
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
function createVerificationPanel() {
const embed = new EmbedBuilder()
.setColor(0x5865F2)
.setTitle('Server Verification')
.setDescription([
'Welcome! Click the button below to verify your account and unlock the server.',
'',
'By verifying, you confirm that you agree to follow the server rules.'
].join('\n'))
.setFooter({ text: 'If the button does not work, please contact a server moderator.' });
if (bannerUrl) {
try {
new URL(bannerUrl);
embed.setImage(bannerUrl);
} catch {
console.warn('VERIFICATION_BANNER_URL is not a valid URL; sending the panel without a banner.');
}
}
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(verificationButtonId)
.setLabel('Verify')
.setStyle(ButtonStyle.Success)
.setEmoji('✅')
);
return { embeds: [embed], components: [row] };
}
async function registerCommands() {
const rest = new REST({ version: '10' }).setToken(token);
await rest.put(Routes.applicationCommands(client.application.id), {
body: [
{
name: 'verifcation',
description: 'Send the server verification panel'
},
{
name: 'verification',
description: 'Send the server verification panel'
}
]
});
}
client.once('clientReady', async () => {
try {
await registerCommands();
console.log(`Logged in as ${client.user.tag}`);
console.log('KODARI_READY');
} catch (error) {
console.error('Failed to register slash commands:', error);
process.exitCode = 1;
await client.destroy();
}
});
client.on('interactionCreate', async (interaction) => {
try {
if (interaction.isChatInputCommand()) {
if (interaction.commandName !== 'verifcation' && interaction.commandName !== 'verification') return;
if (!interaction.inGuild()) {
await interaction.reply({ content: 'The verification panel can only be sent in a server.', ephemeral: true });
return;
}
await interaction.channel.send(createVerificationPanel());
await interaction.reply({ content: 'Verification panel sent.', ephemeral: true });
return;
}
if (!interaction.isButton() || interaction.customId !== verificationButtonId) return;
if (!interaction.inGuild()) {
await interaction.reply({ content: 'Verification is only available in a server.', ephemeral: true });
return;
}
if (!verificationRoleId || verificationRoleId === 'your_verified_role_id_here') {
await interaction.reply({
content: 'Verification is not configured yet. Ask a server administrator to set VERIFICATION_ROLE_ID.',
ephemeral: true
});
return;
}
const member = await interaction.guild.members.fetch(interaction.user.id);
if (member.roles.cache.has(verificationRoleId)) {
await interaction.reply({ content: 'You are already verified. Welcome!', ephemeral: true });
return;
}
await member.roles.add(verificationRoleId, 'Member completed the verification panel');
await interaction.reply({ content: 'You are verified. Welcome to the server!', ephemeral: true });
} catch (error) {
console.error('Interaction failed:', error);
if (interaction.isRepliable() && !interaction.replied && !interaction.deferred) {
await interaction.reply({
content: 'Something went wrong. Please try again or contact a server moderator.',
ephemeral: true
}).catch(console.error);
} else if (interaction.isRepliable() && (interaction.replied || interaction.deferred)) {
await interaction.followUp({
content: 'Something went wrong. Please contact a server moderator.',
ephemeral: true
}).catch(console.error);
}
}
});
client.on('error', (error) => console.error('Discord client error:', error));
process.on('unhandledRejection', (error) => console.error('Unhandled promise rejection:', error));
client.login(token).catch((error) => {
console.error('Failed to log in to Discord:', error);
process.exitCode = 1;
});