0

I'm trying to develop some code that shows the bot when it went online, and let the embed message color change to something else every 2 seconds. (2000ms) But I don't know how, I get an error that says channel.send.edit is not a function or something along those lines.

What I did was... Create a timeout. Edit the message, but it'll show a different message / output for the uptime section. Delete and send a message.

var myInfo = new discord.RichEmbed()
.setAuthor(`${bot.user.username} is now ONLINE`,`${bot.user.avatarURL}`)
.setDescription(`The bot is now up, please check the following below to see when the bot was online.`)
.addField(`Ready At`,`${bot.readyAt}`,true)
.addField(`Timestamp`,`${bot.readyTimestamp}`,true)
.addField(`Uptime`,`${bot.uptime}`,true)
.setColor(0x008704)
.setFooter('Bot is now online.')
.setThumbnail(`${bot.user.avatarURL}`)

var myInfo2 = new discord.RichEmbed()
.setAuthor(`${bot.user.username} is now ONLINE`,`${bot.user.avatarURL}`)
.setDescription(`The bot is now up, please check the following below to see when the bot was online.`)
.addField(`Ready At`,`${bot.readyAt}`,true)
.addField(`Timestamp`,`${bot.readyTimestamp}`,true)
.addField(`Uptime`,`${bot.uptime}`,true)
.setColor(0x00c13a)
.setFooter('Bot is now online.')
.setThumbnail(`${bot.user.avatarURL}`)

bot.channels.get("523649838693482507").send(myInfo).edit(myInfo2);

I expect the bot when it gets online, it'll send the embed message, then 2 seconds later the bot edits the color, and so on.

The output is a bot giving an error, or just not working at all.

SomePerson
  • 1,171
  • 4
  • 16
  • 45

1 Answers1

0

You can use the result of message.channel.send(...) by attaching a then() method to the returned promise like so...

message.channel.send(myInfo)
  .then(m => m.edit(myInfo2)) // Note that this will edit the message immediately.
  .catch(console.error);

You'll notice I added a catch() method to catch the rejected promises in an event where an error is returned.

Alternatively, you can declare a variable as the fulfilled promise. However, the await keyword can only be in async functions. Here's an example...

client.on('message', async message => {
  try {
    const m = await message.channel.send('Hello.');
    await m.edit('Hi again.');
  } catch(err) {
    console.error(err);
  }
});

In this case, we can use a try...catch statement instead of individual catch() methods.

For more about promises, see this MDN documentation.
For more about the TextChannel.send() method, see the Discord.js documentation.

slothiful
  • 5,548
  • 3
  • 12
  • 36