-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (148 loc) · 4.49 KB
/
index.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
const electron = require('electron');
const {app, BrowserWindow, ipcMain, clipboard} = electron;
const Discord = require('discord.js');
const discordClient = new Discord.Client();
let tokenWindow, mainWindow;
let guilds, guildChannels;
let actualChannel = null;
/**
* Start of windows management
*/
function createTokenWindow() {
tokenWindow = new BrowserWindow({
width: 600,
height: 300,
center: true,
title: "Please enter the bot token",
resizable: false,
minimizable: false,
maximizable: false,
autoHideMenuBar: true,
});
tokenWindow.loadFile('front/token.html');
tokenWindow.on('closed', () => {
tokenWindow = null;
});
}
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 1000,
height: 750,
title: "Bot management",
resizable: true,
minimizable: true,
maximizable: true,
autoHideMenuBar: true,
enableLargerThanScreen: true,
});
mainWindow.loadFile('front/main.html');
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.on('window-all-closed', () => {
discordClient.destroy();
app.quit();
});
app.on('ready', createTokenWindow);
/**
* End of Windows Management
* Start of Discord Bot Management
*/
discordClient.on('ready', () => {
createMainWindow();
tokenWindow.close();
});
discordClient.on('error', (err) => {
electron.dialog.showMessageBox(mainWindow, {
type: 'error',
title: 'An error occurred !',
message: `An error occurred with the bot connexion.\n(Error: ${err.message})\n`,
buttons: ['Disconnect', 'Continue'],
}, (response => {
if(response === 0) {
createTokenWindow();
mainWindow.close();
}
}));
});
discordClient.on('message', (message) => {
if(message.channel === actualChannel) {
mainWindow.webContents.send('newmessage', message);
}
});
discordClient.on('messageUpdate', (oldMessage, newMessage) => {
if(newMessage.channel === actualChannel) {
mainWindow.webContents.send('updatedMessage', oldMessage, newMessage);
}
});
discordClient.on('messageDelete', (message) => {
if(message.channel === actualChannel) {
mainWindow.webContents.send('deletedMessage', message);
}
});
/**
* End of Discord Bot Management
* Start of IPC Management
*/
// We manage now IPC :
ipcMain.on('tokenSent', (event, token) => {
const loginPromise = discordClient.login(token);
loginPromise.then(null, (err) => {
electron.dialog.showMessageBox(tokenWindow, {
type: 'error',
title: 'Bot connexion failed',
message: `The connexion to the bot failed.\n(Error: ${err.message})`,
},() => {
tokenWindow.webContents.send('tokenFailed');
});
});
});
ipcMain.on('loaded', (event) => {
guilds = discordClient.guilds.array();
event.sender.send('init', guilds);
});
ipcMain.on('changesrv', (event, id) => {
actualChannel = null;
let server = discordClient.guilds.get(id);
guildChannels = server.channels.array();
event.sender.send('srvinfo', guildChannels);
});
ipcMain.on('changechannel', (event, id) => {
actualChannel = discordClient.channels.get(id);
event.sender.send('channelok');
});
ipcMain.on('post', (event , message) => {
const sendPromise = actualChannel.send(message);
sendPromise.then(null, (err) => {
electron.dialog.showMessageBox(mainWindow, {
type: 'error',
title: 'The message cannot be sent',
message: `The message cannot be sent.\n(Error: ${err.message})`,
});
});
});
ipcMain.on('generateinvitation', (event, channelId) => {
let channel = discordClient.channels.get(channelId);
channel.createInvite({maxAge: 0}).then((invite) => {
electron.dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'The invitation was created',
message: `The invitation was created with following code:\n${invite.code}`,
buttons: ['Ok', 'Copy']
}, (response) => {
if(response === 1) {
clipboard.writeText(invite.code);
}
});
}, (err) => {
electron.dialog.showMessageBox(mainWindow, {
type: 'error',
title: 'Invitation cannot be created',
message: `The invitation creation failed.\n(Error: ${err.message}`,
}, () => {});
});
});
/**
* End of IPC Management
*/