Skip to content

Commit

Permalink
added basic gui
Browse files Browse the repository at this point in the history
command fixes
  • Loading branch information
jagrosh committed Jan 2, 2017
1 parent a66af36 commit 474cc1e
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 39 deletions.
101 changes: 83 additions & 18 deletions src/main/java/me/jagrosh/jmusicbot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import me.jagrosh.jdautilities.commandclient.CommandEvent;
import me.jagrosh.jdautilities.waiter.EventWaiter;
import me.jagrosh.jmusicbot.audio.AudioHandler;
import me.jagrosh.jmusicbot.gui.GUI;
import me.jagrosh.jmusicbot.utils.FormatUtil;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.events.ShutdownEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import net.dv8tion.jda.core.utils.PermissionUtil;
Expand All @@ -54,6 +57,9 @@ public class Bot extends ListenerAdapter {
private final AudioPlayerManager manager;
private final EventWaiter waiter;
private final ScheduledExecutorService threadpool;
private JDA jda;
private GUI gui;
//private GuildsPanel panel;
public final Category MUSIC = new Category("Music");
public final Category DJ = new Category("DJ", event ->
{
Expand Down Expand Up @@ -117,7 +123,7 @@ public AudioHandler setUpHandler(CommandEvent event)
handler = new AudioHandler(player, event.getGuild());
player.addListener(handler);
event.getGuild().getAudioManager().setSendingHandler(handler);
startTopicUpdater(event.getGuild(), handler);
threadpool.scheduleWithFixedDelay(() -> updateTopic(event.getGuild(),handler), 0, 5, TimeUnit.SECONDS);
}
else
{
Expand All @@ -126,32 +132,59 @@ public AudioHandler setUpHandler(CommandEvent event)
return handler;
}

private void startTopicUpdater(Guild guild, AudioHandler handler)
private void updateTopic(Guild guild, AudioHandler handler)
{
threadpool.scheduleWithFixedDelay(() -> {
TextChannel tchan = guild.getTextChannelById(getSettings(guild).getTextId());
if(tchan!=null && PermissionUtil.checkPermission(tchan, guild.getSelfMember(), Permission.MANAGE_CHANNEL))
TextChannel tchan = guild.getTextChannelById(getSettings(guild).getTextId());
if(tchan!=null && PermissionUtil.checkPermission(tchan, guild.getSelfMember(), Permission.MANAGE_CHANNEL))
{
String otherText;
if(tchan.getTopic()==null || tchan.getTopic().isEmpty())
otherText = "";
else if(tchan.getTopic().contains("\u200B"))
otherText = tchan.getTopic().substring(tchan.getTopic().indexOf("\u200B")).trim();
else
otherText = "\n\u200B "+tchan.getTopic();
String text = FormatUtil.formattedAudio(handler, guild.getJDA(), true)+otherText;
if(!text.equals(tchan.getTopic()))
tchan.getManager().setTopic(text).queue();
}
}

public void shutdown(){
manager.shutdown();
threadpool.shutdownNow();
jda.getGuilds().stream().forEach(g -> {
g.getAudioManager().closeAudioConnection();
AudioHandler ah = (AudioHandler)g.getAudioManager().getSendingHandler();
if(ah!=null)
{
String otherText;
if(tchan.getTopic()==null || tchan.getTopic().isEmpty())
otherText = "";
else if(tchan.getTopic().contains("\u200B"))
otherText = tchan.getTopic().substring(tchan.getTopic().indexOf("\u200B")).trim();
else
otherText = "\n\u200B"+tchan.getTopic();
String text = FormatUtil.formattedAudio(handler, guild.getJDA(), true)+otherText;
if(!text.equals(tchan.getTopic()))
tchan.getManager().setTopic(text).queue();
ah.getQueue().clear();
ah.getPlayer().destroy();
updateTopic(g, ah);
}
}, 0, 5, TimeUnit.SECONDS);
});
jda.shutdown();
}

public void setGUI(GUI gui)
{
this.gui = gui;
}

@Override
public void onShutdown(ShutdownEvent event) {
manager.shutdown();
threadpool.shutdown();
if(gui!=null)
gui.dispose();
}

@Override
public void onReady(ReadyEvent event) {
this.jda = event.getJDA();
//if(panel!=null)
// panel.updateList(event.getJDA().getGuilds());
}


// settings

public Settings getSettings(Guild guild)
Expand Down Expand Up @@ -260,4 +293,36 @@ private void writeSettings()
SimpleLog.getLog("Settings").warn("Failed to write to file: "+ex);
}
}

//gui stuff
/*public void registerPanel(GuildsPanel panel)
{
this.panel = panel;
threadpool.scheduleWithFixedDelay(() -> updatePanel(), 0, 5, TimeUnit.SECONDS);
}
public void updatePanel()
{
System.out.println("updating...");
Guild guild = jda.getGuilds().get(panel.getIndex());
panel.updatePanel((AudioHandler)guild.getAudioManager().getSendingHandler());
}
@Override
public void onGuildJoin(GuildJoinEvent event) {
if(panel!=null)
panel.updateList(event.getJDA().getGuilds());
}
@Override
public void onGuildLeave(GuildLeaveEvent event) {
if(panel!=null)
panel.updateList(event.getJDA().getGuilds());
}
@Override
public void onShutdown(ShutdownEvent event) {
((GUI)panel.getTopLevelAncestor()).dispose();
}*/

}
18 changes: 11 additions & 7 deletions src/main/java/me/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@

import java.awt.Color;
import javax.security.auth.login.LoginException;
import javax.swing.JOptionPane;
import me.jagrosh.jdautilities.commandclient.CommandClient;
import me.jagrosh.jdautilities.commandclient.CommandClientBuilder;
import me.jagrosh.jdautilities.commandclient.examples.*;
import me.jagrosh.jdautilities.waiter.EventWaiter;
import me.jagrosh.jmusicbot.commands.*;
import me.jagrosh.jmusicbot.gui.GUI;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import net.dv8tion.jda.core.utils.SimpleLog;

/**
*
Expand Down Expand Up @@ -85,6 +86,14 @@ public static void main(String[] args){
new ShutdownCmd(bot)
).build();


if(!nogui)
{
GUI gui = new GUI(bot);
bot.setGUI(gui);
gui.init();
}

// attempt to log in and start
try {
new JDABuilder(AccountType.BOT)
Expand All @@ -97,12 +106,7 @@ public static void main(String[] args){
.addListener(bot)
.buildAsync();
} catch (LoginException | IllegalArgumentException | RateLimitedException ex) {
if(nogui)
System.out.println("[ERROR] Could not log in: "+ex);
else
JOptionPane.showMessageDialog(null, "Could not log in:\n"+ex, "JMusicBot", JOptionPane.ERROR_MESSAGE);
System.exit(1);
SimpleLog.getLog("Login").fatal(ex);
}

}
}
1 change: 0 additions & 1 deletion src/main/java/me/jagrosh/jmusicbot/audio/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import me.jagrosh.jmusicbot.queue.FairQueue;
import net.dv8tion.jda.core.audio.AudioSendHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/jagrosh/jmusicbot/commands/QueueCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void doCommand(CommandEvent event) {
total += list.get(i).getTrack().getDuration();
songs[i] = list.get(i).toString();
}
builder.setText(event.getClient().getSuccess()+" Current Queue | "+songs.length+" entries | `"+FormatUtil.formatTime(total)+"` ~ ")
builder.setText(event.getClient().getSuccess()+" Current Queue | "+songs.length+" entries | `"+FormatUtil.formatTime(total)+"` ")
.setItems(songs)
.setUsers(event.getAuthor())
.setColor(event.getSelfMember().getColor())
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/jagrosh/jmusicbot/commands/SearchCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import me.jagrosh.jdautilities.menu.orderedmenu.OrderedMenuBuilder;
import me.jagrosh.jmusicbot.Bot;
import me.jagrosh.jmusicbot.utils.FormatUtil;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Message;

/**
Expand All @@ -42,6 +43,7 @@ public SearchCmd(Bot bot)
this.help = "searches Youtube for a provided query";
this.beListening = true;
this.bePlaying = false;
this.botPermissions = new Permission[]{Permission.MESSAGE_EMBED_LINKS};
builder = new OrderedMenuBuilder()
.allowTextInput(true)
.useNumbers()
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/me/jagrosh/jmusicbot/commands/ShutdownCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
import me.jagrosh.jdautilities.commandclient.Command;
import me.jagrosh.jdautilities.commandclient.CommandEvent;
import me.jagrosh.jmusicbot.Bot;
import me.jagrosh.jmusicbot.audio.AudioHandler;

/**
*
* @author John Grosh <john.a.grosh@gmail.com>
*/
public class ShutdownCmd extends Command {

private final Bot bot;
public ShutdownCmd(Bot bot)
{
this.bot = bot;
this.name = "shutdown";
this.help = "safely shuts down";
this.ownerCommand = true;
Expand All @@ -37,12 +38,7 @@ public ShutdownCmd(Bot bot)
@Override
protected void execute(CommandEvent event) {
event.reply(event.getClient().getWarning()+" Shutting down...");
event.getJDA().getGuilds().stream()
.forEach(g -> g.getAudioManager().closeAudioConnection());
event.getJDA().getGuilds().stream()
.filter(g -> g.getAudioManager().getSendingHandler()!=null)
.forEach(g -> ((AudioHandler)g.getAudioManager().getSendingHandler()).getPlayer().destroy());
event.getJDA().shutdown();
bot.shutdown();
}

}
2 changes: 1 addition & 1 deletion src/main/java/me/jagrosh/jmusicbot/commands/StopCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public StopCmd(Bot bot)
@Override
public void doCommand(CommandEvent event) {
AudioHandler handler = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler();
handler.getPlayer().stopTrack();
handler.getQueue().clear();
handler.getPlayer().stopTrack();
event.getGuild().getAudioManager().closeAudioConnection();
event.reply(event.getClient().getSuccess()+" The player has stopped and the queue has been cleared.");
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/me/jagrosh/jmusicbot/gui/ConsolePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2017 John Grosh <john.a.grosh@gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.jagrosh.jmusicbot.gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.PrintStream;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
*
* @author John Grosh <john.a.grosh@gmail.com>
*/
public class ConsolePanel extends JPanel {

public ConsolePanel()
{
super();
JTextArea text = new JTextArea();
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.setEditable(false);
PrintStream con=new PrintStream(new TextAreaOutputStream(text));
System.setOut(con);
System.setErr(con);

JScrollPane pane = new JScrollPane();
pane.setViewportView(text);

super.setLayout(new GridLayout(1,1));
super.add(pane);
super.setPreferredSize(new Dimension(400,300));
}
}
26 changes: 23 additions & 3 deletions src/main/java/me/jagrosh/jmusicbot/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package me.jagrosh.jmusicbot.gui;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
import me.jagrosh.jmusicbot.Bot;


/**
Expand All @@ -26,19 +29,36 @@
*/
public class GUI extends JFrame {

private final ConsolePanel console;
private final GuildsPanel guilds;
private final Bot bot;

public GUI() {
public GUI(Bot bot) {
super();
init();
this.bot = bot;
console = new ConsolePanel();
guilds = new GuildsPanel(bot);
}

private void init()
public void init()
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("JMusicBot");
JTabbedPane tabs = new JTabbedPane();
//tabs.add("Guilds", guilds);
tabs.add("Console", console);
getContentPane().add(tabs);
pack();
setLocationRelativeTo(null);
setVisible(true);
addWindowListener(new WindowListener() {
@Override public void windowOpened(WindowEvent e) {}
@Override public void windowClosing(WindowEvent e) {bot.shutdown();}
@Override public void windowClosed(WindowEvent e) {}
@Override public void windowIconified(WindowEvent e) {}
@Override public void windowDeiconified(WindowEvent e) {}
@Override public void windowActivated(WindowEvent e) {}
@Override public void windowDeactivated(WindowEvent e) {}
});
}
}
Loading

0 comments on commit 474cc1e

Please sign in to comment.