@@ -6,7 +6,13 @@ const axios = require("axios");
66
77const ytdl = require ( "ytdl-core" ) ;
88const Discord = require ( "discord.js" ) ;
9- const client = new Discord . Client ( ) ;
9+ const {
10+ joinVoiceChannel,
11+ VoiceConnectionStatus,
12+ createAudioPlayer,
13+ createAudioResource,
14+ AudioPlayerStatus,
15+ } = require ( "@discordjs/voice" ) ;
1016
1117const { debugLog, discordToken, discordChannel } = require ( "../cliArgs" ) ;
1218const { findMember } = require ( "./discordUtil" ) ;
@@ -17,6 +23,13 @@ const {
1723 handleCommandBedtimeClear,
1824} = require ( "./bedtime" ) ;
1925
26+ const client = new Discord . Client ( {
27+ intents : [
28+ Discord . Intents . FLAGS . GUILD_MESSAGES ,
29+ Discord . Intents . FLAGS . GUILD_VOICE_STATES ,
30+ ] ,
31+ } ) ;
32+
2033/** @type TextChannel */
2134let mainChannel ;
2235let channels = { } ;
@@ -55,7 +68,7 @@ async function getChannel(id) {
5568 return channels [ id ] ;
5669}
5770
58- client . on ( "message " , async ( msg ) => {
71+ client . on ( "messageCreate " , async ( msg ) => {
5972 msg = /** @type Message */ msg ;
6073 if ( msg . author . id === client . user . id ) {
6174 return ;
@@ -99,13 +112,25 @@ client.on("message", async (msg) => {
99112 const playMatch = msg . content . match ( / \b p l a y \b ( h t t p .* y o u t u .* ) \b / i) ;
100113 if ( playMatch ) {
101114 const url = playMatch [ 1 ] ;
102- if ( msg . member . voice . channel ) {
103- const connection = await msg . member . voice . channel . join ( ) ;
104- const stream = connection . play ( ytdl ( url , { filter : "audioonly" } ) , {
105- volume : 0.5 ,
115+ if ( msg . member . voice . channelId ) {
116+ const connection = joinVoiceChannel ( {
117+ channelId : msg . member . voice . channelId ,
118+ guildId : msg . member . guild . id ,
119+ adapterCreator : msg . member . guild . voiceAdapterCreator ,
106120 } ) ;
107- stream . on ( "finish" , ( ) => {
108- connection . disconnect ( ) ;
121+
122+ connection . on ( VoiceConnectionStatus . Ready , ( ) => {
123+ const player = createAudioPlayer ( ) ;
124+ const ytStream = ytdl ( url , { filter : "audioonly" } ) ;
125+ const resource = createAudioResource ( ytStream , {
126+ inlineVolume : true ,
127+ } ) ;
128+ resource . volume . setVolume ( 0.5 ) ;
129+ connection . subscribe ( player ) ;
130+ player . play ( resource ) ;
131+ player . on ( AudioPlayerStatus . Idle , ( ) => {
132+ connection . destroy ( ) ;
133+ } ) ;
109134 } ) ;
110135 } else {
111136 msg . reply (
@@ -215,7 +240,7 @@ async function sendMessage(nickname, message, channelId) {
215240 const channel = channelId
216241 ? await getChannel ( channelId )
217242 : await getMainChannel ( ) ;
218- debugLog ( `Discord sending ${ nickname } : ${ message } ` ) ;
243+ debugLog ( `Discord sending ${ nickname } : ` , message ) ;
219244 if ( ! channel ) {
220245 console . warn ( "Discord: Not connected to channel" , channelId ) ;
221246 return ;
@@ -344,9 +369,13 @@ async function initPlayerStatusPoller(_knownGameApis) {
344369 if ( ! gameMessageMeta [ id ] ) {
345370 gameMessageMeta [ id ] = { } ;
346371 }
347- gameMessageMeta [ id ] . message = await mainChannel . messages . fetch (
348- storedMessages [ id ]
349- ) ;
372+ try {
373+ gameMessageMeta [ id ] . message = await mainChannel . messages . fetch (
374+ storedMessages [ id ]
375+ ) ;
376+ } catch ( e ) {
377+ console . warn ( "Discord: startup failed to read message" , id , e ) ;
378+ }
350379 } )
351380 ) ;
352381 }
@@ -355,18 +384,29 @@ async function initPlayerStatusPoller(_knownGameApis) {
355384 }
356385
357386 setInterval ( async ( ) => {
358- await Promise . all ( Object . values ( knownGameApis ) . map ( pollGameHealth ) ) ;
359- await fs . promises . writeFile (
360- savedMessagesFilePath ,
361- JSON . stringify (
362- Object . keys ( gameMessageMeta ) . reduce ( ( carry , id ) => {
363- if ( gameMessageMeta [ id ] . message ) {
364- carry [ id ] = gameMessageMeta [ id ] . message . id ;
365- }
366- return carry ;
367- } , { } )
368- )
369- ) ;
387+ try {
388+ await Promise . all ( Object . values ( knownGameApis ) . map ( pollGameHealth ) ) ;
389+ } catch ( err ) {
390+ console . warn ( "Discord PlayerStatusPoller error: " , err . message ) ;
391+ }
392+ try {
393+ await fs . promises . writeFile (
394+ savedMessagesFilePath ,
395+ JSON . stringify (
396+ Object . keys ( gameMessageMeta ) . reduce ( ( carry , id ) => {
397+ if ( gameMessageMeta [ id ] . message ) {
398+ carry [ id ] = gameMessageMeta [ id ] . message . id ;
399+ }
400+ return carry ;
401+ } , { } )
402+ )
403+ ) ;
404+ } catch ( err ) {
405+ console . warn (
406+ "Discord failed to write savedMessages error: " ,
407+ err . message
408+ ) ;
409+ }
370410 } , 8000 ) ;
371411}
372412
0 commit comments