commit
This commit is contained in:
parent
3991f820bc
commit
ac0239cdff
3 changed files with 63 additions and 1 deletions
45
src/events/guildMemberAdd.ts
Normal file
45
src/events/guildMemberAdd.ts
Normal file
|
@ -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});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
|
@ -10,6 +10,7 @@ const client: Client = new Client({
|
||||||
GatewayIntentBits.GuildMessages,
|
GatewayIntentBits.GuildMessages,
|
||||||
GatewayIntentBits.MessageContent,
|
GatewayIntentBits.MessageContent,
|
||||||
GatewayIntentBits.DirectMessages,
|
GatewayIntentBits.DirectMessages,
|
||||||
|
GatewayIntentBits.GuildMembers
|
||||||
],
|
],
|
||||||
partials: [
|
partials: [
|
||||||
Partials.Channel,
|
Partials.Channel,
|
||||||
|
|
|
@ -20,7 +20,23 @@ export async function createWelcomeImage(background_url: string, icon_url: strin
|
||||||
const ctx = canvas.getContext('2d')
|
const ctx = canvas.getContext('2d')
|
||||||
|
|
||||||
ctx.drawImage(background, 0, 0, width, height);
|
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");
|
const buffer = canvas.toBuffer("image/png");
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue