This repository was archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
226 lines (193 loc) · 6.79 KB
/
index.js
File metadata and controls
226 lines (193 loc) · 6.79 KB
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
226
const { Plugin } = require('powercord/entities');
const { getModule } = require('powercord/webpack');
const { inject, uninject } = require('powercord/injector');
const Settings = require('./Settings.jsx');
const dgram = require('dgram');
const { getChannel } = getModule([ 'getChannel' ], false);
const { getGuild } = getModule([ 'getGuild' ], false);
const blurple = '#5865f2';
const booster = '#ff73fa';
function sendToXSOverlay (data) {
const server = dgram.createSocket('udp4');
server.send(data, 42069, '127.0.0.1', () => {
server.close();
});
}
function messageTypeToBoostLevel (type) {
switch (type) {
case 9: return 1;
case 10: return 2;
case 11: return 3;
}
}
function formatEmotes (content) {
const matches = content.match(new RegExp('(<a?:\\w+:\\d+>)', 'g'));
if (!matches) {
return content;
}
for (const match of matches) {
content = content.replace(new RegExp(`${match}`, 'g'), `:${match.split(':')[1]}:`);
}
return content;
}
function formatChannelMentions (content) {
const matches = content.match(new RegExp('<(#\\d+)>', 'g'));
if (!matches) {
return content;
}
for (const match of matches) {
let channelId = match.split('<#')[1];
channelId = channelId.substring(0, channelId.length - 1);
content = content.replace(new RegExp(`${match}`, 'g'), `<b><color=${blurple}>#${getChannel(channelId).name}</color></b>`);
}
return content;
}
function formatMessage (msg, author) {
let temp = msg.content;
switch (msg.type) {
// Who the fuck cares about i18n anyway
case 0:
case 19:
break;
case 1:
return `<b>${author.username}</b> added <b>${msg.mentions[0].username}</b> to the group.`;
case 2:
return `<b>${author.username}</b> removed <b>${msg.mentions[0].username}</b> from the group.`;
case 3:
return `<b>${author.username}</b> started a call.`;
case 4:
return `<b>${author.username}</b> changed the channel name: <b>${msg.content}</b>`;
case 5:
return `<b>${author.username}</b> changed the channel icon.`;
case 6:
return `<b>${author.username}</b> pinned <b>a message</b> to this channel.`;
case 7:
return `<b>${author.username}</b> joined the server.`;
case 8:
return `<b>${author.username}</b> just <b><color=${booster}boosted</color></b> the server!`;
case 9:
case 10:
case 11:
return `<b>${author.username}</b> just <b><color=${booster}boosted</color></b> the server! <b>${getGuild(msg.guild_id).name}</b> has achieved <b>Level ${messageTypeToBoostLevel(msg.type)}!</b>`;
case 12:
return `<b>${author.username}</b> has added <b>${msg.content}</b> notifications to this channel.`;
case 14:
case 15:
case 16:
case 17:
case 18:
case 20:
case 21:
case 22:
default:
return `Type of message (${msg.type}) not implemented. Please check yourself.`;
}
if (temp.length === 0 && msg.attachments.length > 0) {
return `Uploaded ${msg.attachments[0].filename}`;
}
if (temp.length === 0 && msg.embeds > 0) {
temp = msg.embeds[0].title;
}
temp = temp.replace(new RegExp('@everyone', 'g'), `<b><color=${blurple}>@everyone</color></b>`);
temp = temp.replace(new RegExp('@here', 'g'), `<b><color=${blurple}>@here</color></b>`);
for (const mention of msg.mentions) {
temp = temp.replace(new RegExp(`<@!?${mention.id}>`, 'g'), `<b><color=${blurple}>@${mention.username}</color></b>`);
}
if (msg.mention_roles.length > 0) {
const { roles } = getGuild(msg.guild_id);
for (const roleId of msg.mention_roles) {
const role = roles[roleId];
temp = temp.replace(new RegExp(`<@&${roleId}>`, 'g'), `<b><color=#${parseInt(role.color).toString(16)}>@${role.name}</color></b>`);
}
}
temp = formatEmotes(temp);
temp = formatChannelMentions(temp);
return temp.length !== 0 ? temp : 'Empty';
}
function clearMessage (content) {
return content.replace(new RegExp('<[^>]*>', 'g'), '');
}
function getUserFromRawRecipients (userId, recipients) {
for (const recipient of recipients) {
if (recipient.id === userId) {
return recipient;
}
}
}
function formatGroupDmTitle (channel, msg) {
if (channel.name !== '') {
return channel.name;
}
let temp = '';
const recipients = [];
recipients.push(...channel.rawRecipients);
if (!recipients.filter(r => r.id === msg.author.id).length === 0) {
recipients.push(msg.author);
}
for (const recipient of channel.recipients) {
temp += `${getUserFromRawRecipients(recipient, recipients).username}, `;
}
return temp.substring(0, temp.length - 2);
}
function formatTitle (channel, msg, author) {
switch (channel.type) {
case 0:
case 5:
case 6:
if (channel.parent_id) {
const category = getChannel(channel.parent_id);
return `${msg.member.nick ? msg.member.nick : author.username} (#${channel.name}, ${category.name})`;
}
return `${msg.member.nick ? msg.member.nick : author.username} (#${channel.name})`;
case 1:
return author.username;
case 3:
return `${author.username} (${formatGroupDmTitle(channel, msg)})`;
}
}
function calculateHeight (content) {
if (content.length <= 100) {
return 100;
} else if (content.length <= 200) {
return 150;
} else if (content.length <= 300) {
return 200;
}
return 250;
}
module.exports = class XSOverlayDiscordNotifications extends Plugin {
async startPlugin () {
powercord.api.settings.registerSettings('xsoverlay-discord-notifications-settings', {
category: this.entityID,
label: 'XSOverlay Discord Notifications',
render: Settings
});
const modules = await getModule([ 'makeTextChatNotification' ]);
inject('xsoverlay-discord-notifications', modules, 'makeTextChatNotification', args => {
const [ channel, msg, author ] = args;
const formattedMessage = formatMessage(msg, author);
fetch(`https://cdn.discordapp.com/avatars/${author.id}/${author.avatar}.png?size=128`).then(response => response.arrayBuffer()).then(buffer => {
const data = JSON.stringify({
messageType: 1,
index: 0,
timeout: parseFloat(this.settings.get('notificationTimeout', 5)),
height: calculateHeight(clearMessage(formattedMessage)),
opacity: parseFloat(this.settings.get('notificationOpacity', 0.9)),
volume: 0,
audioPath: '',
title: formatTitle(channel, msg, author),
content: formattedMessage,
useBase64Icon: true,
icon: Buffer.from(buffer).toString('base64'),
sourceApp: 'XSOverlay-Discord-Notifications'
});
sendToXSOverlay(data);
});
return args;
}, true);
}
pluginWillUnload () {
uninject('xsoverlay-discord-notifications');
powercord.api.settings.unregisterSettings('xsoverlay-discord-notifications-settings');
}
};