I’m pretty new to coding with javascript and still learning the basics. I have a simple bot command that picks a random response from an array, but right now it just sends plain text messages. I want to make these responses look nicer by putting them inside an embed instead of just regular messages.
I’ve tried reading through the Discord.js documentation about embeds but I’m still confused about how to actually use them in my code.
Here’s what I have so far:
bot.on('messageCreate', function(msg) {
if (msg.author.bot) return;
if (!msg.content.startsWith(cmdPrefix)) return;
var commandArgs = msg.content.substring('$'.length).split(' ');
switch (commandArgs[0].toLowerCase()) {
case 'random':
var randomChoice =
botReplies[Math.floor(Math.random() * botReplies.length)];
msg.channel
.send(randomChoice)
.then()
.catch(console.error);
break;
default:
break;
}
});
How can I change this so the randomChoice gets displayed in an embed instead of as plain text?
Had the same issue when I started with Discord bots a few months ago. What worked for me was building the embed right in the send method instead of using separate variables. Just change your msg.channel.send(randomChoice) line to msg.channel.send({ embeds: [{ color: 0x00ff00, description: randomChoice }] }). Keeps things clean and adds color so it pops. The 0x00ff00 is hex for green, but swap it for whatever matches your bot’s vibe. You can add fields or titles once you’re comfortable with this basic setup.
You need to import EmbedBuilder from discord.js first. Add const { EmbedBuilder } = require('discord.js'); at the top of your file. Then replace your send method with this:
const embed = new EmbedBuilder()
.setDescription(randomChoice)
.setColor('#0099ff');
msg.channel.send({ embeds: [embed] })
.then()
.catch(console.error);
This way you get way more control over how the embed looks compared to the inline method. Once you’re comfortable with this, you can throw in .setTitle(), .setFooter() or whatever else to customize it more.
just throw your randomChoice into an embed: msg.channel.send({ embeds: [{ description: randomChoice }] }) instead of plain send. easiest way to start using embeds without making it complicated.