change: clean up code and fix a few issues

This commit is contained in:
ZtereoHYPE 2022-05-10 13:10:20 +02:00
parent 4b9b92d54b
commit 2bbc6b9f1d
8 changed files with 32 additions and 25 deletions

View file

@ -52,7 +52,7 @@ public class ZtereoMUSIC {
EnumSet<GatewayIntent> intents = EnumSet.of(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_VOICE_STATES, GatewayIntent.GUILD_EMOJIS); EnumSet<GatewayIntent> intents = EnumSet.of(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_VOICE_STATES, GatewayIntent.GUILD_EMOJIS);
ztereoMUSIC.setConfig(Config.loadFrom("./config.json5")); ztereoMUSIC.setConfig(Config.loadFrom("./config.json5"));
ztereoMUSIC.setJda(JDABuilder.createDefault(ztereoMUSIC.getConfig().getPropreties().get("token"), intents) ztereoMUSIC.setJda(JDABuilder.createDefault(ztereoMUSIC.getConfig().getPropreties().getProperty("token"), intents)
.enableCache(CacheFlag.VOICE_STATE) .enableCache(CacheFlag.VOICE_STATE)
.build() .build()
.awaitReady()); .awaitReady());
@ -92,7 +92,7 @@ public class ZtereoMUSIC {
for (String commandName : this.getCommandMap().keySet()) { for (String commandName : this.getCommandMap().keySet()) {
for (String aliasName : this.getCommandMap().get(commandName).getMeta().getAliases()) { for (String aliasName : this.getCommandMap().get(commandName).getMeta().getAliases()) {
System.out.println("Loaded alias \"" + aliasName + "\" for command: " + commandName); // System.out.println("Loaded alias \"" + aliasName + "\" for command: " + commandName);
this.getCommandAliases().put(aliasName, commandName); this.getCommandAliases().put(aliasName, commandName);
} }
} }
@ -103,10 +103,12 @@ public class ZtereoMUSIC {
AudioSourceManagers.registerRemoteSources(this.getPlayerManager()); AudioSourceManagers.registerRemoteSources(this.getPlayerManager());
AudioSourceManagers.registerLocalSource(this.getPlayerManager()); AudioSourceManagers.registerLocalSource(this.getPlayerManager());
SpotifyApiHelper.startTokenTimer(); SpotifyApiHelper.startTokenTimer();
System.out.println("started audio");
} }
private void setListeners() { private void setListeners() {
this.getJda().addEventListener(new CommandListener()); this.getJda().addEventListener(new CommandListener());
this.getJda().addEventListener(new AloneDisconnectListener()); this.getJda().addEventListener(new AloneDisconnectListener());
System.out.println("started listeners");
} }
} }

View file

@ -22,8 +22,9 @@ public class CustomAudioLoadResultHandler implements AudioLoadResultHandler {
@Override public void playlistLoaded(AudioPlaylist playlist) { @Override public void playlistLoaded(AudioPlaylist playlist) {
for (AudioTrack track : playlist.getTracks()) { for (AudioTrack track : playlist.getTracks()) {
this.trackManager.queue(track); this.trackManager.getTrackQueue().add(track);
} }
this.messageChannel.sendMessage("Queued " + playlist.getTracks().size() + " songs from: " + playlist.getName()).queue();
} }
@Override public void noMatches() { @Override public void noMatches() {
@ -31,8 +32,7 @@ public class CustomAudioLoadResultHandler implements AudioLoadResultHandler {
} }
@Override public void loadFailed(FriendlyException throwable) { @Override public void loadFailed(FriendlyException throwable) {
this.messageChannel.sendMessage("Failed loading that audio. Try with a different source (eg. YouTube URL)") this.messageChannel.sendMessage("Failed loading that audio. Try with a different source (eg. YouTube URL)").queue();
.queue();
} }
} }

View file

@ -17,11 +17,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TrackManager extends AudioEventAdapter { public class TrackManager extends AudioEventAdapter {
public final List<AudioTrack> trackQueue = new ArrayList<>(); private final @Getter
private final @Getter AudioPlayer player; List<AudioTrack> trackQueue = new ArrayList<>();
private final @Getter
AudioPlayer player;
private String hasRetriedId; private String hasRetriedId;
private @Getter private @Getter
@Setter MessageChannel infoChannel; @Setter
MessageChannel infoChannel;
public TrackManager(AudioPlayerManager playerManager, MessageChannel infoChannel) { public TrackManager(AudioPlayerManager playerManager, MessageChannel infoChannel) {
this.player = playerManager.createPlayer(); this.player = playerManager.createPlayer();
@ -90,6 +93,7 @@ public class TrackManager extends AudioEventAdapter {
case FINISHED -> playNext(); case FINISHED -> playNext();
case LOAD_FAILED -> { case LOAD_FAILED -> {
System.out.println("tracc failed");
String identifier; String identifier;
String trackTitle = track.getInfo().title; String trackTitle = track.getInfo().title;
@ -118,7 +122,7 @@ public class TrackManager extends AudioEventAdapter {
@Override public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) { @Override public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
// infoChannel.sendMessage("Uh oh, a track did something strange. Ask the owner to check for errors in console. ").queue(); // infoChannel.sendMessage("Uh oh, a track did something strange. Ask the owner to check for errors in console. ").queue();
// System.out.println(exception.getCause().getMessage()); System.out.println(exception.getCause().getMessage());
onTrackEnd(player, track, AudioTrackEndReason.LOAD_FAILED); onTrackEnd(player, track, AudioTrackEndReason.LOAD_FAILED);
} }

View file

@ -11,7 +11,8 @@ import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class Clear implements Command { public class Clear implements Command {
private @Getter final CommandMeta meta; private @Getter
final CommandMeta meta;
public Clear() { public Clear() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
@ -20,10 +21,7 @@ public class Clear implements Command {
.description("Clears the queue and stops playing.") .description("Clears the queue and stops playing.")
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED, .checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED, VoiceChecks.BOT_PLAYING, VoiceChecks.USER_CONNECTED, VoiceChecks.SAME_VC_IF_CONNECTED })
VoiceChecks.BOT_PLAYING,
VoiceChecks.USER_CONNECTED,
VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
} }
@ -34,7 +32,7 @@ public class Clear implements Command {
assert trackManager != null; // the command will not execute if it is anyway because of our VoiceChecks (BOT_PLAYING) assert trackManager != null; // the command will not execute if it is anyway because of our VoiceChecks (BOT_PLAYING)
int tracksLeft = trackManager.trackQueue.size(); int tracksLeft = trackManager.getTrackQueue().size();
for (int i = 0; i < tracksLeft; i++) { for (int i = 0; i < tracksLeft; i++) {
trackManager.removeQueueItem(0); trackManager.removeQueueItem(0);
} }

View file

@ -15,7 +15,8 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
public class Remove implements Command { public class Remove implements Command {
private @Getter final CommandMeta meta; private @Getter
final CommandMeta meta;
public Remove() { public Remove() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
@ -53,13 +54,13 @@ public class Remove implements Command {
TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel); TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel);
assert trackManager != null; // the command will not execute if it is anyway because of our VoiceChecks (BOT_PLAYING) assert trackManager != null; // the command will not execute if it is anyway because of our VoiceChecks (BOT_PLAYING)
if (trackManager.trackQueue.size() == 0) { if (trackManager.getTrackQueue().size() == 0) {
messageChannel.sendMessage("There are no songs in queue.").queue(); messageChannel.sendMessage("There are no songs in queue.").queue();
} }
Map<String, Integer> indexAliases = new HashMap<>(); Map<String, Integer> indexAliases = new HashMap<>();
indexAliases.put("first", 1); indexAliases.put("first", 1);
indexAliases.put("last", trackManager.trackQueue.size()); indexAliases.put("last", trackManager.getTrackQueue().size());
int parsedIndex; int parsedIndex;
String index = args[0]; String index = args[0];
@ -75,7 +76,7 @@ public class Remove implements Command {
return; return;
} }
if (parsedIndex > trackManager.trackQueue.size() || parsedIndex < 1) { if (parsedIndex > trackManager.getTrackQueue().size() || parsedIndex < 1) {
messageChannel.sendMessage("That index is out of bounds.").queue(); messageChannel.sendMessage("That index is out of bounds.").queue();
return; return;
} }

View file

@ -6,22 +6,24 @@ import net.shadew.json.JsonNode;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.Properties;
import java.util.Map;
public class Config { public class Config {
private final static Json json5 = Json.json5(); private final static Json json5 = Json.json5();
private @Getter final Map<String, String> propreties = new HashMap<>(); private @Getter
final Properties propreties = new Properties();
private String path; private String path;
public static Config loadFrom(String path) throws IOException { public static Config loadFrom(String path) throws IOException {
Config config = new Config(); Config config = new Config();
System.out.println("Working Directory = " + System.getProperty("user.dir"));
JsonNode tree = json5.parse(new File(path)); JsonNode tree = json5.parse(new File(path));
config.path = path; config.path = path;
for (String key : tree.keySet()) { for (String key : tree.keySet()) {
config.getPropreties().put(key, tree.get(key).asString()); config.getPropreties().setProperty(key, tree.get(key).asString());
} }
return config; return config;

View file

@ -16,7 +16,7 @@ import java.util.Map;
public class CommandListener extends ListenerAdapter { public class CommandListener extends ListenerAdapter {
// TODO: load prefix from a database on a per-server basis // TODO: load prefix from a database on a per-server basis
private static final String PREFIX = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("prefix"); private static final String PREFIX = ZtereoMUSIC.getInstance().getConfig().getPropreties().getProperty("prefix");
private static final Map<String, Command> COMMAND_MAP = ZtereoMUSIC.getInstance().getCommandMap(); private static final Map<String, Command> COMMAND_MAP = ZtereoMUSIC.getInstance().getCommandMap();
private static final Map<String, String> COMMAND_ALIASES = ZtereoMUSIC.getInstance().getCommandAliases(); private static final Map<String, String> COMMAND_ALIASES = ZtereoMUSIC.getInstance().getCommandAliases();

View file

@ -10,7 +10,7 @@ import java.io.IOException;
import java.net.URL; import java.net.URL;
public class YoutubeSearch { public class YoutubeSearch {
private static final String API_KEY = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("yt_api_key"); private static final String API_KEY = ZtereoMUSIC.getInstance().getConfig().getPropreties().getProperty("yt_api_key");
private static final Json JSON = Json.json(); private static final Json JSON = Json.json();
public static Pair<Boolean, String> query(String title) { public static Pair<Boolean, String> query(String title) {