【发布时间】:2022-01-26 10:26:11
【问题描述】:
我的代码 index.js
const Discord = require("discord.js")
require("dotenv").config()
const Image = require("./Image")
const Token = "*MY TOKEN*"
const client = new Discord.Client({
intents: [
"GUILDS",
"GUILD_MESSAGES",
"GUILD_MEMBERS"
]
})
client.on("ready", () =>{
console.log(`Logged in as ${client.user.tag}`)
})
client.on("messageCreate", (message) => {
if (message.content == "hi!"){
message.reply("Hello")
}
})
const welcomeChannelID = "935422032458444901"
client.on("guildMemberAdd", async (member) =>{
const img = await Image(member)
member.guild.channels.cache.get(welcomeChannelID).send({
content:`<@${member.id}> Welcome to the Server!`,
files: [img]
})
})
client.login(Token)
image.js
const Canvas = require("canvas")
const Discord = require("discord.js")
const background = "https://ibb.co/c3YYzgw"
const dim = {
height: 675,
width: 1200,
margin: 50
}
const av = {
size: 256,
x: 480,
y: 170
}
const Image = async (member) => {
let username = member.user.username
let discrim = member.user.discriminator
let avatarURL = member.user.displayAvatarURL({format: "png", dynamic: false, size: av.size})
const canvas = Canvas.createCanvas(dim.width, dim.height)
const ctx = canvas.getContext("2d")
//Draw
const backgimage = await Canvas.loadImage(background)
ctx.drawImage(backgimage, 0, 0)
//Black box
ctx.fillStyle = "rgba(0,0,0,0.8)"
ctx.fillRect(dim.margin, dim.margin, dim.width - 2 * dim.margin, dim.height - 2 * dim.margin)
const avimg = await Canvas.loadImage(avatarURL)
ctx.save()
ctx.beginPath()
ctx.arc(av.x + av.size /2, av.h + av.size /2, av.size/2, 0, Math.PI*2, true)
ctx.closePath()
ctx.clip()
ctx.drawImage(avimg, av.x, av.y)
ctx.restore()
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), "welcome.png")
return attachment
}
module.exports = Image
错误
PS D:\DBS> node index.js
Logged in as XGaming Test Bot#1095
D:\DBS\node_modules\canvas\lib\image.js:91
SetSource.call(img, src)
^
Error: Unsupported image type
at setSource (D:\DBS\node_modules\canvas\lib\image.js:91:13)
at D:\DBS\node_modules\canvas\lib\image.js:59:11
at D:\DBS\node_modules\simple-get\index.js:89:7
at PassThrough.<anonymous> (D:\DBS\node_modules\simple-concat\index.js:8:13)
at Object.onceWrapper (node:events:509:28)
at PassThrough.emit (node:events:390:28)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
每当我在服务器中使用我的 alt 帐户加入时都会收到错误消息,当我键入 hi! 时它会说 hello。但是当用户加入我的服务器时没有给出消息。起初它发出欢迎信息 [当我没有添加图片时] 现在它甚至没有显示文本。 ..................................................... .....
【问题讨论】:
-
https://ibb.co/c3YYzgw不是图片,而是 HTML。
标签: javascript node.js discord.js