-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (32 loc) · 1.13 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
import { config as dotenv } from 'dotenv';
import { Client } from 'tmi.js';
import { exec } from 'child_process';
dotenv();
const allowNotifications = process.argv.includes('--allow-notifications');
const channelName = process.env.twitch_channel_name;
const client = new Client({
channels: [channelName],
});
client.connect();
client.on('message', (_, tags, message, __) => {
const displayName = tags['display-name'];
console.info(`${displayName}: ${message}`);
if (allowNotifications) {
freeDesktopNotification(sanitize(displayName), sanitize(message));
}
});
function freeDesktopNotification(displayName, message) {
exec(
`gdbus call --session \
--dest=org.freedesktop.Notifications \
--object-path=/org/freedesktop/Notifications \
--method=org.freedesktop.Notifications.Notify \
"Twitch Chat" "0" "0" "from: ${displayName}" "${message}" \
[] "{}" 3000
`
);
exec('paplay "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga"');
}
function sanitize(str) {
return str.replace(/[^A-Za-z0-9\?\!\;\,\-\_\ \:\=]/g, '').substring(0, 70);
}