-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
93 lines (77 loc) · 2.74 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
const axios = require('axios');
const config = require('./config');
const clientId = config.clientId;
const clientSecret = config.clientSecret;
const roleId = config.roleId;
const webhookUrl = config.webhookUrl;
const accessTokenUrl = `https://id.twitch.tv/oauth2/token?client_id=${clientId}&client_secret=${clientSecret}&grant_type=client_credentials`;
const badgesUrl = 'https://api.twitch.tv/helix/chat/badges/global';
let currentVersion = null;
let accessToken = null;
async function getAccessToken() {
if (accessToken) {
return accessToken; // Return the cached access token if available
}
try {
const response = await axios.post(accessTokenUrl);
accessToken = response.data.access_token;
return accessToken;
} catch (error) {
throw new Error('Error retrieving access token: ' + error.message);
}
}
async function checkBadgeUpdates() {
try {
const accessToken = await getAccessToken();
if (!accessToken) {
throw new Error('Access token not found.');
}
console.log('Checking for new badges...');
const response = await axios.get(badgesUrl, {
headers: {
'Client-ID': clientId,
'Authorization': `Bearer ${accessToken}`
}
});
const latestBadge = response.data.data[0];
const latestVersion = latestBadge.version;
const latestBadgeName = latestBadge.title;
const latestBadgeImageURL = latestBadge.image_url_4x;
if (currentVersion && latestVersion > currentVersion) {
const message = `**New badge available!** It's called "${latestBadgeName}". Here's what it looks like: ${latestBadgeImageURL}`;
console.log(message); // Print the message to the console
sendDiscordNotification(message, roleId);
}
currentVersion = latestVersion;
} catch (error) {
console.error('Error checking for badge updates:', error.message);
}
}
async function sendDiscordNotification(message, roleId) {
try {
const content = `${message} <@&${roleId}>`; // Mentions the role using <@&roleID>
await axios.post(webhookUrl, { content });
} catch (error) {
console.error('Error sending Discord notification:', error.message);
}
}
async function sendInitialDiscordNotification() {
try {
await axios.post(webhookUrl, { content: 'BadgeBOT is online.' });
} catch (error) {
console.error('Error sending initial Discord notification:', error.message);
}
}
async function runBadgeChecker() {
try {
await checkBadgeUpdates();
} catch (error) {
console.error('An error occurred:', error.message);
}
}
// Send initial Discord notification
sendInitialDiscordNotification();
// Call the runBadgeChecker function initially
runBadgeChecker();
// Call the runBadgeChecker function every minute (adjust as needed)
setInterval(runBadgeChecker, 60000);