简体   繁体   中英

Editing Canvas Attachment in discord.js

I am trying to create a game of sorts using discord.js and Canvas. I recently learned of the Canvas from this tutorial: https://discordjs.guide/popular-topics/canvas.html . I follow most of this example for creating the canvas.

What I want to do is be able to edit the attachment, or replace it with a new one within a setInterval() . I cannot seem to figure out how to edit the attachment, as I have already tried:

//attachid = The id for the canvas attachment; channel is the name of the channel the canvas was set in
bot.login(token).then(() => {
    setInterval(function() { //Start an interval after logging in...
        if(canvas !== null) { //If the canvas was created...
            //Edit canvas here
            var attachment = new Discord.Attachment(canvas.toBuffer) //Create a new canvas attachment
            //The fetchMessage below is where I am having issue
            channel.fetchMessage(attachid).then(msg => msg.edit(attachment));
        }
    }, 1000);
});

I already know how to work with the canvas itself, as it is similar to the HTML5 canvas, but am unsure of how to manipulate this sent attachment. If there is absolutely no way to edit this attachment, how would I delete the first attachment (the one given by attachid)?

If you have any questions, or would like me to provide more code, please let me know!

Discord API doesn't support message edition with another attachment. So you can't add, remove or edit an attachment if the message has already been sent (you can try with your own account, it won't work). The only way you have is to delete the message and re-send it with another attachment...
There is a suggestion submitted to Discord here: https://support.discordapp.com/hc/en-us/community/posts/360041728292-Edit-sent-files

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