-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
193 lines (152 loc) · 5.1 KB
/
main.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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const tmi = require('tmi.js');
const fs = require('fs');
const soundplay = require('sound-play');
// global variables
let twitchAuth;
let settings;
let twitchClient;
let soundsList = [];
let mainWindow;
const loadSettings = () => {
const rawTwitchData = fs.readFileSync(path.resolve(__dirname, 'twitch_auth.json'));
twitchAuth = JSON.parse(rawTwitchData);
const rawSettingsData = fs.readFileSync(path.resolve(__dirname, 'settings.json'));
settings = JSON.parse(rawSettingsData);
}
const loadSoundList = () => {
soundsList = [];
var loadedSounds = fs.readdirSync(settings.sounds.sound_path);
console.log("sounds loaded: " + loadedSounds.length);
loadedSounds.forEach(sound => {
var candidateSound = sound.split(".mp3")[0];
soundsList.push(candidateSound);
});
}
const saveSettings = () => {
fs.writeFileSync(path.resolve(__dirname, 'twitch_auth.json'), JSON.stringify(twitchAuth));
fs.writeFileSync(path.resolve(__dirname, 'settings.json'), JSON.stringify(settings));
}
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 600,
height: 1000,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html');
mainWindow.webContents.once('dom-ready', () => {
mainWindow.webContents.send("settingsLoaded", settings, twitchAuth);
});
}
app.whenReady().then(() => {
// load Previous Settings
loadSettings();
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
})
/* Listeners to renderer.js */
ipcMain.handle("saveAndConnectBot", (event, editedSettings, authSettings) => {
settings = editedSettings;
twitchAuth = authSettings;
saveSettings();
loadSettings();
loadSoundList();
startupBot();
});
ipcMain.handle("disconnectBot", () => {
disconnectBot();
});
ipcMain.handle("setSoundsPath", () => {
getSoundsPath();
});
const getSoundsPath = () => {
let options = {
title: "Choose a directory with your MP3s",
defaultPath: settings.sounds.sound_path || "C://",
properties: ['openDirectory']
}
let filePath = dialog.showOpenDialogSync(mainWindow, options)
mainWindow.webContents.send('newSoundsPath', filePath + "/");
}
/* TWITCH PART */
const startupBot = () => {
console.log("starting on channel: " + twitchAuth.channel);
twitchClient = new tmi.Client({
options: { debug: true },
identity: {
username: twitchAuth.username,
password: twitchAuth.password
},
channels: [twitchAuth.channel]
});
twitchClient.connect().then(botConnectionSuccessCallback(), botConnectionFailedCallback());
}
function botConnectionSuccessCallback(result) {
twitchClient.on('message', (channel, tags, message, self) => {
// if it was myself don't do anything
if (self) return;
var command = findAndSanitizeCommand(settings.command_prefix, message);
// if there's no recognizable command don't do anything
if (command === undefined) return;
checkSocials(channel, command);
checkSounds(tags, channel, command);
})
mainWindow.webContents.send('botConnected');
}
function botConnectionFailedCallback(error) {
console.log(error);
}
function disconnectBot() {
twitchClient.disconnect().finally(() => {
twitchClient = null; // wipes the current twitch client
mainWindow.webContents.send('botDisconnected');
});
}
/** checks for the existence of a command in a message (always takes last one) */
function findAndSanitizeCommand(prefix, message) {
var foundCommand;
var words = message.split(" ");
words.forEach(word => {
if (word.length >= 2 && word.charAt(0) === prefix) {
foundCommand = word.slice(1);
}
});
return foundCommand;
}
function checkSocials(channel, command) {
var socialUrl = settings.socials[command.toLowerCase()];
if (socialUrl !== undefined) {
twitchClient.say(channel, socialUrl);
}
}
function checkSounds(tags, channel, command) {
// if the sounds list isn't defined. don't iterate or respond
if (soundsList === undefined) return;
// if they just give the command then all the sounds are listed
if (command === settings.sounds.command) {
var resultMessage = ""
soundsList.forEach(sound => {
resultMessage = resultMessage + sound + ", ";
});
twitchClient.say(channel, "sounds list: " + resultMessage);
}
// go through each sound and check
if (soundsList.includes(command)) {
soundplay.play(settings.sounds.sound_path + command + '.mp3', 130);
twitchClient.say(channel, "@" + tags.username + ", has played: " + command + ".");
}
}