I'm facing a problem trying to develop a bot... I'm trying to to make a "welcome image" with Jimp (Coding with Javascript) for my bot. Everything works fine, except the jimp part [Using the client.on("guildMemberAdd", async member => code. The code is showed below:
client.on("guildMemberAdd", async member => {
let channel = client.guilds.cache.get('932114971935014982')
let mask = await jimp.read('mascara.png')
let background = await jimp.read('download.jpg')
await jimp.read(member.user.displayAvatarURL({format: 'jpg'})).then(async avatar => {
avatar.resize(130, 130)
mask.resize(130,130)
avatar.mask(mask)
background.composite(avatar, 10, 20).write('SenseiAvatar.jpg')
await channel.send(``, { files: ["SenseiAvatar.jpg"]})
console.log('Imagem de boas-vindas ao novo Sensei enviada com sucesso!')
})
.catch(err => {
console.log('There was an error loading the image')
})
})
If someone can help me, showing me which things are wrong in my code, I'll be so much praised by it!
CodePudding user response:
I don't really know jimp but I really recommend you canvas. It's very easy to make welcome images.
Example welcome image with canvas:
const Canvas = require("canvas");
const { MessageAttachment } = require("discord.js");
client.on("guildMemberAdd", async (member) => {
const user = member.user
canvas = Canvas.createCanvas(1024, 500);
const ctx = canvas.getContext("2d");
const font = "sans-serif"
const font_color = "#ffffff"
const bg_color = "#000000"
const title = `${user.username} has joined the server`
const subtitle = "YOUR SUBTITLE HERE"
ctx.fillStyle = bg_color;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = `42px ${font}`;
ctx.textAlign = "center";
ctx.fillStyle = font_color;
ctx.fillText(title, 512, 390, 950);
ctx.font = `32px ${font}`;
ctx.fillStyle = font_color;
ctx.fillText(subtitle, 512, 440, 950);
ctx.beginPath();
ctx.arc(512, 179, 129, 0, Math.PI * 2, true);
ctx.stroke();
ctx.fill();
ctx.beginPath();
ctx.arc(512, 179, 119, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();
const avatar = await Canvas.loadImage(
user.displayAvatarURL({ format: "jpg", size: 1024 })
);
ctx.drawImage(avatar, 393, 60, 238, 238);
const channel = await member.guild.channels.cache.get("CHANNEL_ID");
attachment = new MessageAttachment(canvas.toBuffer(), "card.png");
await channel.send({
content: `<@${user.id}>, welcome`,
files: [attachment],
});
});
