Cal*_*ium 5 javascript node.js discord discord.js
我目前正在为我的机器人使用 Discord.js 和 Node,它以设定的时间间隔发送附件。我面临一个问题,有时附件没有完全加载(它们无限期加载,只有当我点击“打开原件”时,我才能看到图像的前几个像素)。为什么是这样?是不是因为发送附件时附件文件不完整?
将图像添加到文件
async function makeCanvas(img, code, channel) {
const canvas = createCanvas(900, 1375);
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#000000";
ctx.fillRect(0, 1255, 900, 120);
let image = await loadImage(img);
ctx.drawImage(image, 0, -40);
ctx.font = "bold 100px sans-serif'";
ctx.textAlign = "center";
ctx.fillStyle = "#FFFFFF";
ctx.fillText(`${code}`, 435, 1350);
const writeable = fs.createWriteStream(`./temp/${channel.id}.png`);
const readable = canvas.createPNGStream();
const connection = readable.pipe(writeable);
return connection.path;
}
Run Code Online (Sandbox Code Playgroud)
发送附件
const imgCode = await applyCodeToImg(url, code, message.channel);
await message.channel.send("A new attachment has appeared!", new Discord.MessageAttachment(imgCode));
Run Code Online (Sandbox Code Playgroud)
我对 JavaScript 和 Node 还是很陌生,请耐心等待!
小智 1
您可以使用
message.channel.send("message", { files: [canvas.toBuffer()] });
Run Code Online (Sandbox Code Playgroud)
或者
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'image.png');
message.channel.send("message", attachment);
Run Code Online (Sandbox Code Playgroud)
要直接发送附件,而不使用 fs 保存附件,请务必查看Canvas#toBuffer()以获取有关 API 的更多信息以及有关基本图像加载的Discordjs.guide 。
| 归档时间: |
|
| 查看次数: |
532 次 |
| 最近记录: |