Version 3.0 sortie, fusion avec JujulBot
This commit is contained in:
173
src/utils/twitch.ts
Normal file
173
src/utils/twitch.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
import { Guild, TextChannel, EmbedBuilder } from 'discord.js'
|
||||
import axios, { AxiosHeaderValue } from 'axios'
|
||||
import dbGuild from '../schemas/guild'
|
||||
|
||||
interface NotificationData {
|
||||
metadata: {
|
||||
message_type: string
|
||||
}
|
||||
payload: {
|
||||
subscription: {
|
||||
type: string
|
||||
}
|
||||
event: {
|
||||
broadcaster_user_name: string
|
||||
broadcaster_user_login: string
|
||||
}
|
||||
session: {
|
||||
id: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Condition {
|
||||
broadcaster_user_id: string
|
||||
}
|
||||
|
||||
export async function checkChannel (client_id: string, client_secret: string, channel_access_token: string, guild: Guild) {
|
||||
let guildProfile = await dbGuild.findOne({ guildId: guild?.id })
|
||||
if (!guildProfile) return console.log(`Database data for ${guild?.name} does not exist, please initialize with \`/database init\` !`)
|
||||
|
||||
let dbData = guildProfile.get('guildTwitch')
|
||||
if (!dbData?.enabled) return console.log(`Twitch module is disabled for "${guild?.name}", please activate with \`/database edit guildTwitch.enabled True\` !`)
|
||||
|
||||
if (!await validateToken(channel_access_token)) {
|
||||
let channel_refresh_token = dbData.channelRefreshToken
|
||||
if (!channel_refresh_token) return console.log('No refresh token found in database !')
|
||||
|
||||
let GetAccessFromRefresh = await refreshToken(client_id, client_secret, channel_refresh_token)
|
||||
if (!GetAccessFromRefresh) return false;
|
||||
|
||||
[channel_access_token, channel_refresh_token] = [dbData.channelAccessToken, dbData.channelRefreshToken] = GetAccessFromRefresh
|
||||
|
||||
guildProfile.set('guildTwitch', dbData)
|
||||
guildProfile.markModified('guildTwitch')
|
||||
await guildProfile.save().catch(console.error)
|
||||
}
|
||||
return channel_access_token
|
||||
}
|
||||
|
||||
export async function validateToken (access_token: string) {
|
||||
return await axios.get('https://id.twitch.tv/oauth2/validate', {
|
||||
headers: {
|
||||
'Authorization': `OAuth ${access_token}`,
|
||||
}
|
||||
}).then(() => {
|
||||
return true
|
||||
}).catch(error => { console.error(error.response.data) })
|
||||
}
|
||||
|
||||
export async function refreshToken (client_id: string, client_secret: string, refresh_token: string) {
|
||||
return await axios.post('https://id.twitch.tv/oauth2/token', {
|
||||
client_id,
|
||||
client_secret,
|
||||
refresh_token,
|
||||
grant_type: 'refresh_token'
|
||||
}).then(response => {
|
||||
if (response.data.token_type === 'bearer') return [response.data.access_token, response.data.refresh_token]
|
||||
}).catch(error => { console.error(error.response.data) })
|
||||
}
|
||||
|
||||
export async function getStreams (client_id: string, access_token: string, user_login: string) {
|
||||
return await axios.get(`https://api.twitch.tv/helix/streams?user_login=${user_login}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${access_token}`,
|
||||
'Client-Id': client_id as AxiosHeaderValue
|
||||
}
|
||||
}).then(response => {
|
||||
return response.data.data[0]
|
||||
}).catch(error => { console.error(error.response) })
|
||||
}
|
||||
|
||||
export async function getUserInfo (client_id: string, access_token: string, type: string) {
|
||||
return await axios.get('https://api.twitch.tv/helix/users', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${access_token}`,
|
||||
'Client-Id': client_id as AxiosHeaderValue
|
||||
}
|
||||
}).then(response => {
|
||||
if (type === 'login') return response.data.data[0].login
|
||||
if (type === 'id') return response.data.data[0].id
|
||||
if (type === 'profile_image_url') return response.data.data[0].profile_image_url
|
||||
}).catch(error => { console.error(error.response.data) })
|
||||
}
|
||||
|
||||
export async function notification (client_id: string, channel_access_token: string, data: NotificationData, guild: Guild) {
|
||||
let { subscription, event } = data.payload
|
||||
|
||||
let guildProfile = await dbGuild.findOne({ guildId: guild?.id })
|
||||
if (!guildProfile) return console.log(`Database data for ${guild?.name} does not exist, please initialize with \`/database init\` !`)
|
||||
|
||||
let dbData = guildProfile.get('guildTwitch')
|
||||
if (!dbData?.enabled) return console.log(`Twitch module is disabled for "${guild?.name}", please activate with \`/database edit guildTwitch.enabled True\` !`)
|
||||
|
||||
let liveChannelId = dbData.liveChannelId
|
||||
if (!liveChannelId) return console.log('No live channel id found in database !')
|
||||
|
||||
let liveChannel = guild.channels.cache.get(liveChannelId) as TextChannel
|
||||
if (!liveChannel) return console.log(`\u001b[1;35m Can't find channel with id ${liveChannelId}`)
|
||||
|
||||
if (subscription.type === 'stream.online') {
|
||||
console.log(`\u001b[1;35m Stream from ${event.broadcaster_user_name} is now online, sending Discord message...`)
|
||||
|
||||
let stream_data = await getStreams(client_id, channel_access_token, event.broadcaster_user_login)
|
||||
let user_profile_image_url = await getUserInfo(client_id, channel_access_token, 'profile_image_url')
|
||||
|
||||
let embed = new EmbedBuilder()
|
||||
.setColor('#6441a5')
|
||||
.setTitle(stream_data.title)
|
||||
.setURL(`https://twitch.tv/laytho_`)
|
||||
.setAuthor({ name: `${event.broadcaster_user_login.toUpperCase()} EST ACTUELLEMENT EN LIVE !`, iconURL: user_profile_image_url })
|
||||
.setDescription(`Joue à ${stream_data.game_name} avec ${stream_data.viewer_count} viewers`)
|
||||
.setImage(stream_data.thumbnail_url.replace('{width}', '1920').replace('{height}', '1080'))
|
||||
.setTimestamp()
|
||||
let message = await liveChannel.send({ content: `Hey @everyone ! <@${dbData.liveBroadcasterId}> démarre son live sur **Twitch** !`, embeds: [embed] })
|
||||
|
||||
dbData.liveMessageId = message.id
|
||||
guildProfile.set('guildTwitch', dbData)
|
||||
guildProfile.markModified('guildTwitch')
|
||||
await guildProfile.save().catch(console.error)
|
||||
}
|
||||
else if (subscription.type === 'stream.offline') {
|
||||
console.log(`\u001b[1;35m Stream from laytho_ is now offline, editing Discord message...`)
|
||||
|
||||
let message = liveChannel.messages.cache.find(message => message.id === dbData.liveMessageId)
|
||||
if (!message) return console.log(`\u001b[1;35m Can't find message with id ${dbData.liveMessageId}`)
|
||||
if (!message.embeds[0]) return console.log(`\u001b[1;35m Can't find embed in message with id ${dbData.liveMessageId}`)
|
||||
|
||||
let duration = new Date().getTime() - new Date(message.embeds[0].data.timestamp ?? 0).getTime()
|
||||
let seconds = Math.floor(duration / 1000)
|
||||
let minutes = Math.floor(seconds / 60)
|
||||
let hours = Math.floor(minutes / 60)
|
||||
let duration_string = `${hours ? hours + 'H' : ''} ${minutes % 60 ? minutes % 60 + 'M' : ''} ${seconds % 60 ? seconds % 60 + 'S' : ''}`
|
||||
|
||||
let user_profile_image_url = await getUserInfo(client_id, channel_access_token, 'profile_image_url')
|
||||
|
||||
let embed = new EmbedBuilder()
|
||||
.setColor('#6441a5')
|
||||
.setAuthor({ name: `C'EST FINI, LE LIVE A DURÉ ${duration_string} !`, iconURL: user_profile_image_url })
|
||||
.setTimestamp()
|
||||
|
||||
message.edit({ content: `Re @everyone ! <@${dbData.liveBroadcasterId}> a terminé son live sur **Twitch** !`, embeds: [embed] })
|
||||
}
|
||||
}
|
||||
|
||||
export async function subscribeToEvents (client_id: string, access_token: string, session_id: string, type: string, version: string, condition: Condition) {
|
||||
return await axios.post('https://api.twitch.tv/helix/eventsub/subscriptions', {
|
||||
type,
|
||||
version,
|
||||
condition,
|
||||
transport: {
|
||||
method: 'websocket',
|
||||
session_id
|
||||
}
|
||||
}, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${access_token}`,
|
||||
'Client-Id': client_id,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}).then(response => {
|
||||
return response.data.data[0].status
|
||||
}).catch(error => { return error.response.data })
|
||||
}
|
||||
Reference in New Issue
Block a user