const { Client, Events, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ActivityType, GatewayIntentBits } = require('discord.js'); // Defining the bot. const bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); // Bot prefix const prefix = `+`; // Turning on bot. bot.once(Events.ClientReady, bot => { // Setting the bot's custom status. bot.user.setPresence({ activities: [{ name: "some game", type: ActivityType.Playing }], status: `dnd` }); console.log(`${bot.user.username} is ready.`); }); // Creating commands bot.on(Events.MessageCreate, async (message) => { const { content, author } = message; if(!content.startsWith(prefix) || author.bot) return; // If the message does not starts with the prefix or if the command user is a bot, then the bot will not do anything. const args = content.slice(prefix.length).trim().split(/ +/); const command = args.shift(); // Defining command. // Ping command. if (command === "ping" || "Ping") { const wait = require('node:timers/promises').setTimeout; let a = new EmbedBuilder() .setDescription(":ping_pong: Pong! Calculating ping...") .setColor("NotQuiteBlack"); let s = await message.reply({ embeds: [a] }).then(async (m) => { const botLatency = m.createdTimestamp - message.createdTimestamp; let p = new EmbedBuilder() .setDescription(`API ping: **${bot.ws.ping} ms**\n\nBot latency: **${botLatency} ms**`) .setColor("Blue"); await wait(1000); await m.edit({ embeds: [p] }); }); // Button command. } else if(command === "button" || command === "Button") { let e = new EmbedBuilder() .setDescription("Click the button.") .setColor('#da70d6'); let button = new ActionRowBuilder().addComponents( new ButtonBuilder() .setLabel('Button') .setEmoji("🔵") .setStyle(ButtonStyle.Primary) .setCustomId(`b1`)); let reply = await message.reply({ embeds: [e], components: [button] }); const collector = reply.createMessageComponentCollector(); collector.on('collect', async (i) => { if (i.customId === `b1`) { if (i.user.id !== message.author.id) { return i.reply({ content: ":x: You cannot use this button.", ephemeral: true }); // This will not allow others to use the button (except the command user.) } else { await i.deferUpdate(); let b = new EmbedBuilder() .setDescription("You clicked a button.") .setColor("Blurple"); await i.editReply({ embeds: [b] }); } } }); } }); // Making the bot login in Discord. bot.login(process.env.token);