gpt4 book ai didi

node.js - node-gd 的复制功能无法识别

转载 作者:行者123 更新时间:2023-12-03 12:15:11 24 4
gpt4 key购买 nike

我继承了Discord使用 node-gd 的机器人制作带有用户输入数据的横幅。制作横幅时,我使用 .copy功能将图像放置在基本图像的顶部。但是,当我运行我的代码时,它抛出一个错误,说它不是一个函数。

这是相关的代码( user.input 是一个数字 1-5 ,默认是 ? ):

   if (user.input != '?') {
imgTemp = gd.createFromPng(`./img/INPUT_${user.input}.png`);
imgTemp.copy(img, 213, 60, 0, 0, 37, 17);
}

这是错误:
> /home/discordbots/Desktop/bots/splatoonBot/splatBot2.js:676
> imgTemp.copy(img, 213, 60, 0, 0, 37, 17);
> ^ TypeError: imgTemp.copy is not a function
> at generateBanner (/home/discordbots/Desktop/bots/splatoonBot/splatBot2.js:676:21)
> at Client.<anonymous> (/home/discordbots/Desktop/bots/splatoonBot/splatBot2.js:894:25)
> at Client.emit (events.js:315:20)
> at MessageCreateAction.handle (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
> at Object.module.exports [as MESSAGE_CREATE] (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
> at WebSocketManager.handlePacket (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
> at WebSocketShard.onPacket (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
> at WebSocketShard.onMessage (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
> at WebSocket.onMessage (/home/discordbots/Desktop/bots/splatoonBot/node_modules/ws/lib/event-target.js:125:16)
> at WebSocket.emit (events.js:315:20)

最佳答案

这是因为 gd.createFromPng返回一个 promise 。它不是包含方法 copy 的类.您可以在 node-gd documentation 中阅读更多相关信息。 .

所以本质上:

if (user.input != '?') {
// the "await" keyword lets you grab the actual value from the promise
let imgTemp = await gd.createFromPng(`./img/INPUT_${user.input}.png`);
// You can then implement the copy method on this newly returned value
imgTemp.copy(img, 213, 60, 0, 0, 37, 17);
}

希望这能解决您的问题。

关于node.js - node-gd 的复制功能无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61946611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com