-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
178 lines (151 loc) · 5.98 KB
/
renderer.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
const { ipcRenderer, shell } = window.require("electron");
let editedSettings;
let editedAuthSettings;
let totalSocials = 0;
/* auth settings */
const userNameInput = document.getElementById("user-name");
const channelNameInput = document.getElementById("channel-name");
const oauthPasswordInput = document.getElementById("oauth-password");
/* generate oauth button */
const openOauthBtn = document.getElementById("open-oauth");
openOauthBtn.addEventListener("click", () => {
shell.openExternal("https://twitchapps.com/tmi/");
})
/* socials interaction & button */
const socialListDiv = document.getElementById("socials-list");
const addSocialBtn = document.getElementById("add-social");
addSocialBtn.addEventListener("click", () => {
addSingleSocialInput();
})
/* command prefix */
const setCommandPrefixInput = document.getElementById("cmd-prefix");
/* sounds command */
const setSoundsCommandInput = document.getElementById("sounds-cmd");
/* sounds path */
const setSoundsPathBtn = document.getElementById("set-sounds-path");
const setSoundsPathInput = document.getElementById("sounds-path");
setSoundsPathBtn.addEventListener("click", () => {
ipcRenderer.invoke("setSoundsPath");
})
/** start bot */
let isStarted = false;
const startBotBtn = document.getElementById("toggle-bot");
startBotBtn.innerText = "Start";
startBotBtn.style.color = "white";
startBotBtn.style.backgroundColor = "green";
startBotBtn.style.display = "block";
startBotBtn.style.width = "100%";
startBotBtn.addEventListener("click", () => {
if (!isStarted) {
startBotBtn.innerText = "connecting...";
bundleNewSettings();
ipcRenderer.invoke("saveAndConnectBot", editedSettings, editedAuthSettings);
} else {
ipcRenderer.invoke("disconnectBot");
}
})
ipcRenderer.on("botConnected", () => {
startBotBtn.innerText = "Stop";
startBotBtn.style.backgroundColor = "red";
isStarted = true;
toggleEditable(isStarted)
});
ipcRenderer.on("botDisconnected", () => {
startBotBtn.innerText = "Start";
startBotBtn.style.backgroundColor = "green";
isStarted = false;
toggleEditable(isStarted)
});
/** system level things */
ipcRenderer.on("settingsLoaded", (event, settings, authSettings) => {
editedSettings = settings;
editedAuthSettings = authSettings;
userNameInput.value = editedAuthSettings.username;
channelNameInput.value = editedAuthSettings.channel;
oauthPasswordInput.value = editedAuthSettings.password;
setCommandPrefixInput.value = editedSettings.command_prefix;
setSoundsCommandInput.value = editedSettings.sounds.command;
setSoundsPathInput.value = editedSettings.sounds.sound_path;
renderSocialInputs(editedSettings.socials)
});
ipcRenderer.on("newSoundsPath", (event, path) => {
setSoundsPathInput.value = path;
});
const bundleNewSettings = () => {
editedAuthSettings.username = userNameInput.value;
editedAuthSettings.channel = channelNameInput.value;
editedAuthSettings.password = oauthPasswordInput.value;
editedSettings.command_prefix = setCommandPrefixInput.value;
editedSettings.sounds.command = setSoundsCommandInput.value;
editedSettings.sounds.sound_path = setSoundsPathInput.value;
saveSocialInputs();
}
const toggleEditable = (canEdit) => {
userNameInput.disabled = canEdit;
channelNameInput.disabled = canEdit;
oauthPasswordInput.disabled = canEdit;
openOauthBtn.disabled = canEdit;
toggleEditableSocialInputs(canEdit);
addSocialBtn.disabled = canEdit;
setCommandPrefixInput.disabled = canEdit;
setSoundsCommandInput.disabled = canEdit;
setSoundsPathInput.disabled = canEdit;
setSoundsPathBtn.disabled = canEdit;
}
/* social inputs management */
const renderSocialInputs = (socials) => {
if (socials) {
var socialKeys = Object.keys(socials);
socialKeys.forEach(socialKey => {
const inputKey = document.createElement("input");
inputKey.id = `socialkey${totalSocials}`;
inputKey.value = socialKey;
const inputValue = document.createElement("input");
inputValue.id = `socialvalue${totalSocials}`;
inputValue.value = socials[socialKey];
socialListDiv.appendChild(inputKey);
socialListDiv.appendChild(inputValue);
socialListDiv.appendChild(document.createElement("br"));
totalSocials++;
});
} else {
addSingleSocialInput();
}
}
const saveSocialInputs = () => {
const socialsIteration = (socialListDiv.childElementCount / 2)
// clear current socials
editedSettings.socials = JSON.parse("{}");
if (socialListDiv.hasChildNodes()) {
for (var i = 0; i < socialsIteration; i++) {
var socialKeyInput = socialListDiv.querySelector(`#socialkey${i}`)
var socialValueInput = socialListDiv.querySelector(`#socialvalue${i}`)
if (socialKeyInput && socialValueInput && socialKeyInput.value && socialValueInput.value) {
editedSettings.socials[socialKeyInput.value] = socialValueInput.value;
}
}
}
}
const toggleEditableSocialInputs = (canEdit) => {
const socialsIteration = (socialListDiv.childElementCount / 2)
if (socialListDiv.hasChildNodes()) {
for (var i = 0; i < socialsIteration; i++) {
var socialKeyInput = socialListDiv.querySelector(`#socialkey${i}`)
var socialValueInput = socialListDiv.querySelector(`#socialvalue${i}`)
if (socialKeyInput && socialValueInput) {
socialKeyInput.disabled = canEdit;
socialValueInput.disabled = canEdit;
}
}
}
}
const addSingleSocialInput = () => {
const inputKey = document.createElement("input");
inputKey.id = `socialkey${totalSocials}`;
const inputValue = document.createElement("input");
inputValue.id = `socialvalue${totalSocials}`;
socialListDiv.appendChild(inputKey);
socialListDiv.appendChild(inputValue);
socialListDiv.appendChild(document.createElement("br"));
totalSocials++;
}