Bot sending [object, Object] when trying to send a random embed - javascript

I am trying to make it so when I say a command (ex. "!embedzay") the bot will send a random embed
I have tried pretty much all solutions from other posts I found on how to send random embeds or how to fix the [object Object] thing but they just give errors and the bot crashesenter image description here.
I have managed to get sending a single embed and sending random texts to work but I can't get sending random embeds to work.
A heads up that I am very new to JavaScript so if this is a stupid question I am very sorry.
I am using JavaScript and node.js. If you need more information that what is below, please just tell me.
here is my code for random text that works:
//rtest - random test +
if (command === 'rtest'){
const options = [
"message1",
"message2",
"message3",
]
const random = options[Math.floor(Math.random() * options.length)]
message.channel.send(`${random}`)
}
my code for embeds that works:
//embed thingy - send and embed +
if (command === 'embedz'){
message.channel.send({
embeds: [new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')],
});
}
...And my code for random embeds that won't work:
attempt one - gives [object Object] error
if (command === 'rtest'){
const options = [
{embeds: new EmbedBuilder().setTitle('rtfhgfh')},
"message2",
"message3",
]
const random = options[Math.floor(Math.random() * options.length)]
message.channel.send(`${random}`)
}
attempt two - gives cannot send empty message error
// earlier embed attempt - cannot send empty message
if (command === 'embedzay'){
const embed3 = new EmbedBuilder()
.setTitle('rtfhgfh')
.setDescription('yhhfggf')
const embed4 = new EmbedBuilder()
.setTitle('haha')
.setDescription('bunny')
var embedArr = [embed3, embed4];
let randomEmbed = embedArr[Math.floor(Math.random() * embedArr.length)];
message.channel.send(randomEmbed);
}
attempt three - back to [object Object] error
if (command === 'embedzy'){
const embed1 = new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')
const embed2 = new EmbedBuilder()
.setTitle('Some titleuhhhhhh')
.setImage('https://i.imgur.com/64l7KFc.png');
const embeds = [embed1, embed2]
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send(`${random}`)
}
///
attempt four - kinda works?
Using "const myJSON = JSON.stringify();" the correct way this time
it kinda works but stuff shows up as "{"title":"Some titleuhhhhhh","image":{"url":"https://i.imgur.com/64l7KFc.png%22%7D%7D"
if (command === 'embedzye'){
const embed1 = new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')
const embed2 = new EmbedBuilder()
.setTitle('Some titleuhhhhhh')
.setImage('https://i.imgur.com/64l7KFc.png');
const embeds = [embed1, embed2]
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send(`${JSON.stringify(random)}`)
}
attempt five - yes it works!!
if (command === 'embedzyel'){
const embed1 = new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')
const embed2 = new EmbedBuilder()
.setTitle('Some titleuhhhhhh')
.setImage('https://i.imgur.com/64l7KFc.png');
const embeds = [embed1, embed2]
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send({embeds: [random],});
}

message.channel.send({
embeds: [new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')],
});
This works because you send your embed packed in "embeds" object.
Edit your code and add "embeds" object.
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send({
embeds: [random],
});

Related

Discord-buttons are not working in discord.js v12

I want to make an embed with three buttons in discord.js v12. When I am trying to send the message it returns me [object Object]. What I am doing wrong? Here is my code:
const app = new Discord.MessageEmbed()
.setTitle("my embed here")
let staff = new disbut.MessageButton()
.setStyle('gray')
.setLabel('🔰STAFF')
.setURL('url')
let ellas = new MessageButton()
.setStyle('gray')
.setLabel('👮‍♀️POLICE')
.setURL('url')
let ekav = new disbut.MessageButton()
.setStyle('gray')
.setLabel('🚑EKAB')
.setURL('url')
message.channel.send(app, {
buttons: [staff, ellas, ekav]
});

How i change .setImage in embed without restart bot or write cmd again?

as u can see name topic how do I make the .setImage change every update when i change the image without deleting a msg? or write cmd once again
my first attempt : I tried to make it by uploading the image from a hosting site and to get the image link and from there I change the image and then I used setInterval to update the line .setImage but did not work for me this is my code of first attempt
let exampleEmbed = new MessageEmbed()
.setColor('#0099ff')
exampleEmbed.setImage('https://test.000webhostapp.com/images/image.jpg')
.setTimestamp()
try {
let message1 = await message.channel.send({ embeds: [exampleEmbed] });
setInterval(async function(){
let exampleEmbed2 = new MessageEmbed()
.setColor('#0099ff')
exampleEmbed2.setImage('https://test.000webhostapp.com/images/image.jpg')
console.log('updated')
await message1.edit({ embeds: [exampleEmbed2] })
},7000)
} catch(err) {
console.log(err)
}
my second attempt was from the local computer I used MessageAttachment to get me the file path of image and then I also used setInterval to update the line function inside .setImage + MessageAttachment every 7 seconds it didn't work either. Although when I type the command again, the (updated) image appears on the second try
here is my codes try second attempt
let exampleEmbed = new MessageEmbed()
.setColor('#0099ff')
exampleEmbed.setImage('https://test.000webhostapp.com/images/image.jpg')
.setTimestamp()
try {
let message1 = await message.channel.send({ embeds: [exampleEmbed] });
setInterval(async function(){
let exampleEmbed2 = new MessageEmbed()
const attachement = new MessageAttachment('./images/image.jpg','image.jpg');
.setColor('#0099ff')
exampleEmbed2.setImage('attachment://image.jpg')
console.log('updated')
await message1.edit({ embeds: [exampleEmbed2], files: [attachement] })
},7000)
} catch(err) {
console.log(err)
}
Is there another way please? i would like to do the image update sync in any way without restart the bot or even writing cmd again
If I'm not wrong this is caused by Discord's image caching.
To force it to get fresh image (when you edit the message embed) you can put URL parameters (i.e. timestamp). This way you will trick it to think it's a new uncached image.
Example:
'https://test.000webhostapp.com/images/image.jpg?something=' + new Date().getTime()

Show how many servers bot is in an embed | Discord.js

My bot is in 20+ servers and I want to be able to show users the servers my bot is in. I was able to do this on my own but i want the list of servers in one embed and if it passes the embed character limit a page 2 of the embed.
Code:
if (message.content === 'zservers'){
bot.guilds.cache.forEach((guild) => {
const serverlist = new Discord.MessageEmbed()
.setTitle(`𝘚𝘦𝘳𝘷𝘦𝘳𝘴 𝘭𝘰𝘷𝘦𝘭𝘭 𝘪𝘴 𝘪𝘯.`)
.addField(guild)
.setFooter(`Invite lovell <$`)
message.channel.send(serverlist)
})
}
Result:
I want to be able to list all servers in a single embed!
this should work
if (message.content === 'zservers'){
let serverlist = ''
bot.guilds.cache.forEach((guild) => {
serverlist = serverlist.concat(" - " + guild.name + ": ID: " + guild.id + "\n")
})
const embed = new MessageEmbed()
.setColor("RANDOM")
.setTitle("Servers that have Naruse Jun Bot", '')
.setDescription(serverlist)
message.channel.send({embed});
}
Make a general string and then append the guilds to this string.
if (message.content === 'zservers'){
let guilds = '';
bot.guilds.cache.forEach((guild) => {
guilds = guilds.concat(guild).concat("\n"); // concatenate the guild and add a new line in the end.
})
const serverlist = new Discord.MessageEmbed()
.setTitle(`𝘚𝘦𝘳𝘷𝘦𝘳𝘴 𝘭𝘰𝘷𝘦𝘭𝘭 𝘪𝘴 𝘪𝘯.`)
.addField(guilds) // use the general 'guilds' string.
.setFooter(`Invite lovell <$`)
message.channel.send(serverlist)
}
Work?
Edit: UPDATED!
Should work now.
if (message.content === 'zservers'){
let guilds = bot.guilds.cache.array().join('\n')
const serverlist = new Discord.MessageEmbed()
.setTitle(`𝘚𝘦𝘳𝘷𝘦𝘳𝘴 𝘭𝘰𝘷𝘦𝘭𝘭 𝘪𝘴 𝘪𝘯.`)
.setDescription(guilds)
.setFooter(`Invite lovell <$`)
message.channel.send(serverlist)
}
Edit 2: Similar question and exactly right answer -> https://stackoverflow.com/a/60693028/7090121

Discord.js Avatar command

Hello i am trying to make command to show pfp of mentioned person but i am stuck with that
The code that i am using is this
and has no errors
case 'avatar1':
const user = message.mentions.users.first() || message.author;
const avatarEmbed = new Discord.MessageEmbed()
.setColor("RANDOM")
.setAuthor(user.username)
.setImage(user.avatarURL);
message.channel.send(avatarEmbed);
break;
<User>.avatarURL is a method, therefore it would be <User>.avatarURL()
Documentation: https://discord.js.org/#/docs/main/stable/class/User?scrollTo=avatarURL

Discord.js - how do I edit message.embed() statements?

I am making a ping command -
It is very simple to code, but I haven't got the slightest idea how to edit the embed I'm using. Here is my code - I'm using a command handler explaining the exports.run statement.
const Discord = require('discord.js')
exports.run = (bot, message, args) => {
const pingUpdate = new Discord.MessageEmbed()
.setColor('#0099ff')
.setDescription('pinging...')
message.channel.send(pingUpdate);
}
exports.help = {
name: 'ping'
}
I need to edit the ping update embed to make the .description edit to perform this (simple ping calculation)
message.channel.send('pinging...').then((m) => m.edit(`${m.createdTimestamp - message.createdTimestamp}ms`))
This would make the description change from 'pinging...' to 'examplepingms'
Thank you in advance
You going right way. But to .setDescription you need create new Embed constructor and add description.
message.channel.send('pinging...').then(msg => {
let embed = new Discord.MessageEmbed() //For discord v11 Change to new Discord.RichEmbed()
.setDescription(`${msg.createdTimestamp - message.createdTimestamp}`)
msg.edit(embed)
})
also, instead of doing msg.createTimeStamp - message.createdTimestamp you could also do bot.ping.toFixed(2)
This should work (dont have time to test rn)
const Embed = new Discord.MessageEmbed()
.setDescription(":one:")
const newEmbed = new Discord.MessageEmbed()
.setDescription(":two:")
// Edit Part Below
var Msg = await message.channel.send(Embed); // sends message
Msg.edit(newEmbed) // edits message with newembed
Edit: realized that im using a older version of discord.js updated to make it work with newer version
Solution seems outdated again, now you should edit embed in message using
Message#edit({embeds:[MessageEmbed#]})
For example:
const oldEmbed = new MessageEmbed();
const messageHandle = await textChannel.send({embeds: [oldEmbed]});
const newEmbed = new MessageEmbed();
messageHandle.edit({embeds:[newEmbed]});
You don't actually have to create a new embed. You can edit the original:
UPDATE: Per the docs, it is recommended to create new embeds, but you use the original embed to pre-populate the new embed. Then, just update what you need and edit the message with the new embed:
// the original embed posted during collected.on('end')
const embed = new MessageEmbed()
.setColor('0xff4400')
.setTitle(`My Awesome Embed`)
.setDescription('\u200b')
.setAuthor(collected.first().user.username, collected.first().user.displayAvatarURL())
.setImage(`https://via.placeholder.com/400x300.png/808080/000000?text=Placeholder`)
.setTimestamp(new Date())
.setThumbnail('https://via.placeholder.com/200x200.png/808080/000000?text=Placeholder');
// In the original embed, I have a placeholder image that the user
// can replace by posting a new message with the image they want
client.on('messageCreate', async message => {
// adding image to original embed
if (message.attachments.size > 0 && !message.author.bot) {
// get all messages with attachments
const messages = await client.channels.cache.get('<CHANNEL>').messages.fetch();
// get the newest message by the user
const d = messages.filter(msg => msg.embeds.length > 0).filter(m => message.author.username === m.embeds[0].author.name).first();
// create new embed using original as starter
const tempEmbed = new MessageEmbed(d.embeds[0])
// update desired values
tempEmbed.setImage(message.attachments.first().url);
// edit/update the message
d.edit({ embeds: [tempEmbed] });
// delete the posted image
message.delete();
}
});

Categories

Resources