3131import me .jagrosh .jdautilities .commandclient .CommandEvent ;
3232import me .jagrosh .jdautilities .waiter .EventWaiter ;
3333import me .jagrosh .jmusicbot .audio .AudioHandler ;
34+ import me .jagrosh .jmusicbot .gui .GUI ;
3435import me .jagrosh .jmusicbot .utils .FormatUtil ;
36+ import net .dv8tion .jda .core .JDA ;
3537import net .dv8tion .jda .core .Permission ;
3638import net .dv8tion .jda .core .entities .Guild ;
3739import net .dv8tion .jda .core .entities .Role ;
3840import net .dv8tion .jda .core .entities .TextChannel ;
3941import net .dv8tion .jda .core .entities .VoiceChannel ;
42+ import net .dv8tion .jda .core .events .ReadyEvent ;
4043import net .dv8tion .jda .core .events .ShutdownEvent ;
4144import net .dv8tion .jda .core .hooks .ListenerAdapter ;
4245import net .dv8tion .jda .core .utils .PermissionUtil ;
@@ -54,6 +57,9 @@ public class Bot extends ListenerAdapter {
5457 private final AudioPlayerManager manager ;
5558 private final EventWaiter waiter ;
5659 private final ScheduledExecutorService threadpool ;
60+ private JDA jda ;
61+ private GUI gui ;
62+ //private GuildsPanel panel;
5763 public final Category MUSIC = new Category ("Music" );
5864 public final Category DJ = new Category ("DJ" , event ->
5965 {
@@ -117,7 +123,7 @@ public AudioHandler setUpHandler(CommandEvent event)
117123 handler = new AudioHandler (player , event .getGuild ());
118124 player .addListener (handler );
119125 event .getGuild ().getAudioManager ().setSendingHandler (handler );
120- startTopicUpdater ( event .getGuild (), handler );
126+ threadpool . scheduleWithFixedDelay (() -> updateTopic ( event .getGuild (),handler ), 0 , 5 , TimeUnit . SECONDS );
121127 }
122128 else
123129 {
@@ -126,32 +132,59 @@ public AudioHandler setUpHandler(CommandEvent event)
126132 return handler ;
127133 }
128134
129- private void startTopicUpdater (Guild guild , AudioHandler handler )
135+ private void updateTopic (Guild guild , AudioHandler handler )
130136 {
131- threadpool .scheduleWithFixedDelay (() -> {
132- TextChannel tchan = guild .getTextChannelById (getSettings (guild ).getTextId ());
133- if (tchan !=null && PermissionUtil .checkPermission (tchan , guild .getSelfMember (), Permission .MANAGE_CHANNEL ))
137+ TextChannel tchan = guild .getTextChannelById (getSettings (guild ).getTextId ());
138+ if (tchan !=null && PermissionUtil .checkPermission (tchan , guild .getSelfMember (), Permission .MANAGE_CHANNEL ))
139+ {
140+ String otherText ;
141+ if (tchan .getTopic ()==null || tchan .getTopic ().isEmpty ())
142+ otherText = "" ;
143+ else if (tchan .getTopic ().contains ("\u200B " ))
144+ otherText = tchan .getTopic ().substring (tchan .getTopic ().indexOf ("\u200B " )).trim ();
145+ else
146+ otherText = "\n \u200B " +tchan .getTopic ();
147+ String text = FormatUtil .formattedAudio (handler , guild .getJDA (), true )+otherText ;
148+ if (!text .equals (tchan .getTopic ()))
149+ tchan .getManager ().setTopic (text ).queue ();
150+ }
151+ }
152+
153+ public void shutdown (){
154+ manager .shutdown ();
155+ threadpool .shutdownNow ();
156+ jda .getGuilds ().stream ().forEach (g -> {
157+ g .getAudioManager ().closeAudioConnection ();
158+ AudioHandler ah = (AudioHandler )g .getAudioManager ().getSendingHandler ();
159+ if (ah !=null )
134160 {
135- String otherText ;
136- if (tchan .getTopic ()==null || tchan .getTopic ().isEmpty ())
137- otherText = "" ;
138- else if (tchan .getTopic ().contains ("\u200B " ))
139- otherText = tchan .getTopic ().substring (tchan .getTopic ().indexOf ("\u200B " )).trim ();
140- else
141- otherText = "\n \u200B " +tchan .getTopic ();
142- String text = FormatUtil .formattedAudio (handler , guild .getJDA (), true )+otherText ;
143- if (!text .equals (tchan .getTopic ()))
144- tchan .getManager ().setTopic (text ).queue ();
161+ ah .getQueue ().clear ();
162+ ah .getPlayer ().destroy ();
163+ updateTopic (g , ah );
145164 }
146- }, 0 , 5 , TimeUnit .SECONDS );
165+ });
166+ jda .shutdown ();
147167 }
148168
169+ public void setGUI (GUI gui )
170+ {
171+ this .gui = gui ;
172+ }
173+
149174 @ Override
150175 public void onShutdown (ShutdownEvent event ) {
151- manager .shutdown ();
152- threadpool .shutdown ();
176+ if (gui !=null )
177+ gui .dispose ();
178+ }
179+
180+ @ Override
181+ public void onReady (ReadyEvent event ) {
182+ this .jda = event .getJDA ();
183+ //if(panel!=null)
184+ // panel.updateList(event.getJDA().getGuilds());
153185 }
154186
187+
155188 // settings
156189
157190 public Settings getSettings (Guild guild )
@@ -260,4 +293,36 @@ private void writeSettings()
260293 SimpleLog .getLog ("Settings" ).warn ("Failed to write to file: " +ex );
261294 }
262295 }
296+
297+ //gui stuff
298+ /*public void registerPanel(GuildsPanel panel)
299+ {
300+ this.panel = panel;
301+ threadpool.scheduleWithFixedDelay(() -> updatePanel(), 0, 5, TimeUnit.SECONDS);
302+ }
303+
304+ public void updatePanel()
305+ {
306+ System.out.println("updating...");
307+ Guild guild = jda.getGuilds().get(panel.getIndex());
308+ panel.updatePanel((AudioHandler)guild.getAudioManager().getSendingHandler());
309+ }
310+
311+ @Override
312+ public void onGuildJoin(GuildJoinEvent event) {
313+ if(panel!=null)
314+ panel.updateList(event.getJDA().getGuilds());
315+ }
316+
317+ @Override
318+ public void onGuildLeave(GuildLeaveEvent event) {
319+ if(panel!=null)
320+ panel.updateList(event.getJDA().getGuilds());
321+ }
322+
323+ @Override
324+ public void onShutdown(ShutdownEvent event) {
325+ ((GUI)panel.getTopLevelAncestor()).dispose();
326+ }*/
327+
263328}
0 commit comments