-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.js
114 lines (97 loc) · 2.76 KB
/
badge.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
const canvas = require('canvas')
const fs = require("fs")
let blurple = "#5865F2"
let gray = "#2c2f33"
let white = "#ffffff"
const Status = {
0: "#57F287", // Online
1: "#FEE75C", // Idle
2: "#ED4245", // Do not disturb
3: "#EB459E" // Offline
}
function GenerateJSON(username, status) {
let status_text = ""
switch (status) {
case 0:
status_text = "Online"
break;
case 1:
status_text = "Idle"
break;
case 2:
status_text = "Do Not Disturb"
break
case 3:
status_text = "Offline"
break
}
return {
username: username,
status_id: status,
status_text: status_text,
status_color: Status[status]
}
}
async function GenerateBadge(username, status) {
let fontSize = 50
let stext = ""
switch (status) {
case 0:
stext = "Online"
break;
case 1:
stext = "Idle"
break;
case 2:
stext = "Do Not Disturb"
break
case 3:
stext = "Offline"
break
}
let ctt = canvas.createCanvas(1000, 2000).getContext('2d')
ctt.font = "bold " + fontSize + 'px Segoe UI';
let ts = ctt.measureText(username).width
let sts = ctt.measureText(stext).width
let w = 175 + ts + 45 + sts + 40;
let h = 126;
const canva = canvas.createCanvas(w, h)
const ctx = canva.getContext('2d')
ctx.fillStyle = blurple
ctx.fillRect(0, 0, 175 + ts + 5, h);
ctx.fillStyle = gray
ctx.fillRect(175 + ts + 5, 0, w - 175 + ts + 5, h);
ctx.fillStyle = white
ctx.font = "bold " + fontSize + 'px Segoe UI';
ctx.fillText(username, 155, h / 2 + fontSize * 0.38)
ctx.fillStyle = Status[status]
ctx.fillText(stext, 175 + ts + 45, h / 2 + fontSize * 0.38)
let image = await canvas.loadImage("./discordicon.png")
ctx.drawImage(image, 20, h / 2 - 78.16 / 2, 103.8, 78.16)
return canva
}
async function GenerateBadgeWithoutStatus(username) {
let fontSize = 50
let ctt = canvas.createCanvas(1000, 2000).getContext('2d')
ctt.font = "bold " + fontSize + 'px Segoe UI';
let ts = ctt.measureText(username).width
let w = 175 + ts + 5;
let h = 126;
const canva = canvas.createCanvas(w, h)
const ctx = canva.getContext('2d')
ctx.fillStyle = blurple
ctx.fillRect(0, 0, 175 + ts + 5, h);
ctx.fillStyle = gray
ctx.fillRect(175 + ts + 5, 0, w - 175 + ts + 5, h);
ctx.fillStyle = white
ctx.font = "bold " + fontSize + 'px Segoe UI';
ctx.fillText(username, 155, h / 2 + fontSize * 0.38)
let image = await canvas.loadImage("./discordicon.png")
ctx.drawImage(image, 20, h / 2 - 78.16 / 2, 103.8, 78.16)
return canva
}
async function Render() {
let canva = await GenerateBadgeWithoutStatus("Av32000#9052")
canva.createPNGStream().pipe(fs.createWriteStream('result.png'))
}
module.exports = { GenerateBadge, GenerateBadgeWithoutStatus, GenerateJSON }