forked from Metalloriff/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserBirthdays.plugin.js
225 lines (162 loc) · 7.47 KB
/
UserBirthdays.plugin.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//META{"name":"UserBirthdays","website":"https://metalloriff.github.io/toms-discord-stuff/","source":"https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/UserBirthdays.plugin.js"}*//
class UserBirthdays {
getName() { return "User Birthdays"; }
getDescription() { return "Allows you to set birthdays for users and get notified when it's a user's birthday."; }
getVersion() { return "1.0.7"; }
getAuthor() { return "Metalloriff"; }
load() {}
start() {
let libLoadedEvent = () => {
try{ this.onLibLoaded(); }
catch(err) { console.error(this.getName(), "fatal error, plugin could not be started!", err); try { this.stop(); } catch(err) { console.error(this.getName() + ".stop()", err); } }
};
let lib = document.getElementById("NeatoBurritoLibrary");
if(!lib) {
lib = document.createElement("script");
lib.id = "NeatoBurritoLibrary";
lib.type = "text/javascript";
lib.src = "https://rawgit.com/Metalloriff/BetterDiscordPlugins/master/Lib/NeatoBurritoLibrary.js";
document.head.appendChild(lib);
}
this.forceLoadTimeout = setTimeout(libLoadedEvent, 30000);
if(typeof window.NeatoLib !== "undefined") libLoadedEvent();
else lib.addEventListener("load", libLoadedEvent);
}
get theme() {
return document.getElementsByClassName("theme-dark").length ? "theme-dark" : "theme-light";
}
onLibLoaded() {
NeatoLib.Updates.check(this);
this.birthdays = { "264163473179672576" : { day : "5/20", hadIn : "never" } };
const oldData = NeatoLib.Data.load("UserBirthdays", "data").birthdays;
if(oldData) {
for(let i = 0; i < oldData.length; i++) {
this.birthdays[oldData[i][0]] = { day : oldData[i][1], hadIn : oldData[i][3] };
}
}
this.birthdays = NeatoLib.Data.load("UserBirthdays", "birthdays", this.birthdays);
this.popObserver = new MutationObserver(m => {
let pop = m[0].addedNodes[0];
if(!pop) return;
if(pop.className.indexOf("popout") != -1) {
const uid = NeatoLib.ReactData.getProp(pop.getElementsByClassName("discriminator")[0], "user.id"), birthday = this.birthdays[uid];
if(!uid) return;
NeatoLib.DOM.insertHTMLBefore(pop.getElementsByClassName(NeatoLib.getClass("body", "footer"))[0], `
<div class="body-3iLsc4 da-body">
<div class="bodyTitle-Y0qMQz marginBottom8-AtZOdT size12-3R0845 weightBold-2yjlgw">Birthday</div>
<div class="note-3kmerW note-3HfJZ5">
<textarea id="ub-birthdayfield" placeholder="No birthday specified, click to add one. Example: 4/20 or April 20" maxlength="50" class="scrollbarGhostHairline-1mSOM1 scrollbar-3dvm_9 da-scrollbarGhostHairline" style="height: 36px;">${birthday ? birthday.day : ""}</textarea>
</div>
</div>
`);
const field = document.getElementById("ub-birthdayfield");
field.addEventListener("focusout", () => {
if(field.value && new Date(field.value) == "Invalid Date") return NeatoLib.showToast("Invalid date value", "error");
this.setBirthday(uid, field.value);
NeatoLib.showToast("Birthday set!", "success");
});
} else {
pop = m[1].addedNodes[0];
if(pop.className.indexOf("modal") != -1 && (pop.getElementsByClassName(NeatoLib.getClass("profileBadge", "body")).length || pop.getElementsByClassName(NeatoLib.getClass("userInfoSection")).length)) {
const uid = NeatoLib.ReactData.getProp(pop.getElementsByClassName("discriminator")[0], "user.id"), birthday = this.birthdays[uid];
if(!uid) return;
NeatoLib.DOM.insertHTMLAtIndex(1, `
<div class="userInfoSection-2acyCx"><div class="userInfoSectionHeader-CBvMDh size12-3R0845 weightBold-2yjlgw">Birthday</div><div class="note-3kmerW note-QfFU8y"><textarea id="ub-birthdayfield" placeholder="No birthday specified, click to add one. Example: 4/20 or April 20" maxlength="50" class="scrollbarGhostHairline-1mSOM1 scrollbar-3dvm_9" style="height: 24px;">${birthday ? birthday.day : ""}</textarea></div></div>
`, pop.getElementsByClassName(NeatoLib.getClass("scrollerWrapPolyfill", "scroller"))[0]);
const field = document.getElementById("ub-birthdayfield");
field.addEventListener("focusout", () => {
if(field.value && new Date(field.value) == "Invalid Date") return NeatoLib.showToast("Invalid date value", "error");
this.setBirthday(uid, field.value);
NeatoLib.showToast("Birthday set!", "success");
});
}
}
});
const pops = document.getElementsByClassName(this.theme);
this.popObserver.observe(pops[pops.length - 2], { childList : true });
this.popObserver.observe(pops[pops.length - 1], { childList : true });
this.style = NeatoLib.injectCSS(`
.ub-item {
margin: 20px;
min-height: 134px;
display: flex;
flex-direction: column;
contain: layout;
pointer-events: auto;
}
.ub-item-inner {
padding: 10px;
align-items: center;
display: flex;
background: #7289da;
border-radius: 5px;
min-width: 400px;
}
.ub-icon {
margin-right: 10px;
height: 90px;
width: 90px;
}
.ub-icon img {
border-radius: 5px;
}
.ub-label {
color: white;
margin-top: 10px;
width: 95%;
}
.ub-description {
opacity: 0.6;
}
.ub-x-button {
float: right;
cursor: pointer;
color: white;
width: 15px;
height: 15px;
margin-bottom: 17.5%;
}
`);
this.checkForBirthdays();
this.checkLoop = setInterval(() => this.checkForBirthdays(), 60000);
NeatoLib.Events.onPluginLoaded(this);
}
checkForBirthdays() {
const now = new Date();
for(let uid in this.birthdays) {
const user = NeatoLib.Modules.get("getUser").getUser(uid) || { getAvatarURL : function(){ return "/assets/f046e2247d730629309457e902d5c5b3.svg"; }, tag : uid, username : "Unknown User" },
birthday = new Date(this.birthdays[uid].day);
if(now.getMonth() == birthday.getMonth() && now.getDate() == birthday.getDate() && (isNaN(this.birthdays[uid].hadIn) || now.getFullYear() != this.birthdays[uid].hadIn)) {
document.getElementsByClassName("app")[0].insertAdjacentHTML("beforeend", `
<div id="ub-alertwindow">
<div class="backdrop-1wrmKB da-backdrop" style="opacity: 0.85; background-color: rgb(0, 0, 0); transform: translateZ(0px);" onclick="this.parentElement.remove();"></div>
<div class="modal-1UGdnR da-modal" style="opacity: 1;">
<div class="ub-item">
<header class="ub-item-inner">
<div class="ub-icon"><img src="${user.getAvatarURL()}" height="90" width="90"></div>
<div style="flex: 1;">
<span class="ub-label">${uid == "264163473179672576" ? "Metalloriff#2891" : user.tag}</span>
<div class="ub-label ub-description">It's ${uid == "264163473179672576" ? "the plugin creator" : user.username}'s birthday today!</div>
</div>
<span class="ub-x-button" onclick="document.getElementById('ub-alertwindow').remove();">X</span>
</header>
</div>
</div>
</div>
`);
this.birthdays[uid].hadIn = now.getFullYear();
NeatoLib.Data.save("UserBirthdays", "birthdays", this.birthdays);
}
}
}
setBirthday(uid, day) {
if(day) this.birthdays[uid] = { day : day, hadIn : "never" };
else delete this.birthdays[uid];
NeatoLib.Data.save("UserBirthdays", "birthdays", this.birthdays);
}
stop() {
clearInterval(this.checkLoop);
this.popObserver.disconnect();
this.style.destroy();
}
}