使用discord.js编辑消息不起作用

时间:2017-07-25 02:03:58

标签: javascript node.js discord

这个问题令我头疼。

我正在尝试编辑不和谐信息,但它只是无法工作。

我收到错误:slotDisplayer.edit不是函数

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

我需要一个在discord.js中编辑消息的示例。

1 个答案:

答案 0 :(得分:3)

您需要使用.then()。

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

原因是因为channel.send()根据API返回Promise:https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=send

此外,您可以将消息对象分配给.then()中的另一个变量,这样您就不必处理大量难以处理的缩进。

相关问题