简体   繁体   中英

Editing messages using discord.js not working

This problem is giving me a headache.

I'm trying to edit discord messages, but it just won't work.

I get the error: slotDisplayer.edit is not a function

exports["%slots play"] = function(args, data) {
    var frame_count = utils.getRandomInt(15, 25);
    var main_reels = utils.newReels(3);
    var slotDisplayer = data.channel.send(`You spent 1📀 on this slot.\n\nSpinning...`);
    slotDisplayer.then(function(msg){
        utils.nextFrame(main_reels, 0, 0, frameDisplay);
        return msg;
    }).catch(function(err){
        console.log(err);
    });
    console.log(slotDisplayer);
    function frameDisplay(res) {
        var f = res.frame;
        console.log(slotDisplayer);
        console.log(`|${f[0][0]} | ${f[0][1]} | ${f[0][2]} |\n|${f[1][0]} | ${f[1][1]} | ${f[1][2]} |\n|${f[2][0]} | ${f[2][1]} | ${f[2][2]} |`);
        slotDisplayer.edit(utils.generateFrame()).catch(function(err){
            console.log(err);
        });
        if(frame_count > res.frame_index){
            var properIndex = res.index >= main_reels[0].length - 2 ? 0 : res.index;
            setTimeout(function(){utils.nextFrame(main_reels, properIndex,res.frame_index,frameDisplay);}, 200);
        } else {
            var payobj = utils.logic(res.frame);
            slotDisplayer.edit(`|${f[0][0]} | ${f[0][1]} | ${f[0][2]} |\n|${f[1][0]} | ${f[1][1]} | ${f[1][2]} |\n|${f[2][0]} | ${f[2][1]} | ${f[2][2]} |\n${payobj.message}`);
        }
    }
}

I need an example for editing messages in discord.js.

You will need to use .then().

data.channel.send("blah blah").then((msg)=>{
    //your code here! msg.edit will work here.
})

The reason for this is because channel.send() returns a Promise, as per the API: https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=send

Additionally, you can assign the message object to another variable in the .then() so you do not have to work with large amounts of awkward indentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM