Version 3.0 sortie, fusion avec JujulBot
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
import { Events, Client, ActivityType } from 'discord.js'
|
||||
import { YoutubeiExtractor } from "discord-player-youtubei"
|
||||
import { useMainPlayer } from 'discord-player'
|
||||
import { connect } from 'mongoose'
|
||||
import WebSocket from 'websocket'
|
||||
import chalk from 'chalk'
|
||||
import 'dotenv/config'
|
||||
|
||||
import dbGuildInit from '../../utils/dbGuildInit'
|
||||
import dbGuild from '../../schemas/guild'
|
||||
import replay from '../../utilsPlayer/replay'
|
||||
import disco from '../../utilsPlayer/disco'
|
||||
import { playerDisco, playerReplay } from '../../utils/player'
|
||||
import * as Twitch from '../../utils/twitch'
|
||||
import rss from '../../utils/rss'
|
||||
|
||||
|
||||
export default {
|
||||
name: Events.ClientReady,
|
||||
once: true,
|
||||
@@ -18,7 +19,8 @@ export default {
|
||||
console.log(chalk.blue(`[DiscordJS] Connected to Discord ! Logged in as ${client.user?.tag ?? 'unknown'}`))
|
||||
client.user?.setActivity('some bangers...', { type: ActivityType.Listening })
|
||||
|
||||
await useMainPlayer().extractors.loadDefault(ext => ext === 'YouTubeExtractor' || ext === 'SpotifyExtractor').then(() => console.log(chalk.blue('[Discord-Player] YouTube and Spotify extractors loaded.'))).catch(console.error)
|
||||
await useMainPlayer().extractors.loadDefault(ext => ext === 'SpotifyExtractor').then(() => console.log(chalk.blue('[Discord-Player] Spotify extractor loaded.'))).catch(console.error)
|
||||
await useMainPlayer().extractors.register(YoutubeiExtractor, {}).then(() => console.log(chalk.blue('[Discord-Player] Youtube extractor loaded.'))).catch(console.error)
|
||||
|
||||
let mongo_url = `mongodb://${process.env.MONGOOSE_USER}:${process.env.MONGOOSE_PASSWORD}@${process.env.MONGOOSE_HOST}/${process.env.MONGOOSE_DATABASE}`
|
||||
await connect(mongo_url).catch(console.error)
|
||||
@@ -29,14 +31,14 @@ export default {
|
||||
let guildProfile = await dbGuild.findOne({ guildId: guild.id })
|
||||
|
||||
if (!guildProfile) guildProfile = await dbGuildInit(guild)
|
||||
if (guildProfile.guildPlayer?.replay?.enabled && guildProfile.guildPlayer?.replay?.textChannelId) await replay(client, guildProfile)
|
||||
if (guildProfile.guildPlayer?.replay?.enabled && guildProfile.guildPlayer?.replay?.textChannelId) await playerReplay(client, guildProfile)
|
||||
|
||||
client.disco = { interval: {} as NodeJS.Timeout }
|
||||
client.disco.interval = setInterval(async () => {
|
||||
let guildProfile = await dbGuild.findOne({ guildId: guild.id })
|
||||
|
||||
if (guildProfile?.guildPlayer?.disco?.enabled) {
|
||||
let state = await disco(client, guildProfile)
|
||||
let state = await playerDisco(client, guildProfile)
|
||||
if (state === 'clear') clearInterval(client.disco.interval)
|
||||
}
|
||||
}, 3000)
|
||||
@@ -50,6 +52,64 @@ export default {
|
||||
if (state === 'clear') clearInterval(client.rss.interval)
|
||||
}
|
||||
}, 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 !'))
|
||||
|
||||
connection.on('message', async message => { if (message.type === 'utf8') { try {
|
||||
let data = JSON.parse(message.utf8Data)
|
||||
let channel_access_token = guildProfile.get('guildTwitch')?.channelAccessToken as string
|
||||
|
||||
// Check when Twitch asks to login
|
||||
if (data.metadata.message_type === 'session_welcome') {
|
||||
|
||||
// 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 !"))
|
||||
|
||||
// Get broadcaster user id and reward id
|
||||
let broadcaster_user_id = await Twitch.getUserInfo(client_id, channel_access_token, 'id') as string
|
||||
|
||||
let topics: { [key: string]: { version: string; condition: { broadcaster_user_id: string } } } = {
|
||||
'stream.online': { version: '1', condition: { broadcaster_user_id } },
|
||||
'stream.offline': { version: '1', condition: { broadcaster_user_id } }
|
||||
}
|
||||
|
||||
// Subscribe to all events required
|
||||
for (let type in topics) {
|
||||
console.log(chalk.magenta(`[Twitch] 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}`))
|
||||
}
|
||||
}
|
||||
|
||||
// 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)))
|
||||
.on('close', () => {
|
||||
console.log(chalk.magenta('[Twitch] EventSub Connection Closed !'))
|
||||
twitch.connect('wss://eventsub.wss.twitch.tv/ws')
|
||||
})
|
||||
}).on('connectFailed', error => console.error(chalk.magenta('[Twitch] ' + error)))
|
||||
|
||||
// LAUNCH TWITCH EVENTSUB
|
||||
twitch.connect('wss://eventsub.wss.twitch.tv/ws')
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user