-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (57 loc) · 2.39 KB
/
index.ts
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
import { Client, Collection, Events, GatewayIntentBits } from 'discord.js';
import { TwitchAuthResponse, TwitchStreamResponse } from './interfaces'
import { streamers } from './streamers';
import { TextChannel } from 'discord.js';
const { discord_token, twitch_client_id, twitch_secret, discord_channel_ids } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.login(discord_token)
const getTwitchOath = async (): Promise<string> => {
const res = await fetch(`https://id.twitch.tv/oauth2/token?client_id=${twitch_client_id}&client_secret=${twitch_secret}&grant_type=client_credentials`, {
method: 'POST'
})
const jsonRes = await res.json() as TwitchAuthResponse
return jsonRes.access_token
}
const getStreamInfo = async (streamer: string): Promise<TwitchStreamResponse> => {
const res = await fetch(`https://api.twitch.tv/helix/streams?client_id=${twitch_client_id}&user_login=${streamer}`, {
headers: {
'Authorization': `Bearer ${await getTwitchOath()}`,
'Client-Id': twitch_client_id
}
})
return await res.json() as TwitchStreamResponse
}
const isStreamLive = (streamInfo: TwitchStreamResponse): boolean => {
return streamInfo.data.length > 0
}
interface History {
[key: string]: boolean
}
const history: History = {}
const checkStreamers = async () => {
console.log('polling streamers...')
for (const streamer of streamers) {
const streamInfo = await getStreamInfo(streamer)
if (isStreamLive(streamInfo)) {
if (history[streamer] !== true) {
alertChannels(streamer, streamInfo)
}
history[streamer] = true
} else {
history[streamer] = false
}
}
}
const alertChannels = async (streamer: string, streamInfo: TwitchStreamResponse) => {
const channelIds: Array<string> = discord_channel_ids
for (const channelId of channelIds) {
const channel = client.channels.cache.get(channelId) as TextChannel
channel.send(`@here ${streamer} is live!\n${streamInfo.data[0].title}\nhttps://twitch.tv/${streamer}\n`)
console.log(`channel ${channelId} alerted for ${streamer}`)
}
return
}
setInterval(checkStreamers, 15000) // 60000 milliseconds = 1 minute