JaBa/events/guildMemberAdd.js

151 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const Canvas = require("canvas"),
Discord = require("discord.js"),
stringCleaner = require("@sindresorhus/slugify"),
{ resolve } = require("path");
// Register assets fonts
Canvas.registerFont(resolve("./assets/fonts/RubikMonoOne-Regular.ttf"), { family: "RubikMonoOne" });
Canvas.registerFont(resolve("./assets/fonts/KeepCalm-Medium.ttf"), { family: "KeepCalm" });
2021-12-10 21:39:54 +05:00
const applyText = (canvas, text, defaultFontSize) => {
const ctx = canvas.getContext("2d");
do {
ctx.font = `${defaultFontSize -= 10}px RubikMonoOne`;
2021-12-10 21:39:54 +05:00
} while (ctx.measureText(text).width > 600);
return ctx.font;
};
module.exports = class {
2021-12-26 19:29:37 +05:00
constructor(client) {
2021-12-10 21:39:54 +05:00
this.client = client;
}
2021-12-26 19:29:37 +05:00
async run(member) {
2021-12-10 21:39:54 +05:00
await member.guild.members.fetch();
2021-12-26 19:29:37 +05:00
const guildData = await this.client.findOrCreateGuild({
id: member.guild.id
});
2021-12-10 21:39:54 +05:00
member.guild.data = guildData;
2021-12-26 19:29:37 +05:00
const memberData = await this.client.findOrCreateMember({
id: member.id,
guildID: member.guild.id
});
2021-12-10 21:39:54 +05:00
if (memberData.mute.muted && memberData.mute.endDate > Date.now()) {
member.guild.channels.cache.forEach((channel) => {
2022-01-03 23:20:33 +05:00
channel.permissionOverwrites.edit(member.id, {
2021-12-10 21:39:54 +05:00
SEND_MESSAGES: false,
ADD_REACTIONS: false,
CONNECT: false
}).catch(() => {});
});
};
// Check if the autorole is enabled
2021-12-16 23:42:58 +05:00
if (guildData.plugins.autorole.enabled) member.roles.add(guildData.plugins.autorole.role).catch(() => {});
2021-12-10 21:39:54 +05:00
// Check if welcome message is enabled
if (guildData.plugins.welcome.enabled) {
const channel = member.guild.channels.cache.get(guildData.plugins.welcome.channel);
if (channel) {
const message = guildData.plugins.welcome.message
.replace(/{user}/g, member)
.replace(/{server}/g, member.guild.name)
.replace(/{membercount}/g, member.guild.memberCount)
.replace(/{createdat}/g, this.client.printDate(member.user.createdAt))
if (guildData.plugins.welcome.withImage) {
const canvas = Canvas.createCanvas(1024, 450),
ctx = canvas.getContext("2d");
// Background image
const background = await Canvas.loadImage("./assets/img/greetings_background.png");
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// This uses the canvas dimensions to stretch the image onto the entire canvas
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Draw username
ctx.fillStyle = "#ffffff";
const username = stringCleaner(member.user.username, {
separator: " ",
lowercase: false,
decamelize: false,
preserveLeadingUnderscore: true,
});
ctx.font = applyText(canvas, username, 50);
ctx.fillText(username, canvas.width - 660, canvas.height - 250);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Draw server name
ctx.font = applyText(canvas, member.guild.translate("administration/welcome:IMG_WELCOME", {
server: member.guild.name
}), 53);
ctx.fillText(member.guild.translate("administration/welcome:IMG_WELCOME", {
server: member.guild.name
}), canvas.width - 690, canvas.height - 85);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Draw discriminator
ctx.font = "35px RubikMonoOne";
2021-12-10 21:39:54 +05:00
ctx.fillText(member.user.discriminator, canvas.width - 624, canvas.height - 180);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Draw number
ctx.font = "22px RubikMonoOne";
2021-12-10 21:39:54 +05:00
ctx.fillText(member.guild.translate("administration/welcome:IMG_NB", {
memberCount: member.guild.memberCount
}), 50, canvas.height - 50);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Draw # for discriminator
ctx.fillStyle = "#44d14a";
ctx.font = "70px RubikMonoOne";
2021-12-10 21:39:54 +05:00
ctx.fillText("#", canvas.width - 690, canvas.height - 165);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Draw Title with gradient
ctx.font = "65px RubikMonoOne";
2021-12-10 21:39:54 +05:00
ctx.strokeStyle = "#1d2124";
ctx.lineWidth = 15;
ctx.strokeText(member.guild.translate("administration/welcome:TITLE"), canvas.width - 670, canvas.height - 330);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
var gradient = ctx.createLinearGradient(canvas.width - 780, 0, canvas.width - 30, 0);
gradient.addColorStop(0, "#e15500");
gradient.addColorStop(1, "#e7b121");
ctx.fillStyle = gradient;
ctx.fillText(member.guild.translate("administration/welcome:TITLE"), canvas.width - 670, canvas.height - 330);
// Pick up the pen
ctx.beginPath();
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
//Define Stroke Line
ctx.lineWidth = 10;
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
//Define Stroke Style
ctx.strokeStyle = "#03A9F4";
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Start the arc to form a circle
ctx.arc(180, 225, 135, 0, Math.PI * 2, true);
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Draw Stroke
ctx.stroke();
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Put the pen down
ctx.closePath();
2021-12-26 19:29:37 +05:00
2021-12-10 21:39:54 +05:00
// Clip off the region you drew on
ctx.clip();
2021-12-26 19:29:37 +05:00
const options = {
format: "png",
size: 512
},
2021-12-10 21:39:54 +05:00
avatar = await Canvas.loadImage(member.user.displayAvatarURL(options));
2021-12-26 19:29:37 +05:00
// Move the image downwards vertically and constrain its height to 200, so it"s a square
2021-12-10 21:39:54 +05:00
ctx.drawImage(avatar, 45, 90, 270, 270);
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), "welcome-image.png");
2022-01-03 23:20:33 +05:00
channel.send({ content: message, files: [attachment] });
2021-12-10 21:39:54 +05:00
} else {
2022-01-03 23:20:33 +05:00
channel.send({ content: message });
2021-12-10 21:39:54 +05:00
};
};
};
}
};