-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelcome.js
149 lines (122 loc) · 4.67 KB
/
welcome.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const discord = require("discord.js");
const Canvas = require("canvas");
const path = require("path");
const getMongoClient = require("./mongo");
const editJsonFile = require("edit-json-file");
const { isDate } = require("util");
const file = editJsonFile(path.join(__dirname, "/config.json"));
Canvas.registerFont(path.join(__dirname, "/NotoSans-Regular.ttf"), {
family: "fontfamily",
});
Canvas.registerFont(path.join(__dirname, "/aileron.light-italic.otf"), {
family: "italic fontfamily",
});
module.exports = async (client) => {
client.on("guildMemberAdd", async (member) => {
const { guild } = member;
const channel = member.guild.channels.cache.find(
(ch) => ch.name === "welcome"
);
if (!channel) return;
const canvas = Canvas.createCanvas(700, 250);
const context = canvas.getContext("2d");
const background = await Canvas.loadImage("./background.png");
let x = 0;
let y = 0;
context.drawImage(background, x, y, canvas.width, canvas.height);
context.fillStyle = "#ffffff";
context.font = "30px fontfamily";
let text = `Welcome ${member.user.tag}`;
x = canvas.width / 2 - context.measureText(text).width / 2;
context.fillText(text, x, 60 + 128);
context.font = "27px italic fontfamily";
text = `Member #${guild.memberCount}`;
x = canvas.width / 2 - context.measureText(text).width / 2;
context.fillText(text, x, 100 + 128);
const pfp = await Canvas.loadImage(
member.user.displayAvatarURL({ format: "png" })
);
x = canvas.width / 2 - 128 / 2;
y = 25;
// Pick up the pen
context.beginPath();
// Start the arc to form a circle
context.arc(350, 89, 64, 0, Math.PI * 2, true);
context.lineWidth = 5;
context.strokeStyle = "white";
context.stroke();
// Put the pen down
context.closePath();
// Clip off the region you drew on
context.clip();
context.drawImage(pfp, x, y, 128, 128);
const attachment = new discord.MessageAttachment(
canvas.toBuffer(),
"welcome-image.png"
);
channel.send(`${file.get().message}, ${member}!`);
// New memeber add
let embed = new discord.MessageEmbed();
// const discordNewbie = member.guild.channels.cache.find(
// (ch) => ch.name === 'discord-newbie'
// );
// const getRoles = member.guild.channels.cache.find(
// (ch) => ch.name === 'get-roles'
// );
// const aboutServer = member.guild.channels.cache.find(
// (ch) => ch.name === 'about'
// );
// const chat = member.guild.channels.cache.find((ch) => ch.name === 'chat');
// embed.setTitle('Welcome to C Workshop server\n');
// // embed.setAuthor(client.user.username, client.user.avatarURL(32));
// embed.setDescription(
// `If you are new to discord visit <#${discordNewbie.id}> channel and then visit <#${getRoles.id}>
// Read about the server in <#${aboutServer.id}> and be sure to send your first message in <#${chat.id}>
// `
// );
const titleAndDescription = await getTitleAndDescription(
member.guild.id,
member.guild.name
);
embed.setTitle(titleAndDescription.title);
// embed.setAuthor(client.user.username, client.user.avatarURL(32));
embed.setDescription(titleAndDescription.description);
embed.setFooter("💫⭐💫");
//embed.attachFiles(attachment).setImage("attachment://welcome-image.png");
embed.setImage("attachment://welcome-image.png");
channel.send({ embeds: [embed] , files: [attachment]});
// let role = member.guild.roles.cache.find(
// (role) => role.name === 'Participants'
// );
// if (role) member.roles.add(role);
});
};
async function getTitleAndDescription(guildID, guildName) {
const mongoClient = getMongoClient();
try {
await mongoClient.connect();
const collection = await mongoClient
.db("configs")
.collection("welcomeMessage");
const result = await collection.findOne({ _id: guildID });
titleAndDescription = {};
if (result) {
titleAndDescription["title"] = (await result.title)
? result.title
: "Hello";
titleAndDescription["description"] = (await result.description)
? result.description
: `Welcome to ${guildName}, hope you have a nice time here`;
} else {
titleAndDescription["title"] = "Hello";
titleAndDescription[
"description"
] = `Welcome to ${guildName}, hope you have a nice time here`;
}
} catch (e) {
console.log(e);
} finally {
await mongoClient.close();
}
return await titleAndDescription;
}