@@ -15,6 +15,7 @@ import {
1515  joinVoiceChannel , 
1616  NoSubscriberBehavior , 
1717  StreamType , 
18+   VoiceConnection , 
1819  VoiceConnectionStatus 
1920}  from  '@discordjs/voice' ; 
2021import  prism  from  'prism-media' ; 
@@ -95,7 +96,20 @@ async function setMuteAllPlayers(interaction: Interaction, mute: boolean) {
9596  } 
9697} 
9798
99+ function  playSound ( input : string ,  connection : VoiceConnection )  { 
100+   const  player  =  createAudioPlayer ( { 
101+     behaviors : { 
102+       noSubscriber : NoSubscriberBehavior . Play , 
103+     } , 
104+   } ) ; 
105+   const  resource  =  createAudioResource ( input ) ; 
106+   player . play ( resource ) ; 
107+   connection . subscribe ( player ) ; 
108+ } 
109+ 
98110let  abortController : AbortController  |  null  =  null ; 
111+ let  watchConnection : VoiceConnection  |  null  =  null ; 
112+ let  playConnection : VoiceConnection  |  null  =  null ; 
99113
100114client . on ( 'interactionCreate' ,  async  ( interaction )  =>  { 
101115  if  ( interaction . isChatInputCommand ( ) )  { 
@@ -106,15 +120,15 @@ client.on('interactionCreate', async (interaction) => {
106120      } 
107121
108122      abortController  =  new  AbortController ( ) ; 
109-       const   watchConnection  =  joinVoiceChannel ( { 
123+       watchConnection  =  joinVoiceChannel ( { 
110124        group : 'speaker' , 
111125        channelId : process . env . WATCHING_CHANNEL_ID  as  string , 
112126        guildId : interaction . guild ?. id  as  string , 
113127        adapterCreator : client . guilds . cache . get ( interaction . guild ?. id  as  string ) ?. voiceAdapterCreator  as  DiscordGatewayAdapterCreator , 
114128        selfMute : true , 
115129        selfDeaf : false , 
116130      } ) ; 
117-       const   playConnection  =  joinVoiceChannel ( { 
131+       playConnection  =  joinVoiceChannel ( { 
118132        group : 'listener' , 
119133        channelId : process . env . PLAYING_CHANNEL_ID  as  string , 
120134        guildId : interaction . guild ?. id  as  string , 
@@ -137,6 +151,7 @@ client.on('interactionCreate', async (interaction) => {
137151
138152      const  receiver  =  playConnection . receiver ; 
139153      receiver . speaking . on ( 'start' ,  ( userId )  =>  { 
154+         if  ( watchConnection  ==  null )  return ; 
140155        const  standaloneInput  =  new  Input ( { 
141156          channels : 2 , 
142157          bitDepth : 16 , 
@@ -182,8 +197,10 @@ client.on('interactionCreate', async (interaction) => {
182197      await  interaction . reply ( 'VCを中継を開始しました' ) ; 
183198
184199      abortController . signal . addEventListener ( 'abort' ,  ( )  =>  { 
185-         playConnection . destroy ( ) ; 
186-         watchConnection . destroy ( ) ; 
200+         playConnection ?. destroy ( ) ; 
201+         playConnection  =  null ; 
202+         watchConnection ?. destroy ( ) ; 
203+         watchConnection  =  null ; 
187204      } ) ; 
188205    }  else  if  ( interaction . commandName  ===  'stop-sync' )  { 
189206      if  ( abortController  !=  null )  { 
@@ -194,10 +211,16 @@ client.on('interactionCreate', async (interaction) => {
194211    }  else  if  ( interaction . commandName  ===  'mute-all-players' )  { 
195212      await  interaction . deferReply ( ) ; 
196213      await  setMuteAllPlayers ( interaction ,  true ) ; 
214+       if  ( playConnection  !=  null )  { 
215+         playSound ( 'sounds/discord-mute.mp3' ,  playConnection ) ; 
216+       } 
197217      await  interaction . followUp ( 'ミュートを設定しました' ) ; 
198218    }  else  if  ( interaction . commandName  ===  'unmute-all-players' )  { 
199219      await  interaction . deferReply ( ) ; 
200220      await  setMuteAllPlayers ( interaction ,  false ) ; 
221+       if  ( playConnection  !=  null )  { 
222+         playSound ( 'sounds/discord-unmute.mp3' ,  playConnection ) ; 
223+       } 
201224      await  interaction . followUp ( 'ミュートを設定解除しました' ) ; 
202225    } 
203226  } 
0 commit comments