Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 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 Reply

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...