Skip to content

Commit

Permalink
Major changes and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LXGaming committed Sep 15, 2019
1 parent 04ccda7 commit d9fb653
Show file tree
Hide file tree
Showing 43 changed files with 980 additions and 785 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
apply plugin: "java"
apply plugin: "signing"
plugins {
id "java"
id "signing"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
59 changes: 31 additions & 28 deletions src/main/java/io/github/lxgaming/discordmusic/DiscordMusic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import io.github.lxgaming.discordmusic.configuration.Config;
import io.github.lxgaming.discordmusic.configuration.Configuration;
import io.github.lxgaming.discordmusic.configuration.category.GeneralCategory;
import io.github.lxgaming.discordmusic.manager.AccountManager;
import io.github.lxgaming.discordmusic.manager.AudioManager;
import io.github.lxgaming.discordmusic.manager.CommandManager;
import io.github.lxgaming.discordmusic.manager.MessageManager;
import io.github.lxgaming.discordmusic.manager.ServiceManager;
import io.github.lxgaming.discordmusic.service.MessageService;
import io.github.lxgaming.discordmusic.util.Reference;
import io.github.lxgaming.discordmusic.util.ShutdownHook;
import io.github.lxgaming.discordmusic.util.Toolbox;
Expand All @@ -30,7 +33,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;

import java.nio.file.Path;
import java.time.Instant;
import java.util.Optional;

Expand All @@ -39,42 +41,51 @@ public class DiscordMusic {
private static DiscordMusic instance;
private final Instant startTime;
private final Logger logger;
private final Path path;
private final Configuration configuration;

public DiscordMusic() {
instance = this;
startTime = Instant.now();
logger = LogManager.getLogger(Reference.APP_ID);
path = Toolbox.getPath().orElse(null);
configuration = new Configuration();
this.startTime = Instant.now();
this.logger = LogManager.getLogger(Reference.NAME);
this.configuration = new Configuration(Toolbox.getPath());
}

public void loadDiscordMusic() {
getLogger().info("Initializing...");
public void load() {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
getLogger().info("Initializing...");
if (!reload()) {
getLogger().error("Failed to load");
return;
}

getLogger().info("Loading configuration...");
getConfiguration().loadConfiguration();
AccountManager.prepare();
AudioManager.prepare();
CommandManager.prepare();
MessageManager.prepare();
ServiceManager.prepare();

reloadLogger();
AccountManager.buildAccount();
AccountManager.reloadAccount();
CommandManager.buildCommands();
MessageManager.buildColors();
ServiceManager.buildServices();
getConfiguration().saveConfiguration();
getLogger().info("{} v{} has loaded", Reference.APP_NAME, Reference.APP_VERSION);

ServiceManager.schedule(new MessageService());
getLogger().info("{} v{} has loaded", Reference.NAME, Reference.VERSION);
}

public void reloadLogger() {
if (getConfig().map(Config::isDebug).orElse(false)) {
public boolean reload() {
getConfiguration().loadConfiguration();
if (!getConfig().isPresent()) {
return false;
}

getConfiguration().saveConfiguration();
if (getConfig().map(Config::getGeneralCategory).map(GeneralCategory::isDebug).orElse(false)) {
Configurator.setLevel(getLogger().getName(), Level.DEBUG);
getLogger().debug("Debug mode enabled.");
} else {
Configurator.setLevel(getLogger().getName(), Level.INFO);
getLogger().info("Debug mode disabled.");
}

return true;
}

public static DiscordMusic getInstance() {
Expand All @@ -89,19 +100,11 @@ public Logger getLogger() {
return logger;
}

public Path getPath() {
return path;
}

public Configuration getConfiguration() {
return configuration;
}

public Optional<Config> getConfig() {
if (getConfiguration() != null) {
return Optional.ofNullable(getConfiguration().getConfig());
}

return Optional.empty();
return Optional.ofNullable(getConfiguration().getConfig());
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/github/lxgaming/discordmusic/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public class Main {
public static void main(String[] args) {
Thread.currentThread().setName("Main Thread");
DiscordMusic discordMusic = new DiscordMusic();
discordMusic.loadDiscordMusic();
discordMusic.load();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ public ClearCommand() {
@Override
public void execute(Message message, List<String> arguments) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setAuthor(message.getJDA().getSelfUser().getName(), null, message.getJDA().getSelfUser().getEffectiveAvatarUrl());
embedBuilder.setColor(MessageManager.getColor(Color.DEFAULT));

BlockingQueue<AudioTrack> audioQueue = AudioManager.getAudioQueue(message.getGuild());
if (audioQueue == null || audioQueue.isEmpty()) {
embedBuilder.setColor(MessageManager.getColor(Color.WARNING));
embedBuilder.setTitle("Nothing queued");
if (audioQueue == null) {
embedBuilder.setColor(MessageManager.getColor(Color.ERROR));
embedBuilder.setTitle("AudioQueue is unavailable");
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
return;
}

audioQueue.clear();
AudioManager.playNext(message.getGuild());
embedBuilder.setColor(MessageManager.getColor(Color.SUCCESS));
embedBuilder.setTitle("Queue cleared");
if (!audioQueue.isEmpty()) {
audioQueue.clear();
AudioManager.playNext(message.getGuild());
embedBuilder.setColor(MessageManager.getColor(Color.SUCCESS));
embedBuilder.setTitle("Queue cleared");
} else {
embedBuilder.setColor(MessageManager.getColor(Color.WARNING));
embedBuilder.setTitle("Nothing queued");
}

MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import io.github.lxgaming.discordmusic.DiscordMusic;
import io.github.lxgaming.discordmusic.configuration.Config;
import io.github.lxgaming.discordmusic.configuration.category.GeneralCategory;
import io.github.lxgaming.discordmusic.data.Color;
import io.github.lxgaming.discordmusic.manager.MessageManager;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;

import java.util.List;
import java.util.Optional;

public class DebugCommand extends AbstractCommand {

Expand All @@ -37,32 +37,35 @@ public DebugCommand() {
@Override
public void execute(Message message, List<String> arguments) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setAuthor(message.getJDA().getSelfUser().getName(), null, message.getJDA().getSelfUser().getEffectiveAvatarUrl());
embedBuilder.setColor(MessageManager.getColor(Color.DEFAULT));

Optional<Config> config = DiscordMusic.getInstance().getConfig();
if (!config.isPresent()) {
if (!arguments.isEmpty()) {
embedBuilder.setColor(MessageManager.getColor(Color.ERROR));
embedBuilder.setTitle("Configuration error");
embedBuilder.setTitle("Invalid arguments");
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
return;
}

if (arguments.isEmpty()) {
if (config.get().isDebug()) {
config.get().setDebug(false);
DiscordMusic.getInstance().reloadLogger();
embedBuilder.setColor(MessageManager.getColor(Color.WARNING));
embedBuilder.setTitle("Debugging disabled");
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
return;
}

config.get().setDebug(true);
DiscordMusic.getInstance().reloadLogger();
GeneralCategory generalCategory = DiscordMusic.getInstance().getConfig().map(Config::getGeneralCategory).orElse(null);
if (generalCategory == null) {
embedBuilder.setColor(MessageManager.getColor(Color.ERROR));
embedBuilder.setTitle("GeneralCategory is unavailable");
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
return;
}

if (generalCategory.isDebug()) {
generalCategory.setDebug(false);
embedBuilder.setColor(MessageManager.getColor(Color.WARNING));
embedBuilder.setTitle("Debugging disabled");
} else {
generalCategory.setDebug(true);
embedBuilder.setColor(MessageManager.getColor(Color.SUCCESS));
embedBuilder.setTitle("Debugging enabled");
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
}

DiscordMusic.getInstance().getConfiguration().saveConfiguration();
DiscordMusic.getInstance().reload();

MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package io.github.lxgaming.discordmusic.command;

import io.github.lxgaming.discordmusic.DiscordMusic;
import io.github.lxgaming.discordmusic.configuration.Config;
import io.github.lxgaming.discordmusic.configuration.category.GeneralCategory;
import io.github.lxgaming.discordmusic.data.Color;
import io.github.lxgaming.discordmusic.manager.CommandManager;
import io.github.lxgaming.discordmusic.manager.GroupManager;
Expand All @@ -26,13 +29,13 @@
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class HelpCommand extends AbstractCommand {

public HelpCommand() {
addAlias("help");
addAlias("?");
setDescription("Displays helpful information.");
setPermission("help.base");
setUsage("[Command]");
Expand All @@ -44,7 +47,7 @@ public void execute(Message message, List<String> arguments) {
embedBuilder.setColor(MessageManager.getColor(Color.DEFAULT));

if (arguments.isEmpty()) {
for (AbstractCommand command : CommandManager.getCommands()) {
for (AbstractCommand command : CommandManager.COMMANDS) {
if (command.getAliases().isEmpty() || !GroupManager.hasPermission(message.getMember(), command.getPermission())) {
continue;
}
Expand All @@ -60,49 +63,59 @@ public void execute(Message message, List<String> arguments) {
}

embedBuilder.setTitle("Help: Index");
embedBuilder.setFooter("<> = Required Argument, [] = Optional Argument", null);
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
return;
}

Optional<AbstractCommand> command = CommandManager.getCommand(Toolbox.newArrayList(arguments.toArray(new String[0])));
if (!command.isPresent()) {
embedBuilder.setColor(MessageManager.getColor(Color.ERROR));
embedBuilder.setTitle("No help present for " + StringUtils.join(arguments, " "));
List<String> childArguments = Toolbox.newArrayList(arguments.toArray(new String[0]));
AbstractCommand command = CommandManager.getCommand(childArguments).orElse(null);
if (command == null) {
embedBuilder.setColor(MessageManager.getColor(Color.WARNING));
embedBuilder.setTitle("Unknown command");
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
return;
}

if (!GroupManager.hasPermission(message.getMember(), command.get().getPermission())) {
int index = (arguments.size() - childArguments.size());
while (index < arguments.size()) {
arguments.remove(index);
}

if (!GroupManager.hasPermission(message.getMember(), command.getPermission())) {
embedBuilder.setColor(MessageManager.getColor(Color.WARNING));
embedBuilder.setTitle("You do not have permission to view this command");
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
return;
}

embedBuilder.setTitle("Help: " + StringUtils.capitalize(command.get().getPrimaryAlias().orElse("Unknown")));
if (command.get().getAliases().size() > 1) {
embedBuilder.getDescriptionBuilder().append("**Aliases**: ").append(command.get().getAliases().stream().skip(1).collect(Collectors.joining(", ")));
embedBuilder.setTitle("Help: " + StringUtils.capitalize(command.getPrimaryAlias().orElse("Unknown")));
if (command.getAliases().size() > 1) {
embedBuilder.getDescriptionBuilder().append("**Aliases**: ").append(command.getAliases().stream().skip(1).collect(Collectors.joining(", ")));
embedBuilder.getDescriptionBuilder().append("\n");
}

List<String> children = Toolbox.newArrayList();
for (AbstractCommand childCommand : command.get().getChildren()) {
for (AbstractCommand childCommand : command.getChildren()) {
children.add(childCommand.getPrimaryAlias().orElse("Unknown"));
}

if (!children.isEmpty()) {
embedBuilder.getDescriptionBuilder().append("**Children**: ").append(StringUtils.join(children, ", "));
embedBuilder.getDescriptionBuilder().append("**Children**: ").append(String.join(", ", children));
embedBuilder.getDescriptionBuilder().append("\n");
}

embedBuilder.getDescriptionBuilder().append("**Description**: ");
embedBuilder.getDescriptionBuilder().append(StringUtils.defaultIfBlank(command.get().getDescription(), "No description provided.")).append("\n");
embedBuilder.getDescriptionBuilder().append("**Usage**: ").append(StringUtils.join(arguments, " "));
if (StringUtils.isNotBlank(command.get().getUsage())) {
embedBuilder.getDescriptionBuilder().append(" ").append(command.get().getUsage());
embedBuilder.getDescriptionBuilder().append(StringUtils.defaultIfBlank(command.getDescription(), "No description provided.")).append("\n");
embedBuilder.getDescriptionBuilder().append("**Usage**: ")
.append(DiscordMusic.getInstance().getConfig().map(Config::getGeneralCategory).map(GeneralCategory::getCommandPrefix).orElse("/"))
.append(" ")
.append(String.join(" ", arguments));

if (StringUtils.isNotBlank(command.getUsage())) {
embedBuilder.getDescriptionBuilder().append(" ").append(command.getUsage());
}

embedBuilder.setFooter("<> = Required Argument, [] = Optional Argument", null);
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.github.lxgaming.discordmusic.command;

import com.jagrosh.jdautilities.commons.JDAUtilitiesInfo;
import com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary;
import io.github.lxgaming.discordmusic.DiscordMusic;
import io.github.lxgaming.discordmusic.data.Color;
Expand All @@ -42,14 +43,15 @@ public InfoCommand() {
@Override
public void execute(Message message, List<String> arguments) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setAuthor(Reference.APP_NAME + " v" + Reference.APP_VERSION, Reference.SOURCE, message.getJDA().getSelfUser().getEffectiveAvatarUrl());
embedBuilder.setAuthor(Reference.NAME + " v" + Reference.VERSION, Reference.SOURCE, message.getJDA().getSelfUser().getEffectiveAvatarUrl());
embedBuilder.setColor(MessageManager.getColor(Color.DEFAULT));
embedBuilder.addField("Uptime", Toolbox.getTimeString(Duration.between(DiscordMusic.getInstance().getStartTime(), Instant.now()).toMillis()), false);
embedBuilder.addField("Authors", Reference.AUTHORS, false);
embedBuilder.addField("Source", Reference.SOURCE, false);
embedBuilder.addField("Website", Reference.WEBSITE, false);
embedBuilder.addField("Dependencies", ""
+ "\n- " + "JDA (Java Discord API) v" + JDAInfo.VERSION
+ "\n- " + "JDA v" + JDAInfo.VERSION
+ "\n- " + "JDA-Utilities v" + JDAUtilitiesInfo.VERSION
+ "\n- " + "LavaPlayer v" + PlayerLibrary.VERSION, false);
MessageManager.sendTemporaryMessage(message.getChannel(), embedBuilder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public JoinCommand() {
@Override
public void execute(Message message, List<String> arguments) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(MessageManager.getColor(Color.DEFAULT));

Set<VoiceChannel> voiceChannels = Toolbox.newLinkedHashSet();
if (arguments.isEmpty()) {
Expand Down
Loading

0 comments on commit d9fb653

Please sign in to comment.