Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
790 views
in Technique[技术] by (71.8m points)

discord.js .msg.channel.send().then promise not working

So this is my code for sending an embed, that says Loading shop with the dots changing. However, I am getting a pretty strange error message, which first tells me the error is at msg.delete() with "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'delete' of undefined" and then a second one at the second msg.edit() saying "Cannot read property 'edit' of undefined". I searched up the docs and many forums, but the .then() syntax should be right. Any idea?

var embed = new Discord.MessageEmbed().setColor("GOLD").setTitle("Loading shop");
    msg.channel.send(embed)
        .then(msg => {setTimeout(()=>{msg.edit(embed.setTitle("Loading shop."))}, 500)})
        .then(msg => {setTimeout(()=>{msg.edit(embed.setTitle("Loading shop.."))}, 500)})
        .then(msg => {setTimeout(()=>{msg.edit(embed.setTitle("Loading shop..."))}, 500)})
        .then(msg => {setTimeout(()=>{msg.edit(embed.setTitle("Loading shop"))}, 500)})
        .then(msg => {setTimeout(()=>{msg.edit(embed.setTitle("Loading shop."))}, 500)})
        .then(msg => {setTimeout(()=>{msg.edit(embed.setTitle("Loading shop.."))}, 500)})
        .then(msg => {setTimeout(()=>{msg.edit(embed.setTitle("Loading shop..."))}, 500)})
        .then(msg => {msg.delete()});
question from:https://stackoverflow.com/questions/65923286/discord-js-msg-channel-send-then-promise-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. Do not use this all "thens"
  2. Use await

const embed = new Discord.MessageEmbed().setColor("GOLD").setTitle("Loading shop");
  const msg = message.channel.send(embed); // i change to message because msg will be used as var name
        setTimeout(() => {
      // do stuff
      msg.edit(embed.setTitle("Loading shop.")); // etc..
}, 500);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...