-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
128 lines (97 loc) · 3.23 KB
/
preload.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
// @ts-nocheck
const fs = require('fs');
const discord = require('discord.js');
const electron = require('electron');
const config = require('./config.json');
const messagesSelector = 'div#messages';
const statusSymbol = '•';
const modes = config.modes;
const assets = {};
for (const mode of modes) {
assets[mode] = fs.readdirSync(`./assets/${mode}`).map((file) => `./assets/${mode}/${file}`);
}
let client;
function createDiscordBot () {
if (client) {
client.destroy();
}
client = new discord.Client({
intents: [
discord.Intents.FLAGS.GUILD_MESSAGES,
discord.Intents.FLAGS.GUILDS
]
});
client.on('messageCreate', (message) => {
if (message.author.bot && !message.webhookId) {
return;
}
if (!config.channels.includes(message.channel.id)) {
return;
}
receiveMessage(message);
});
client.on('error', (error) => {
showStatusMessage(error.message);
});
client.on('warn', (message) => {
showStatusMessage(message);
});
client.login(config.token).then(() => showStatusMessage('Discord bot logged in.'));
}
function showStatusMessage (message) {
const messages = document.querySelector(messagesSelector);
const element = document.createElement('p');
element.classList.add('message', 'status');
element.textContent = ` ${statusSymbol} ${message}`;
messages.append(element);
}
function sendMessage (text) {
const message = text.trim();
for (const channel of config.channels) {
client.channels.cache.get(channel).send(message);
}
const messages = document.querySelector(messagesSelector);
const element = document.createElement('p');
element.classList.add('message');
element.textContent = `[${new Date().toTimeString().split(' ')[0]}] Me: ${message}`;
messages.append(element);
messages.scrollTop = messages.scrollHeight;
}
function receiveMessage (message) {
const messages = document.querySelector(messagesSelector);
const element = document.createElement('p');
element.classList.add('message');
const images = getImages(message.content);
let string = message.content.trim();
element.textContent = `[${message.createdAt.toTimeString().split(' ')[0]}] ${message.author.tag}: ${string}`;
if (images) {
for (const image of images) {
const img = document.createElement('img');
img.classList.add('message-image');
img.src = image;
element.append(img);
string = string.replaceAll(image, '');
}
}
for (const attachment of message.attachments) {
const img = document.createElement('img');
img.classList.add('message-image');
img.src = attachment[1].url;
element.append(img);
}
messages.append(element);
messages.scrollTop = messages.scrollHeight;
}
function getImages (url) {
return url.match(/https?.+\.(png|jpg|jpeg|bmp|gif)/gu);
}
function hide () {
electron.ipcRenderer.send('hide');
}
electron.contextBridge.exposeInMainWorld('assets', assets);
electron.contextBridge.exposeInMainWorld('modes', {modes});
electron.contextBridge.exposeInMainWorld('messages', {sendMessage});
electron.contextBridge.exposeInMainWorld('discord', {createDiscordBot});
electron.contextBridge.exposeInMainWorld('config', {config});
electron.contextBridge.exposeInMainWorld('hide', {hide});
createDiscordBot();