This repository has been archived by the owner on Jun 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCards.js
141 lines (139 loc) · 4.68 KB
/
Cards.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
const { MessageAttachment } = require("discord.js");
const { getMember } = require("./function");
const Canvas = require("canvas");
const Cards = require("./model/card");
module.exports = {
// Level up image
async lvlupimg(message, users) {
const applyText = (canvas, text) => {
const ctx = canvas.getContext("2d");
let fontSize = 70;
do {
ctx.font = `${fontSize -= 10}px sans-serif`;
} while (ctx.measureText(text).width > canvas.width - 300);
return ctx.font;
};
const canvas = Canvas.createCanvas(934, 282);
const ctx = canvas.getContext("2d");
Cards.findOne({
did: message.author.id,
}, async (err, cards)=>{
const cardbg = cards.link;
const member = getMember(message);
const background = await Canvas.loadImage(cardbg);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
// Draw rectangle
ctx.beginPath();
ctx.fillStyle = "rgba(0, 0, 0, 0.8)";
ctx.fillRect(260, 80, 650, 160);
ctx.closePath();
ctx.stroke();
// show Username
ctx.font = applyText(canvas, member.displayName);
ctx.fillStyle = "#fff";
ctx.fillText(member.displayName + " Level up!", 280, 136);
// Show Level & XP
const nxtlvl = 300 * Math.pow(2, users.level);
const xpleft = nxtlvl - users.xp;
ctx.font = "40px sans-serif";
ctx.fillStyle = "#fff";
ctx.fillText("You are level now " + users.level + " - " + users.xp + " XP", 280, 180);
// xp Left
ctx.font = "50px sans-serif";
ctx.fillStyle = "#fff";
ctx.fillText("Next Level in " + xpleft + " xp", 280, 225);
// Get avatar
const avatar = await Canvas.loadImage(message.author.displayAvatarURL({ format: "jpg" }));
ctx.beginPath();
ctx.arc(125, 140, 100, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(avatar, 25, 40, 200, 200);
// put image together and send it
const lvlupimg = new MessageAttachment(canvas.toBuffer(), "lvlup-image.png");
message.channel.send(lvlupimg);
});
},
// Welcome Cards
async WelcomeCad(member, channel) {
const applyText = (canvas, text) => {
const ctx = canvas.getContext("2d");
let fontSize = 70;
do {
ctx.font = `${fontSize -= 10}px sans-serif`;
} while (ctx.measureText(text).width > canvas.width - 300);
return ctx.font;
};
Cards.findOne({
did: member.user.id,
}, async (err, cards)=>{
const canvas = Canvas.createCanvas(934, 282);
const ctx = canvas.getContext("2d");
const cardbg = cards.link;
const background = await Canvas.loadImage(cardbg);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.fillStyle = "rgba(0, 0, 0, 0.8)";
ctx.fillRect(260, 80, 650, 130);
ctx.stroke();
// get username
ctx.font = applyText(canvas, member.user.username);
ctx.fillStyle = "#fff";
ctx.fillText(member.user.username, 280, 141);
// Get guild name
ctx.font = applyText(canvas, member.guild.name);
ctx.fillStyle = "#fff";
ctx.fillText("Joined the server! ", 280, 195);
// Get avatar
const avatar = await Canvas.loadImage(member.user.displayAvatarURL({ format: "jpg" }));
ctx.beginPath();
ctx.arc(140, 128, 110, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(avatar, 25, 15, 256, 256);
const attachment = new MessageAttachment(canvas.toBuffer(), "welcome-image.png");
channel.send(`Welcome ${member.user}`, attachment);
});
},
// Farewell Cards
async farewell(member, channel) {
const applyText = (canvas, text) => {
const ctx = canvas.getContext("2d");
let fontSize = 70;
do {
ctx.font = `${fontSize -= 10}px sans-serif`;
} while (ctx.measureText(text).width > canvas.width - 300);
return ctx.font;
};
Cards.findOne({
did: member.user.id,
}, async (err, cards) =>{
if(err) return console.log(err);
const canvas = Canvas.createCanvas(934, 282);
const ctx = canvas.getContext("2d");
const background = await Canvas.loadImage(cards.link);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.fillStyle = "rgba(0, 0, 0, 0.8)";
ctx.fillRect(260, 80, 650, 130);
ctx.stroke();
// get username
ctx.font = applyText(canvas, member.user.username);
ctx.fillStyle = "#fff";
ctx.fillText(member.user.username, 280, 141);
// Get guild name
ctx.font = applyText(canvas, member.guild.name);
ctx.fillStyle = "#fff";
ctx.fillText("Left the server! ", 280, 195);
// Get avatar
const avatar = await Canvas.loadImage(member.user.displayAvatarURL({ format: "jpg" }));
ctx.beginPath();
ctx.arc(140, 128, 110, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(avatar, 25, 15, 256, 256);
const attachment = new MessageAttachment(canvas.toBuffer(), "farewell-image.png");
channel.send(attachment);
});
},
};