discord - 使用画布 discord.js 创建个人资料图像时出错
问题描述
当我运行生成带有配置文件信息的图像的命令时出现此错误我已经尝试了一段时间。
我需要帮助来解决这个问题,我不喜欢来寻求帮助,但我已经数小时了,我找不到解决这个问题的解决方案
错误
(node:33728) UnhandledPromiseRejectionWarning: TypeError: (intermediate value).setColor(...).addImage is not a function
at createCanvas (C:\Users\Akure\Desktop\erzavdois\src\commands\Perfil\s.js:29:14)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Object.module.exports.run (C:\Users\Akure\Desktop\erzavdois\src\commands\Perfil\s.js:90:46)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:33728) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:33728) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
代码
const { Canvas } = require('canvas-constructor');
const { createCanvas, loadImage } = require('canvas');
const { MessageAttachment } = require('discord.js');
const { get } = require('node-superfetch');
const db = require('../../../database')
module.exports.run = async (client,message,args) => {
let user = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find(r => r.user.username.toLowerCase() === args.join(' ').toLocaleLowerCase()) || message.guild.members.cache.find(r => r.displayName.toLowerCase() === args.join(' ').toLocaleLowerCase()) || message.member;
if (user.user.bot) return message.channel.send(`**Bots Don't Have A Profile!**`);
let bg = await db.fetch(`bg_${user.id}`)
if (bg === null) bg = 'https://i.imgur.com/cnCS5SG.jpg'
let bg2 = await db.fetch(`bg2_${user.id}`)
if (bg2 === null) bg2 = 'https://i.imgur.com/O8rg78M.png'
let background = bg;
let background3 = bg2;
async function createCanvas() {
var username = user.user.username;
var name = username.length > 10 ? username.substring(0, 12) + "..." : username;
var { body: avatar } = await get(user.user.displayAvatarURL({ format: 'jpg', size: 1024 }));
var { body: background1 } = await get(background)
var { body: background2 } = await get(background3);
return new Canvas(600, 500)
.setColor('#000000')
.addImage(background1, 0, 0, 600, 500)
.addBeveledImage(background2, 0, 0, 600, 400)
.setTextFont('30px Impact')
.addText(`${name}'s Profile Card`, 190, 105)
.addText('Fishes', 445, 140)
.addText('-', 530, 140)
.addText(`${crFormat(fish)}`, 550, 140)
.setTextFont('20px Impact')
.addText('Tags', 525, 190)
.addText(workTag, 507, 230)
.addText(begTag, 520, 270)
.addText(gamesTag, 520, 310)
.setTextFont('30px Impact')
.addText('_______', 505, 190)
.addText('_______', 505, 230)
.addText('_______', 505, 270)
.addText('_______', 505, 310)
.addText('_____________________', 150, 396)
.addText('_____________________', 152, 423)
.addText('|', 148, 422)
.addText('|', 503, 422)
.setTextFont('28px Courier New')
.addText(`About ${user.user.username}`, 160, 182)
.setTextFont('30px Impact')
.addText('Level', 190, 140)
.addText('Ranks', 310, 140)
.setTextFont('23px Impact')
.addText('Works', 31, 260)
.addText('Begs', 31, 315)
.addText('Games', 31, 370)
.addText('-', 97, 260)
.addText('-', 85, 315)
.addText('-', 100, 370)
.addText(`${crFormat(work)}`, 31, 285)
.addText(`${crFormat(begs)}`, 31, 340)
.addText(`${crFormat(games)}`, 31, 395)
.setTextFont('30px Impact')
.addText('Total XP', 160, 340)
.addText('Balance', 160, 380)
.addText(`${crFormat(xp)}`, 273, 340)
.addText(`$${crFormat(balance)}`, 278, 380)
.setTextAlign('center')
.setTextFont('20px Courier New')
.setTextFont('30px Impact')
.addText(`${level}`, 280, 140)
.addText(`${vip}`, 410, 140)
.addText('-', 260, 140)
.addText('-', 393, 140)
.addText('-', 263, 340)
.addText('-', 268, 380)
.setColor("#459466")
.addRect(154, 400, difference, 25)
.setTextFont("18px RobotoRegular")
.setColor("#000000")
.setTextAlign('left')
.addText(`${Info}`, 165, 200)
.addText(`XP: ${xp} / ${nxtLvlXp}`, 300, 418)
.addCircularImage(avatar, 90, 93, 89, 104)
.toBufferAsync();
}
const attachment = new MessageAttachment(await createCanvas(), 'profile.png')
message.channel.send(attachment)
}
module.exports.help = {
name: "s",
aliases: [],
status: 'on',
category: 'perfil'
}
解决方案
正如错误所说,.setColor(...).addImage is not a function
. 您需要定义一个变量,然后设置颜色、添加图像等。
let canvas = new Canvas(600, 500);
canvas.setColor('#000000');
canvas.addImage(background1, 0, 0, 600, 500);
canvas.addBeveledImage(background2, 0, ;0, 600, 400);
canvas.setTextFont('30px Impact')
canvas.addText(`${name}'s Profile Card`, 190, 105);
/*
* And so on...
*
*/
然后return canvas;
。
推荐阅读
- gradle - Gradle 编译文件和复制 fileTree 的工作原理是什么
- django - Django模板变量(模型实例)翻译
- java - Intellij 无法解析符号 'lang3' apache
- mongodb - 启用用户访问控制后 MongoDB 身份验证失败
- c - 在 malloc 块中存储整数
- logging - How to print information in .net core test projects?
- javascript - Vue - Is there a way to scope $refs on a form
- ruby-on-rails - 启动服务器时的 Rails + ActiveStorage 问题 = 未定义的方法“has_one_attached”
- javascript - 以密码输入类型在手机上显示数字键盘
- java - 改进线性搜索技术?