【发布时间】:2022-04-06 07:35:25
【问题描述】:
信息:在此文件中使用 NodeJS、Discord.js 和画布,以及 repl.it 托管。
此代码在我的本地计算机上运行,但在移至 repl.it 托管它时,我收到错误“Fontconfig 错误:无法加载默认配置文件”代码如下
const Canvas = require('canvas')
const { MessageAttachment } = require('discord.js')
const path = require('path')
module.exports = (client) => {
client.on('guildMemberAdd', async (member) => {
const { guild } = member
const channelId = "830958927674998815"
if (!channelId) {
return
}
const channel = guild.channels.cache.get(channelId)
if (!channel) {
return
}
const canvas = Canvas.createCanvas(700, 250)
const ctx = canvas.getContext('2d')
const background = await Canvas.loadImage(
path.join(__dirname, '../background.png')
)
let x = 0
let y = 0
ctx.drawImage(background, x, y)
const pfp = await Canvas.loadImage(
member.user.displayAvatarURL({
format: 'png',
})
)
x = canvas.width / 2 - pfp.width / 2
y = 25
ctx.drawImage(pfp, x, y)
ctx.fillStyle = '#ffffff'
ctx.font = '35px sans-serif'
let text = `Welcome ${member.user.tag}`
x = canvas.width / 2 - ctx.measureText(text).width / 2
ctx.fillText(text, x, 60 + pfp.height)
ctx.font = '30px sans-serif'
text = `Member #${guild.memberCount}`
x = canvas.width / 2 - ctx.measureText(text).width / 2
ctx.fillText(text, x, 100 + pfp.height)
const attachment = new MessageAttachment(canvas.toBuffer())
channel.send('', attachment)
})
}```
【问题讨论】:
-
我对 Discord.js 或您可能正在使用的某些框架一无所知,但如果它在 Web 浏览器中运行,您可以在 Firefox 或 Chrome 中使用开发人员工具 (Ctrl+Shift+I)通过查看网络(选项卡)并重新加载和检查 repl.it 主机为字体资源请求返回的响应来解决此问题——尤其是媒体类型和 HTTP 状态代码应该很有帮助。
-
它不在网络上。 discord.js 是一个用于在 discord 平台(具有聊天和语音通道的服务器网络)上制作机器人的框架
-
好的,我看到你用 Node.js 运行它。我会检查您使用上述内容生成的请求的 HTTP 响应代码。如果有的话,您从哪一行得到“Fontconfig 错误”?
-
实际上并没有说是哪一行,但我假设我做 ctx.font 的那一行
标签: javascript node.js discord.js node-canvas