First I'd like to point out that I just started learning discord.js a month ago, so my code is probably all wrong and I understand that.
I'm trying to learn how to write embeds, but a whole ton of stuff doesn't work, mainly because I don't know where to put everything.
In my main js file, I have the following:
} else if (command == 'embed') {
client.commands.get('embed').execute(message, args);
}
And in my embed.js file, I have all of this code which doesn't work at all.
module.exports = {
name: 'embed',
description: 'example embed.',
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields({
name: 'Regular field title',
value: 'Some value here'
}, {
name: '\u200B',
value: '\u200B'
}, {
name: 'Inline field title',
value: 'Some value here',
inline: true
}, {
name: 'Inline field title',
value: 'Some value here',
inline: true
}, )
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
channel.send(exampleEmbed);
}
Thank you for listening to this. I'd love for anyone to help.
You are trying to export a MessageEmbed in the module.exports, but you need to export a function called execute.
module.exports = {
name: 'embed',
description: 'example embed.',
execute: (message, args) => {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields({
name: 'Regular field title',
value: 'Some value here'
}, {
name: '\u200B',
value: '\u200B'
}, {
name: 'Inline field title',
value: 'Some value here',
inline: true
}, {
name: 'Inline field title',
value: 'Some value here',
inline: true
}, )
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
message.channel.send(exampleEmbed);
}
}
Related
When trying to send a Discord.js embed, I'm getting a "DiscordAPIError: Invalid Form Body
embeds[0].description: This field is required" error. Does anyone know why this is happening? I suspect this might be an issue with the source code. I'm using Discord.js v13, and I copied the embed straight off of the Discord.js guide website:
const exampleEmbed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor({ name: 'Some name', iconURL: 'https://i.imgur.com/AfFp7pu.png', url: 'https://discord.js.org' })
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/AfFp7pu.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addFields({ name: 'Inline field title', value: 'Some value here', inline: true })
.setImage('https://i.imgur.com/AfFp7pu.png')
.setTimestamp()
.setFooter({ text: 'Some footer text here', iconURL: 'https://i.imgur.com/AfFp7pu.png' });
outputChannel.send({ embeds: [exampleEmbed] });
Thanks!
You can't use EmbedBuilder in discord.js v13 because this is a discord.js v14 constructor. Just change it to MessageEmbed and it should work.
I am trying to create an embed and I pasted the code straight from the discord.js guide right into the code but it's not working and I can't figure out why it's wrong. The code and error are below
module.exports = {
name: 'infotest',
description: 'infotest',
execute(message, args) {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
channel.send(exampleEmbed);
},
};
ReferenceError: Discord is not defined
at Object.execute (/home/runner/BotNameHere/Commands/info2.js:5:24)
at Client.<anonymous> (/home/runner/BotNameHere/index.js:33:32)
at Client.emit (events.js:314:20)
at Client.EventEmitter.emit (domain.js:483:12)
at MessageCreateAction.handle (/home/runner/BotNameHere/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/runner/BotNameHere/node_modules/ws/lib/event-target.js:132:16)
Read your error, you have to define the variable Discord. Just use require() to use the discord.js module.
When I code the embed, it doesn't work
module.exports = {
name: 'ping',
description: 'it shows the servers ping',
execute(message, args, Discord) {
const newEmbed = new Discord.MessageEmbed()
.setColor('#00A6FF')
.setTitle('Ping')
.setURL('https://discord.gg/X9cpCJ8F5J')
.setDescription('Test')
.setFooter('Test');
message.channel.send(newEmbed);
}
}
No errors, doesn't send the embed doesn't even send a message, no message and no errors
If you use discord.js:
You can use this code instead (official manual of discord.js):
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
channel.send(exampleEmbed);
If you use discord.py:
You can use this code instead (I tested with my bot also):
if message.content.startswith("!command"):
embed=discord.Embed(title="ping", description="Test", color=0x00A6FF)
embed.set_author(name="Test", url="https://discord.gg/X9cpCJ8F5J", icon_url="https://cdn.discordapp.com/attachments/541913766296813570/672624076589760512/DRG.png")
embed.set_footer(text="Test")
await message.channel.send(embed=embed)
I get error embed.description is not a function and I dont know why
I try to do command that write a random command
name: 'random game',
description: "All commands",
execute(message, args, Discord){
const embed = new Discord.MessageEmbed()
let Games = [
"Fortnite",
"Overwatch",
"Among us",
"Rocket league",
"Fall guys",
"Spellbreak",
"Counter Strike Global Offensive",
"Minecraft",
"Valorant",
]
embed.setTitle("Random Game");
embed.description(`You should play ${(Games[Math.floor(Math.random() * (Games.length))])}`);
embed.setColor("RANDOM");
return message.channel.send(embed)
}
}
If you need embed help, the Discord.JS docs can be very helpful.
Here is a sample embed that uses every method in the #messageEmbed() obj:
// at the top of your file
const Discord = require('discord.js');
// inside a command, event listener, etc.
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
channel.send(exampleEmbed);
Also, your error (as mentioned above) is that it is #setDescription()
I want to validate a discord embed before sending it.
Which means, I want to know if an embed has nothing wrong with it before actually sending it on a channel.
var embed = {
title: 'some title',
description: 'some description',
color: 'foo bar'
}
// embed.color is not a valid color, so it will trigger an error when I send it.
// Here I want some way to know that there's something wrong with the embed object.
message.channel.send({embed});
I have made a function to validate embeds. However, it's incomplete and may have false positives or false negatives, but it should meet your needs.
function validURL(url) {
try {
new URL(url);
} catch (err) {
return false;
}
return true;
}
function validEmbed(embed) {
return Object.entries({
title: (title) => typeof title === 'string',
description: (description) => typeof description === 'string',
url: (url) => validURL(url),
timestamp: (timestamp) => (new Date(timestamp)).getTime() > 0,
color: (color) => typeof color === 'number',
fields: (fields) => fields.every(field => typeof field.name === 'string' && typeof field.value === 'string' && typeof field.inline === 'boolean'),
thumbnail: (thumbnail) => validURL(thumbnail.url),
image: (image) => validURL(image.url),
author: (author) => typeof author.name === 'string' && validURL(author.url) && validURL(author.icon_url),
footer: (footer) => typeof footer.text === 'string' && validURL(footer.icon_url)
}).every(field => {
if (!(field[0] in embed)) return true;
return embed[field[0]] && field[1](embed[field[0]]);
});
}
Here is an exaple snippet:
function validURL(e){try{new URL(e)}catch(e){return!1}return!0}function validEmbed(e){return Object.entries({title:e=>"string"==typeof e,description:e=>"string"==typeof e,url:e=>validURL(e),timestamp:e=>new Date(e).getTime()>0,color:e=>"number"==typeof e,fields:e=>e.every(e=>"string"==typeof e.name&&"string"==typeof e.value&&"boolean"==typeof e.inline),thumbnail:e=>validURL(e.url),image:e=>validURL(e.url),author:e=>"string"==typeof e.name&&validURL(e.url)&&validURL(e.icon_url),footer:e=>"string"==typeof e.text&&validURL(e.icon_url)}).every(t=>!(t[0]in e)||e[t[0]]&&t[1](e[t[0]]))}
/* returns true */
console.log(validEmbed({
title: 'The title'
}));
/* returns true */
console.log(validEmbed({
title: 'The title',
description: 'Sample Description.',
url: 'https://i.imgur.com/ttHwmCl.jpg',
timestamp: '2020-07-30T14:44:05.876Z',
color: 3447003,
fields: [{
name: 'name',
value: 'value',
inline: false
},
{
name: 'name2',
value: 'value2',
inline: true
}
],
thumbnail: {
url: 'attachment://stackoverflow2.jpg'
},
image: {
url: 'attachment://stackoverflow.jpg'
},
author: {
name: 'Daemon Beast',
url: 'https://i.imgur.com/P4L260P.jpg',
icon_url: 'https://i.imgur.com/P4L260P.jpg'
},
footer: {
text: 'footer text',
icon_url: 'https://i.imgur.com/ttHwmCl.jpg'
}
}));
/* returns false */
console.log(validEmbed({
author: 4567876567
}));