This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
212 lines (188 loc) · 7.93 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
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
const remote = require('electron').remote;
const tmi = require('tmi.js');
const sanitizer = require('sanitizer');
const DiscordRPC = require('discord-rpc');
const settings = require('electron-settings');
DiscordRPC.register('536605206599958535');
const rpc = new DiscordRPC.Client({transport: 'ipc'});
const startTimestamp = new Date();
settings.set('channels', ['#' + settings.get('channel')]);
if (settings.get('identity.username') !== '?anonymous?') {
var client = new tmi.client(settings.getAll());
} else {
$(".loader").css("visibility", "hidden");
$(".msg-container").append(`<div class="msg action">You are watching this stream anonymously.</div>`);
$(".msg-container").append(`<div class="msg action">To use chat, please <a href="login.html">log in</a> with your Twitch username and IRC token.</div>`);
var client = {
on: (a, b) => {},
connect: () => {}
};
}
client.on("message", function(channel, userstate, message, self) {
var badge = "";
if (userstate['badges'] != null) {
if ("broadcaster" in userstate['badges']) {
badge = "<img class='badge' src='https://static-cdn.jtvnw.net/badges/v1/5527c58c-fb7d-422d-b71b-f309dcb85cc1/1'>";
} else if (userstate['user-type'] == "mod" || userstate['mod'] === true) {
badge = "<img class='badge' src='https://static-cdn.jtvnw.net/badges/v1/3267646d-33f0-4b17-b3df-f923a41db1d0/1'>";
} if ("premium" in userstate['badges']) {
badge += "<img class='badge' src='https://static-cdn.jtvnw.net/badges/v1/a1dd5073-19c3-4911-8cb4-c464a7bc1510/1'>";
} if (userstate['subscriber'] === true) {
badge += "<img class='badge' src='https://static-cdn.jtvnw.net/badges/v1/5d9f2208-5dd8-11e7-8513-2ff4adfae661/1'>";
}
}
switch (userstate["message-type"]) {
case "action":
$(".msg-container").append(`<div class="msg" style="color:${userstate['color']}"><b>${badge} ${userstate['display-name']}</b> ${sanitizer.escape(message)}</div>`);
$(".msg-container").scrollTop($(".msg-container")[0].scrollHeight);
break;
case "chat":
$(".msg-container").append(`<div class="msg"><span class="author" style="color:${userstate['color']}">${badge} ${userstate['display-name']}</span><span class="content">${sanitizer.escape(message)}</span></div>`);
$(".msg-container").scrollTop($(".msg-container")[0].scrollHeight);
break;
default:
console.warn(`unknown message type '${userstate['message-type']}'`);
break;
}
});
client.on("notice", function(channel, msgid, message) {
$(".msg-container").append(`<div class="msg action">${sanitizer.escape(message)}</div>`);
$(".msg-container").scrollTop($(".msg-container")[0].scrollHeight);
});
client.on("slowmode", function(channel, enabled, length) {
if (enabled) {
$(".msg-container").append(`<div class="msg action">This room is now in slow mode. You may send messages every ${length} seconds.</div>`);
$(".msg-container").scrollTop($(".msg-container")[0].scrollHeight);
} else {
$(".msg-container").append(`<div class="msg action">This room is no longer in slow mode.</div>`);
$(".msg-container").scrollTop($(".msg-container")[0].scrollHeight);
}
});
client.on("subscribers", function(channel, enabled) {
if (enabled) {
$(".msg-container").append(`<div class="msg action">Subscribers-only mode has been turned on for this room.</div>`);
$(".msg-container").scrollTop($(".msg-container")[0].scrollHeight);
} else {
$(".msg-container").append(`<div class="msg action">Subscribers-only mode has been turned off.</div>`);
$(".msg-container").scrollTop($(".msg-container")[0].scrollHeight);
}
});
client.on("clearchat", function(channel) {
$(".msg-container").html(`<div class="msg action">Chat has been cleared.</div>`);
});
client.on("connecting", function(address, port) {
$(".loader").css("visibility", "visible");
rpc.login({clientId: '536605206599958535'}).catch(console.error);
});
client.on("connected", function(address, port) {
$(".loader").css("visibility", "hidden");
$(".msg-container").append(`<div class="msg action">Connected to ${address}:${port}</div>`);
});
client.on("disconnected", function(reason) {
$(".msg-container").append(`<div class="msg action">You were disconnected. Reason: ${reason}</div>`);
$(".msg-container").append(`<div class="msg action"><a href="login.html">Go back to login</a></div>`);
});
client.on("reconnect", function () {
$(".loader").css("visibility", "visible");
$(".msg-container").append(`<div class="msg action">Reconnecting to server...</div>`);
});
$('.msg-sender').keypress(function(e) {
var keycode = event.keyCode || event.which;
if (keycode == '13') {
client.say(settings.get('channels')[0], $('.msg-sender').val());
$(".msg-sender").val("");
}
});
$(".send-button").click(function() {
client.say(settings.get('channels')[0], $('.msg-sender').val());
$(".msg-sender").val("");
});
$(".settings-btn").click(function() {
remote.getCurrentWindow().loadURL(`file://${__dirname}/settings.html`);
});
$(".chat-collapser").click(function() {
toggleChat();
});
if (settings.get('opacity') == 100) {
$("body").css("background-color", `rgb(14, 12, 19)`);
} else {
$("body").css("background-color", `rgba(14, 12, 19, .${settings.get('opacity')})`);
}
$(".channel-name").html(settings.get('channel'));
var options = {
width: 275,
height: 155,
channel: settings.get('channel')
};
var player = new Twitch.Player("twitch-embed", options);
var streamData = {"title": "Unknown"};
player.setMuted(false);
player.setVolume(1);
function toggleChat() {
$("body").toggleClass("nochat");
const window = remote.getCurrentWindow();
var classes = $("body").attr('class').split(' ');
var display = remote.screen.getPrimaryDisplay();
remote.getCurrentWindow().setResizable(true);
if (classes.indexOf('nochat') != -1) {
// chat is collapsed
window.setSize(325, 250);
window.setPosition(display.bounds.width - window.getSize()[0], 0);
player.setWidth(325);
player.setHeight(205);
} else {
window.setSize(275, 480);
window.setPosition(display.bounds.width - window.getSize()[0], 0);
player.setWidth(275);
player.setHeight(155);
}
remote.getCurrentWindow().setResizable(false);
}
function getStreamMetadata() {
if (player.getEnded()) {
return {"title": "Stream ended"};
}
client.api({
url: `https://api.twitch.tv/helix/streams?user_login=${player.getChannel()}`,
headers: {"Client-ID": "vsvrwnky0r1f14e0lraro310k085wk"}
}, (err, res, body) => {
if (err) {console.error(err);}
console.log(body);
streamData = body.data[0];
//startTimestamp = new Date(streamData.started_at);
});
}
function updateRPC(body) {
if (body === undefined) {
var body = {"title": "Stream offline"}
}
if (player.getEnded()) {
body.title = "Stream offline";
}
var icon = player.isPaused() ? 'pause' : 'play';
var icon = player.getEnded() ? undefined : icon;
rpc.setActivity({
details: `Watching ${player.getChannel()}`,
state: `${body.title}`,
startTimestamp,
largeImageKey: 'twitch',
largeImageText: `TwitchOverlay v${remote.app.getVersion()}\nhttps://overlay.twitchbot.io`,
smallImageKey: icon,
smallImageText: player.isPaused() ? 'Stream paused' : 'Stream playing',
instance: false
});
console.log('RPC Update');
}
rpc.on('ready', () => {
getStreamMetadata();
setInterval(() => {
updateRPC(streamData);
}, 2e3);
});
if (require('electron').remote.getCurrentWindow().getSize()[1] == 250) {
$('body').addClass('nochat');
player.setWidth(325);
player.setHeight(205);
}
module.exports = {client, player, rpc, toggleChat};
client.connect();