Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #19 from Team-Creative-Name/dev-branch
Browse files Browse the repository at this point in the history
Merge Dev Branch into Master - Bump version to v1.0-beta.5.3
  • Loading branch information
collectioncard authored Jun 28, 2020
2 parents fc4a40a + 57751b9 commit 57107a3
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 75 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'com.github.tcn.plexi'
version 'v1.0-beta.5.2'
version 'v1.0-beta.5.3'

sourceCompatibility = 1.8

Expand All @@ -17,7 +17,7 @@ dependencies {
compile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.code.gson:gson:2.8.6'
compile 'com.jagrosh:jda-utilities:3.0.4'
compile 'net.dv8tion:JDA:4.1.1_165'
compile 'net.dv8tion:JDA:4.1.1_166'
compile 'com.squareup.okhttp3:okhttp:4.2.2'
compile 'com.typesafe:config:1.4.0'
compile 'com.vdurmont:emoji-java:5.1.1'
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/github/tcn/plexi/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ public static void main(String[] args) {
Settings.getInstance();


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainView().setVisible(true);
SwingUtilities.invokeLater(() -> {
new MainView().setVisible(true);

System.out.println(logo);
System.out.println("\n\nPress the start button to start Plexi.");

}
System.out.println(logo);
System.out.println("\n\nPress the start button to start Plexi.");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

public class Commands extends CommandClientBuilder {


public void commandList(EventWaiter waiter) {
this.addCommand(new SearchCommand(waiter));
this.addCommand(new MediaInfoCommand());
this.addCommand(new RequestCommand());
this.addCommand(new GetMissingEpisodes());
this.addCommand(new ShutdownCommand());
this.addCommand(new RestartCommand());
this.addCommand(new getPingCommand());
}
}
14 changes: 13 additions & 1 deletion src/main/java/com/github/tcn/plexi/discordBot/EmbedManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,25 @@ public EmbedBuilder createMissingEpisodeEmbed(ArrayList<String> missingEpisodeAr
return eb;
}

public EmbedBuilder createPingEmbed(long gatewayPing, long discordPing, long ombiPing) {
EmbedBuilder eb = new EmbedBuilder();
eb.setColor(new Color(0x00Ae86));
eb.setTitle("Ping Times");
eb.setDescription("Current ping times for all enabled APIs");
eb.addField("Gateway", stringVerifier(gatewayPing + "ms", 5), true);
eb.addField("Discord", stringVerifier(String.valueOf(discordPing), 5) + "ms", true);
eb.addField("Ombi", stringVerifier(String.valueOf(ombiPing), 5) + "ms", true);

return eb;
}

//Methods that clean up information
//Field IDs-------------
// 1. Title
// 2. Description
// 3. TVDB ID
// 4. Original Network
// 5. Status - Original Language - URL
// 5. Status - Original Language - URL - number
// 6. Monitored
// 7. Footer
// 8. Date
Expand Down
70 changes: 50 additions & 20 deletions src/main/java/com/github/tcn/plexi/discordBot/PlexiBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,45 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;

import javax.security.auth.login.LoginException;
public class PlexiBot {

//globals

public class PlexiBot {
//reference to PlexiBot obj
private static PlexiBot botObj = null;
//reference to plexi object
private JDA botInstance = null;


//lock the constructor
private PlexiBot() {

}

//static methods
public static PlexiBot getInstance() {
//if there is no plexi obj, create one.
if (botObj == null) {
botObj = new PlexiBot();
}
//return the bot object
return botObj;
}

//reference to JDA - also used to determine if the bot is running
private static JDA bot = null;
//public methods
public JDA getJDAInstance() {
if (botInstance == null) {
return null;
}
return botInstance;
}

public boolean isRunning() {
return botInstance != null;
}

public static void startBot() throws LoginException {
//create Settings Object reference
public void startBot() {
//get instance of settings class
Settings settings = Settings.getInstance();

//Create Commands object
Expand Down Expand Up @@ -50,35 +78,37 @@ public static void startBot() throws LoginException {
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Error Starting Bot");
shutdownBot();

stopBot();
}
//set global botInstance obj to the newly created one
this.botInstance = botInstance;


bot = botInstance;
}

public static void shutdownBot() {
if (bot != null) {
bot.shutdown();
bot = null;
public void stopBot() {
//ensure that there is a bot instance running
if (botInstance != null) {
botInstance.shutdownNow();
//remove reference to other bot
botInstance = null;
System.out.println("Shutdown Complete");
} else {
System.out.println("Error while shutting down: There is no bot running!");
}

}

public static void restartBot(JDA botInstance) throws LoginException {
shutdownBot();
public void restartBot() {
stopBot();
startBot();
}

public static JDA getJDAInstance() {
return bot;
//methods to return discord ping times
public long getGatewayPing() {
return botInstance.getGatewayPing();
}

public static boolean isRunning() {
return bot != null;
public long getRestPing() {
return botInstance.getRestPing().complete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;

import javax.security.auth.login.LoginException;

public class RestartCommand extends Command {

Expand All @@ -18,14 +17,6 @@ public RestartCommand() {
@Override
protected void execute(CommandEvent event) {
event.reply("Plexi will be back in 1 second. Please wait.");
try {
PlexiBot.restartBot(event.getJDA());
} catch (LoginException e) {
System.out.println("ERROR RESTARTING JDA");
e.printStackTrace();
}

PlexiBot.getInstance().restartBot();
}


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.tcn.plexi.discordBot.commands;

import com.github.tcn.plexi.discordBot.PlexiBot;
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;

public class ShutdownCommand extends Command {
public ShutdownCommand() {
this.name = "shutdown";
this.aliases = new String[]{"stop"};
this.help = "safely shuts off the bot";
this.guildOnly = false;
this.ownerCommand = true;
Expand All @@ -14,10 +16,9 @@ public ShutdownCommand() {
@Override
protected void execute(CommandEvent event) {
event.reply("Plexi is shutting down.");
event.reactWarning();
event.getJDA().retrieveApplicationInfo().queue();


//turn off plexi
PlexiBot.getInstance().stopBot();
System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.tcn.plexi.discordBot.commands;

import com.github.tcn.plexi.discordBot.EmbedManager;
import com.github.tcn.plexi.discordBot.PlexiBot;
import com.github.tcn.plexi.ombi.OmbiCallers;
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;

public class getPingCommand extends Command {

public getPingCommand() {
this.name = "ping";
this.help = "Returns the time in ms that all of the apis took to respond to plexi";
this.guildOnly = false;
}

@Override
protected void execute(CommandEvent event) {
//create OmbiCaller obj
OmbiCallers caller = new OmbiCallers();
EmbedManager embedManager = new EmbedManager();
event.reply(embedManager.createPingEmbed(PlexiBot.getInstance().getGatewayPing(), PlexiBot.getInstance().getRestPing(), caller.getPingTime()).build());
}
}
52 changes: 36 additions & 16 deletions src/main/java/com/github/tcn/plexi/gui/MainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.github.tcn.plexi.discordBot.PlexiBot;
import com.github.tcn.plexi.settingsManager.Settings;

import javax.security.auth.login.LoginException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.PrintStream;

public class MainView extends JFrame {
Expand All @@ -20,6 +21,9 @@ public class MainView extends JFrame {
//get reference to settings obj
Settings settings = Settings.getInstance();

//get reference to plexi obj
PlexiBot botInstance = PlexiBot.getInstance();

public MainView() {
//set title of window
setTitle("Plexi " + settings.getVersionNumber());
Expand Down Expand Up @@ -56,20 +60,40 @@ public MainView() {

//add event handler for start button
buttonState.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent actionEvent) {

startStopButton();
}
});

//set default action to happen when closing window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

//register WindowListener for the window closing event
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//we only need to prompt the user for conformation if plexi is currently running
if (botInstance.isRunning()) {
int usrChoice = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit? This will stop plexi.");
if (usrChoice == 0) {
//we need to properly shut the bot down at this point
botInstance.stopBot();
} else {
//this means that the user decided to avoid shutdown. Print to log and return.
System.out.println("Avoided Shutdown");
return;
}
}
//shut down Jframe and exit program
super.windowClosing(e);
dispose();
System.exit(0);
}
});

//set window properties
setSize(400, 300);
setSize(400, 335);
setLocationRelativeTo(null);

}
Expand All @@ -78,26 +102,22 @@ private void startStopButton() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
if (PlexiBot.isRunning()) {
if (botInstance.isRunning()) {
//ask the user if they really want to shutdown
int choice = JOptionPane.showConfirmDialog(null, "Are you sure you want to shut down Plexi?");
int choice = JOptionPane.showConfirmDialog(null, "Are you sure you want to stop Plexi?");
if (choice == 0) {
PlexiBot.shutdownBot();
botInstance.stopBot();
//now that the bot is off, change button label
if (!PlexiBot.isRunning()) {
if (!botInstance.isRunning()) {
buttonState.setText("Start");
} else {
System.out.println("Error: Unable to stop bot");
}
}
} else {
try {
PlexiBot.startBot();
} catch (LoginException e) {
System.out.println("It died");
}
botInstance.startBot();
//now that the bot is on, change button label
if (PlexiBot.isRunning()) {
if (botInstance.isRunning()) {
buttonState.setText("Stop");
}
}
Expand Down
Loading

0 comments on commit 57107a3

Please sign in to comment.