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();
}
});
Related
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],
});
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()
case 'test':
let timed = "10s"
const tests = new Discord.RichEmbed()
.setTitle("tets")
.setColor('#000000')
message.channel.send(tests);
setTimeout(function(){
tests.setColor('#5e4242')
}, ms(timed));
break;
so im trying to make the embed's color change after 10 secs,this is a test command so its called test.I tried many ways and i searched google and it showed this so i decided to try it and it does completly nothing.I use richembed because when i use messagemebed it says client error
Setting the color after you sent the message will edit the object but it won't edit that message, you can edit the message with the same message object but after changing the color aswell
let timed = "10s"
const tests = new Discord.RichEmbed()
.setTitle("tets")
.setColor('#000000');
var testMsg = message.channel.send(tests).then(
setTimeout(function(){
tests.setColor('#5e4242');
testMsg.edit(tests); // edit the message with the same object but different color
}, ms(timed));
)
break;
You would need to set the message you send into a variable and then edit that message.
let timed = "10s"
const tests = new Discord.RichEmbed()
.setTitle("tets")
.setColor('#000000')
const res = await message.channel.send(tests);
setTimeout(function(){
tests.setColor('#5e4242');
res.edit(tests);
}, ms(timed));
Should also note, await only works in an async function
Here is the V12 version
let embed = new Discord.MessageEmbed()
.setColor('#ffffff')
.setTitle('I am a colour-changing embed')
let msg = await message.channel.send(embed)
setTimeout(() => {
embed.setColor('#5e4242');
msg.edit(embed);
}, /*Delay*/);
This code makes a new embed and then creates a let variable which the message is sent with [kinda like sending the message with a tag]. Then it takes the embed variable edits the color and edits the previous message after a delay.
im trying to make my discord.js bot send an embed when someone joins, with as thumbnail their pfp, but it always leaves the same error.
code:
bot.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
const WelcomeChannel = member.guild.channels.cache.find(ch => ch.name === config.WelcomeChannelVar);
var newMember = member
// Do nothing if the channel wasn't found on this server
if (!WelcomeChannel) return;
const welcomeEmbed = new Discord.MessageEmbed()
.setTitle(newMember + 'joined!')
.addField('username', member)
.setColor(0x348a58)
.setThumbnail(newMember.showAvatarURL())
.setFooter('wow very nice profile bro')
WelcomeChannel.send(welcomeEmbed);
member.send("welcome to frogpond! read the rules :)");
});
error:
TypeError: newMember.showAvatarURL is not a function
I've tried everything. can somebody help plz?
thanks!
I'm pretty sure that showAvatarURL() is not a function and you mean avatarURL() instead, so you should change your code to be like this:
const welcomeEmbed = new Discord.MessageEmbed()
// your stuff before thumbnail
.setThumbnail(newMember.user.avatarURL())
// your stuff after thumbnail
edit: the .user is important, because avatarURL is only a User function
It's because you wrote showAvatarURL() instead of displayAvatarURL().
I made so code to build an embed for my Discord bot, but it doesn't seem to work:
client.on('message', message => {
// Ignore messages that aren't from a guild
if (!message.guild) return;
if (message.content.startsWith('$Commands')) {
let Embed = new Rich.Embed;
let Message = message.guild;
let botEmbed = new Discord.RichEmbed()
.setTitle("Commands From TTB")
.setDescription("**Commands :**")
.setColor("#4dff077")
.setAuthorName("Galak$y#3038")
.setAvatar("https://cdn.discordapp.com/avatars/563449221701959700/8386d5fe48d71898c40244e7a5a66d58.png")
.addField("1. *Ban* = *Bans User Mentioned After Command* ``$ban <Mention User>``")
.addField("2. Kick = Kicks User Mentioned After Command ``$kick <Mention User>``")
.addField("3. what is myavatar = ``$what is my avatar``")
.addFooter("New Commands Coming Soon...")
return;
message.channel.send(botEmbed)
}
});
Here is the error:
Embeds are created using the RichEmbed class constructor like so:
let embed = new Discord.RichEmbed()
When you declare Embed you're using Rich.Embed, and as it says, Rich is undefined.