diff --git a/src/events/guildMemberAdd.ts b/src/events/guildMemberAdd.ts new file mode 100644 index 0000000..91a4c62 --- /dev/null +++ b/src/events/guildMemberAdd.ts @@ -0,0 +1,45 @@ +import { Events, GuildMember, EmbedBuilder, AttachmentBuilder } from "discord.js"; +import { connectToDb, getGuild, getWelcomeConfig } from "../libs/mysql.js"; +import { createWelcomeImage } from "../libs/imageGeneration.js" + +export default { + name: Events.GuildMemberAdd, + async execute(member: GuildMember ) { + const connection = await connectToDb(); + const guild = await getGuild(connection, member.guild.id); + const config = await getWelcomeConfig(connection, member.guild.id); + + if (guild[0]?.welcome_message && config[0]) { + const embed = new EmbedBuilder(); + + if (config[0].title) { + embed.setTitle(config[0].title); + } + + if (config[0].description_format) { + embed.setDescription(config[0].description_format.replace("{user}", `<@${member.user.id}>`).replace("{welcomer}", `<@&${config[0].welcomer_role_id ? config[0].welcomer_role_id : ""}>`)); + } + + if (config[0].vignette_url) { + embed.setThumbnail(config[0].vignette_url); + } + + var attachement; + var buffer; + var files = []; + + if (config[0].background_url) { + buffer = await createWelcomeImage(config[0].background_url, `https://cdn.discordapp.com/avatars/${member.user.id}/${member.user.avatar}.jpeg`, member.user.id); + attachement = new AttachmentBuilder(buffer, { name: `${member.user.id}.png` }) + files.push(attachement) + embed.setImage(`attachment://${member.user.id}.png`) + } + + const channel = member.guild.channels.cache.get(config[0].channel_id); + + if (channel?.isTextBased()) { + channel.send({content: config[0].message_format ? config[0].message_format.replace("{user}", `<@${member.user.id}>`).replace("{welcomer}", `<@&${config[0].welcomer_role_id ? config[0].welcomer_role_id : ""}>`) : "", embeds: [embed], files: files}); + } + } + }, +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a8dab99..3f74d31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ const client: Client = new Client({ GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.DirectMessages, + GatewayIntentBits.GuildMembers ], partials: [ Partials.Channel, diff --git a/src/libs/imageGeneration.ts b/src/libs/imageGeneration.ts index e052558..d1a04a8 100644 --- a/src/libs/imageGeneration.ts +++ b/src/libs/imageGeneration.ts @@ -20,7 +20,23 @@ export async function createWelcomeImage(background_url: string, icon_url: strin const ctx = canvas.getContext('2d') ctx.drawImage(background, 0, 0, width, height); - ctx.drawImage(icon, height / 4, height / 4, height / 4 * 2, height / 4 * 2); + + const circle = { + x: height / 2, + y: height / 2, + radius: height / 4, + } + + ctx.beginPath(); + ctx.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2, true); + ctx.closePath(); + ctx.clip(); + + const aspect = icon.height / icon.width; + const hsx = circle.radius * Math.max(1.0 / aspect, 1.0); + const hsy = circle.radius * Math.max(aspect, 1.0); + + ctx.drawImage(icon,circle.x - hsx,circle.y - hsy,hsx * 2,hsy * 2); const buffer = canvas.toBuffer("image/png"); return buffer;