Correctif import + library Opus + Fix util Twitch

This commit is contained in:
Angels-dev
2024-08-09 02:05:09 +02:00
parent 586f68b0df
commit 6bfc3839f6
16 changed files with 178 additions and 1621 deletions

View File

@@ -54,17 +54,11 @@ export default {
}, 30000)
// TWITCH EVENTSUB
let dbData = guildProfile.get('guildTwitch')
if (!dbData?.enabled) return console.log(chalk.magenta(`[Twitch] Module is disabled for "${guild?.name}", please activate with \`/database edit guildTwitch.enabled True\` !`))
let client_id = process.env.TWITCH_APP_ID as string
if (!client_id) return console.log(chalk.magenta('[Twitch] App ID is not defined !'))
let client_secret = process.env.TWITCH_APP_SECRET as string
if (!client_secret) return console.log(chalk.magenta('[Twitch] App Secret is not defined !'))
let twitch = new WebSocket.client().on('connect', async connection => {
console.log(chalk.magenta('[Twitch] EventSub WebSocket Connected !'))
console.log(chalk.magenta(`[Twitch] {${guild.name}} EventSub WebSocket Connected !`))
connection.on('message', async message => { if (message.type === 'utf8') { try {
let data = JSON.parse(message.utf8Data)
@@ -75,7 +69,7 @@ export default {
// Check if the channel access token is still valid before connecting
channel_access_token = await Twitch.checkChannel(client_id, client_secret, channel_access_token, guild) as string
if (!channel_access_token) return console.log(chalk.magenta("[Twitch] Can't refresh channel access token !"))
if (!channel_access_token) return console.log(chalk.magenta(`[Twitch] {${guild.name}} Can't refresh channel access token !`))
// Get broadcaster user id and reward id
let broadcaster_user_id = await Twitch.getUserInfo(client_id, channel_access_token, 'id') as string
@@ -87,29 +81,31 @@ export default {
// Subscribe to all events required
for (let type in topics) {
console.log(chalk.magenta(`[Twitch] Creating ${type}...`))
console.log(chalk.magenta(`[Twitch] {${guild.name}} Creating ${type}...`))
let { version, condition } = topics[type]
let status = await Twitch.subscribeToEvents(client_id, channel_access_token, data.payload.session.id, type, version, condition)
if (!status) return console.error(chalk.magenta(`[Twitch] Failed to create ${type}`))
else if (status.error) return console.log(chalk.magenta('[Twitch] Erreur de connexion EventSub, veuillez vous reconnecter !'))
else console.log(chalk.magenta(`[Twitch] Successfully created ${type}`))
if (!status) return console.error(chalk.magenta(`[Twitch] {${guild.name}} Failed to create ${type}`))
else if (status.error) return console.log(chalk.magenta(`[Twitch] {${guild.name}} Erreur de connexion EventSub, veuillez vous reconnecter !`))
else console.log(chalk.magenta(`[Twitch] {${guild.name}} Successfully created ${type}`))
}
}
// Handle notification messages
else if (data.metadata.message_type === 'notification') Twitch.notification(client_id, channel_access_token, data, guild)
} catch (error) { console.error(chalk.magenta('[Twitch] ' + error)) } } })
.on('error', error => console.error(chalk.magenta('[Twitch] ' + error)))
} catch (error) { console.error(chalk.magenta(`[Twitch] {${guild.name}} ` + error)) } } })
.on('error', error => console.error(chalk.magenta(`[Twitch] {${guild.name}} ` + error)))
.on('close', () => {
console.log(chalk.magenta('[Twitch] EventSub Connection Closed !'))
console.log(chalk.magenta('[Twitch] {${guild.name}} EventSub Connection Closed !'))
twitch.connect('wss://eventsub.wss.twitch.tv/ws')
})
}).on('connectFailed', error => console.error(chalk.magenta('[Twitch] ' + error)))
}).on('connectFailed', error => console.error(chalk.magenta(`[Twitch] {${guild.name}} ` + error)))
// LAUNCH TWITCH EVENTSUB
twitch.connect('wss://eventsub.wss.twitch.tv/ws')
let dbData = guildProfile.get('guildTwitch')
if (!dbData?.enabled) return console.log(chalk.magenta(`[Twitch] {${guild.name}} Module is disabled for "${guild?.name}", please activate with \`/database edit guildTwitch.enabled True\` !`))
else if (!client_id || !client_secret) return console.log(chalk.magenta(`[Twitch] {${guild.name}} App ID or Secret is not defined !`))
else twitch.connect('wss://eventsub.wss.twitch.tv/ws')
})
}
}