【发布时间】:2022-09-25 20:39:47
【问题描述】:
我已经尝试修复这个问题一个多星期了,在谷歌和 youtube 中搜索这个该死的错误,但没有找到解决方案。我知道我在 node js 的第一天,所以我的代码可能并不完美,但是画布是从做过教程的人那里得到的,我尝试了很多,但无论如何结果总是相同的错误。显然,如果我删除所有相关的 displayAvatarURL 代码,错误就会消失,这很糟糕,因为我不能在我的欢迎图像中使用用户头像。我尝试更改格式,更改代码,用 Gimp 制作的黑色更改背景图像(没关系,因为问题似乎是头像)并删除背景以再次检查。没有任何工作。一旦真正的用户加入,机器人总是会崩溃,这可能是因为头像图像并且在邀请机器人进行测试时它不会崩溃(因为它显然没有自定义头像?)。感谢您的帮助。
错误:
节点:事件:505 投掷者; // 未处理的 \'error\' 事件 ^
错误:不支持的图像类型 在 setSource (C:**\\桌面\\lolinya_bot_js\\node_modules\\canvas\\lib\\image.js:91:13) 在 C:**\\桌面\\lolinya_bot_js\\node_modules\\canvas\\lib\\image.js:59:11 在 C:**\\桌面\\lolinya_bot_js\\node_modules\\simple-get\\index.js:97:7 在传入消息。 (C:**\\Desktop\\lolinya_bot_js\\node_modules\\simple-concat\\index.js:88:13) 在 Object.onceWrapper (节点:事件:641:28) 在 IncomingMessage.emit (节点:事件:539:35) 在 endReadableNT(节点:内部/流/可读:1345:12) 在 processTicksAndRejections (节点:internal/process/task_queues:83:21) 在客户端实例上发出 \'error\' 事件: 在 emitUnhandledRejectionOrErr (node:events:384:10) 在 processTicksAndRejections (节点:internal/process/task_queues:85:21)
索引.js:
const { Client, GatewayIntentBits, Routes, // InteractionType, } = require(\'discord.js\'); const Discord = require(\'discord.js\'); const { REST } = require(\'@discordjs/rest\'); const fs = require(\'node:fs\'); // const { Console } = require(\'node:console\'); const generateImage = require(\'../generateImage.js\'); dotenv.config(); // - CONFIG TOKEN, CLIENT AND GUILD ID const TOKEN = process.env.TOKEN; const CLIENT_ID = process.env.CLIENT_ID; const GUILD_ID = process.env.GUILD_ID; const WELCOME_ID = process.env.WELCOME_ID; const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent, ], }); // - CONFIG SLASH COMMANDS - const commands = []; const commandFiles = fs.readdirSync(\'./src/commands\') .filter(file => file.endsWith(\'js\')); client.slashcommands = new Discord.Collection(); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.slashcommands.set(command.data.name, command); commands.push(command.data.toJSON()); } const rest = new REST({ version: \'10\' }).setToken(TOKEN); // - CLIENT EMITTERS - client.on(\'ready\', () => { console.log(`${client.user.tag} has logged in.`); }); client.on(\'guildMemberAdd\', async (member) => { const img = await generateImage(member); member.guild.channels.cache.get(WELCOME_ID).send({ content: `<@${member.id}> Welcome to the server!`, files: [img] }); }); client.on(\'interactionCreate\', async (interaction) => { if (!interaction.isChatInputCommand()) return; const slashcmd = client.slashcommands.get(interaction.commandName); await slashcmd.run({ client, interaction }); }); // - INITIALIZE THE BOT AND ALSO REFRESH SLASH COMMANDS LIST - (async () => { try { console.log(\'Started refreshing application (/) commands.\'); await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body: commands, }); console.log(\'Successfully reloaded application (/) commands.\'); client.login(TOKEN); } catch (err) { console.error(err); } })(); generateImage.js: const { createCanvas, loadImage, registerFont } = require(\'canvas\'); registerFont(\'./font/Cat paw.ttf\', {family: \'neko\'}); const Discord = require(\'discord.js\'); const { AttachmentBuilder } = require(\'discord.js\'); const background = \"https://i.imgur.com/VZblp7S.jpg\"; const dim = { height: 675, width: 1200, margin: 50, } const av = { size: 256, x: 480, y: 170, } const generateImage = async (member) => { let canvas = await createCanvas(dim.width, dim.height); let ctx = await canvas.getContext(\'2d\'); let username = member.user.username; let discrim = member.user.discriminator; // let avatarURL = member.displayAvatarURL({format: \'jpg\', dynamic: true, size: av.size}) // Draw the canvas for our image // const canvas = Canvas.createCanvas(dim.width, dim.height); // const ctx = canvas.getContext(\'2d\'); const backimg = await loadImage(background); let x = 0 //canvas.width / 2 - backimg.width / 2; let y = 0 //canvas.height / 2 - backimg.height / 2; ctx.drawImage(backimg, x, y); // Draw a semi-transparent box for text readability ctx.fillStyle = \"rgba(0,0,0,0.7)\" ctx.fillRect( dim.margin, dim.margin, dim.width - 2 * dim.margin, dim.height - 2 * dim.margin ); //fillRect(posX,posY, width, height) ctx.save(); ctx.beginPath(); ctx.arc( av.x + av.size / 2, av.y + av.size / 2, av.size / 2, 0, Math.PI * 2, false ); //arc(centerX, centerY, radius, startAngle, endAngle, clockwise) ctx.clip(); let avimg = await loadImage(member.displayAvatarURL({ format: \'png\' })); ctx.drawImage(avimg, av.x, av.y); ctx.restore(); // Config our welcome text ctx.fillStyle = \'pink\'; ctx.textAlign = \'center\'; // Draw the welcome text ctx.font = \'80px Cat paw\'; ctx.fillText(\"Welcome to the Server\", dim.width / 2, dim.margin + 70) // Draw the username text ctx.font = \'100px Cat paw\'; ctx.fillText(`${username} #${discrim}`, dim.width / 2, dim.height - dim.margin - 125); // Draw the server name text ctx.font = \'40px Cat paw\'; ctx.fillText(`You are the member #${member.guild.memberCount}!`, dim.width / 2, dim.height - dim.margin - 50); let attachment = new AttachmentBuilder(canvas.toBuffer(), { name: \'welcome.png\' }) return attachment; } module.exports = generateImage ```
标签: node.js canvas discord bots