[英]Unable to send image buffer attachment using discord.js
我有一個 bot 命令,我在其中訪問 Wolfram Alpha API 以返回圖像並回答用戶提出的問題,但由於某種原因,圖像緩沖區未定義,使用時出現以下錯誤命令
C:\Users\ACER PREDATOR 300\Desktop\MathBot\node_modules\discord.js\src\util\DataResolver.js:127
throw new TypeError('REQ_RESOURCE_TYPE');
^
TypeError [REQ_RESOURCE_TYPE]: The resource must be a string, Buffer or a valid file stream.
at Function.resolveFile (C:\Users\ACER PREDATOR 300\Desktop\MathBot\node_modules\discord.js\src\util\DataResolver.js:127:11)
at Function.resolveFile (C:\Users\ACER PREDATOR 300\Desktop\MathBot\node_modules\discord.js\src\structures\MessagePayload.js:257:41)
at C:\Users\ACER PREDATOR 300\Desktop\MathBot\node_modules\discord.js\src\structures\MessagePayload.js:222:85
at Array.map (<anonymous>)
at MessagePayload.resolveFiles (C:\Users\ACER PREDATOR 300\Desktop\MathBot\node_modules\discord.js\src\structures\MessagePayload.js:222:56)
at TextChannel.send (C:\Users\ACER PREDATOR 300\Desktop\MathBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:50)
at Object.execute (C:\Users\ACER PREDATOR 300\Desktop\MathBot\commands\misc\ask.js:40:25)
at Object.execute (C:\Users\ACER PREDATOR 300\Desktop\MathBot\events\messageCreate.js:159:12)
at Client.<anonymous> (C:\Users\ACER PREDATOR 300\Desktop\MathBot\bot.js:41:35)
at Client.emit (node:events:538:35) {
[Symbol(code)]: 'REQ_RESOURCE_TYPE'
}
這是我的機器人命令的代碼
/**
* @type {import('../../typings').LegacyCommand}
*/
const { MessageAttachment } = require("discord.js");
const WolframAlphaAPI = require('wolfram-alpha-api');
const waApi = WolframAlphaAPI('API-KEY-REMOVED-FOR-QUESTION');
// Make a getImage function
function getImage(question) {
// Returns the buffer of the image
waApi.getSimple(question).then((url) => {
// Extract the base64 string from the url
const sfbuff = new Buffer.from(url.split(",")[1]);
// Convert the base64 string to base64
const base64 = sfbuff.toString('base64');
// Convert the base64 to a buffer
const buffer = Buffer.from(base64, 'base64');
return buffer;
});
}
module.exports = {
name: "ask",
// Refer to typings.d.ts for available properties.
execute(message, args) {
// Get the question from the args
let question = args.join(" ");
// Using the getImage function, get the image of the answer.
const buffer = getImage(question);
const imageAttachment = new MessageAttachment(buffer, "image-attachment.png");
// Send the image
message.channel.send({
content: `Image:\n`,
files: [
{ attachment: imageAttachment }
]
});
},
};
我已經提取了 base64 字符串並將其放入在線 imageToBase64 轉換器中,它確實可以工作,但我似乎無法將其正確轉換為緩沖區。 我懷疑這可能與處理時間有關,但我可能是錯的。
任何幫助將不勝感激,謝謝。
getImage
返回一個承諾,但您沒有正確處理它我會以這種方式更改代碼:
/**
* @type {import('../../typings').LegacyCommand}
*/
const { MessageAttachment } = require("discord.js");
const WolframAlphaAPI = require('wolfram-alpha-api');
const waApi = WolframAlphaAPI('API-KEY-REMOVED-FOR-QUESTION');
// Make a getImage function
function getImage(question) {
// Returns the promise
return waApi.getSimple(question);
}
module.exports = {
name: "ask",
// Refer to typings.d.ts for available properties.
execute(message, args) {
// Get the question from the args
let question = args.join(" ");
// Using the getImage function, get the image of the answer.
const buffer = getImage(question).then((url) => {
// Extract the base64 string from the url
const sfbuff = new Buffer.from(url.split(",")[1]);
// Convert the base64 string to base64
const base64 = sfbuff.toString('base64');
// Convert the base64 to a buffer
const buffer = Buffer.from(base64, 'base64');
const imageAttachment = new MessageAttachment(buffer, "image-attachment.png");
// Send the image
message.channel.send({
content: `Image:\n`,
files: [
{ attachment: imageAttachment }
]
});
})
},
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.