change: reformat code because i messed up first time

This commit is contained in:
ZtereoHYPE 2022-02-04 14:30:37 +01:00
parent 41438cae0d
commit 735d4bc193
13 changed files with 130 additions and 130 deletions

View file

@ -56,9 +56,9 @@ public class ZtereoMUSIC {
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().get("token"), intents)
.enableCache(CacheFlag.VOICE_STATE) .enableCache(CacheFlag.VOICE_STATE)
.build() .build()
.awaitReady()); .awaitReady());
ztereoMUSIC.setupAudio(); ztereoMUSIC.setupAudio();
ztereoMUSIC.setCommands(); ztereoMUSIC.setCommands();

View file

@ -32,7 +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

@ -18,10 +18,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TrackManager extends AudioEventAdapter { public class TrackManager extends AudioEventAdapter {
private final @Getter AudioPlayer player;
public final List<AudioTrack> trackQueue = new ArrayList<>(); public final List<AudioTrack> trackQueue = new ArrayList<>();
private @Getter @Setter MessageChannel infoChannel; private final @Getter AudioPlayer player;
private final Guild guild; private final Guild guild;
private @Getter @Setter MessageChannel infoChannel;
public TrackManager(AudioPlayerManager playerManager, MessageChannel infoChannel, Guild guild) { public TrackManager(AudioPlayerManager playerManager, MessageChannel infoChannel, Guild guild) {
this.player = playerManager.createPlayer(); this.player = playerManager.createPlayer();
@ -106,8 +106,8 @@ public class TrackManager extends AudioEventAdapter {
} }
ZtereoMUSIC.getInstance() ZtereoMUSIC.getInstance()
.getPlayerManager() .getPlayerManager()
.loadItem(identifier, new CustomAudioLoadResultHandler(this, infoChannel)); .loadItem(identifier, new CustomAudioLoadResultHandler(this, infoChannel));
} }
} }
@ -120,7 +120,8 @@ 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( infoChannel.sendMessage(
"Uh oh, a track did something strange. Ask the owner to check for errors in console. Skpping...").queue(); "Uh oh, a track did something strange. Ask the owner to check for errors in console. Skpping...")
.queue();
System.out.println(exception.getCause().getMessage()); System.out.println(exception.getCause().getMessage());
} }

View file

@ -15,16 +15,16 @@ public class Clear implements Command {
public Clear() { public Clear() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("clear") .name("clear")
.aliases(new String[] { "deleteall" }) .aliases(new String[] { "deleteall" })
.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.BOT_PLAYING,
VoiceChecks.USER_CONNECTED, VoiceChecks.USER_CONNECTED,
VoiceChecks.SAME_VC_IF_CONNECTED }) VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
} }
@Override public void execute(MessageReceivedEvent messageEvent, String[] args) { @Override public void execute(MessageReceivedEvent messageEvent, String[] args) {

View file

@ -12,15 +12,15 @@ public class Disconnect implements Command {
public Disconnect() { public Disconnect() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("disconnect") .name("disconnect")
.description("A command to kick the bot from the vc.") .description("A command to kick the bot from the vc.")
.aliases(new String[] { "fuckoff", "bye" }) .aliases(new String[] { "fuckoff", "bye" })
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED, .checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED,
VoiceChecks.USER_CONNECTED, VoiceChecks.USER_CONNECTED,
VoiceChecks.SAME_VC_IF_CONNECTED }) VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
} }
@Override public CommandMeta getMeta() { @Override public CommandMeta getMeta() {

View file

@ -14,16 +14,16 @@ public class Pause implements Command {
public Pause() { public Pause() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("pause") .name("pause")
.description("Pause the playing music") .description("Pause the playing music")
.aliases(new String[] { "resume" }) .aliases(new String[] { "resume" })
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED, .checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED,
VoiceChecks.BOT_PLAYING, VoiceChecks.BOT_PLAYING,
VoiceChecks.USER_CONNECTED, VoiceChecks.USER_CONNECTED,
VoiceChecks.SAME_VC_IF_CONNECTED }) VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
} }
@Override public CommandMeta getMeta() { @Override public CommandMeta getMeta() {

View file

@ -10,13 +10,13 @@ public class Ping implements Command {
public Ping() { public Ping() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("ping") .name("ping")
.description("A command to get pinged") .description("A command to get pinged")
.aliases(new String[] { "pong", "pog" }) .aliases(new String[] { "pong", "pog" })
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[0]) .checks(new VoiceChecks[0])
.build(); .build();
} }
@Override public CommandMeta getMeta() { @Override public CommandMeta getMeta() {
@ -24,7 +24,7 @@ public class Ping implements Command {
} }
public void execute(MessageReceivedEvent messageEvent, String[] args) { public void execute(MessageReceivedEvent messageEvent, String[] args) {
messageEvent.getMessage().reply("get ping'd lolmao").queue(); messageEvent.getMessage().reply("git ping'd lolmao").queue();
} }
} }

View file

@ -24,19 +24,19 @@ public class Play implements Command {
private static final Pattern URL_PATTERN = Pattern.compile("^(http|https)://([a-z]+\\.[a-z]+)+/\\S+$", private static final Pattern URL_PATTERN = Pattern.compile("^(http|https)://([a-z]+\\.[a-z]+)+/\\S+$",
Pattern.CASE_INSENSITIVE); Pattern.CASE_INSENSITIVE);
private static final Pattern SPOTIFY_URL_PATTERN = Pattern.compile( private static final Pattern SPOTIFY_URL_PATTERN = Pattern.compile(
"^(?:https://open\\.spotify\\.com/(track|playlist)/)(\\S+(?:\\?si=\\S+))$"); "^(?:https://open\\.spotify\\.com/(track|playlist)/)(\\S+(?:\\?si=\\S+))$");
private final CommandMeta meta; private final CommandMeta meta;
public Play() { public Play() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("play") .name("play")
.description("Play music!") .description("Play music!")
.aliases(new String[] { "p" }) .aliases(new String[] { "p" })
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[] { VoiceChecks.USER_CONNECTED, VoiceChecks.SAME_VC_IF_CONNECTED }) .checks(new VoiceChecks[] { VoiceChecks.USER_CONNECTED, VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
} }
@Override public CommandMeta getMeta() { @Override public CommandMeta getMeta() {
@ -56,7 +56,8 @@ public class Play implements Command {
if (trackManager == null || !trackManager.getPlayer().isPaused()) { if (trackManager == null || !trackManager.getPlayer().isPaused()) {
messageChannel.sendMessage( messageChannel.sendMessage(
"What should I play? Type the name of the song after the command or use a YouTube link!").queue(); "What should I play? Type the name of the song after the command or use a YouTube link!")
.queue();
return; return;
} }
@ -99,7 +100,7 @@ public class Play implements Command {
TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel); TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel);
ZtereoMUSIC.getInstance() ZtereoMUSIC.getInstance()
.getPlayerManager() .getPlayerManager()
.loadItem(identifier, new CustomAudioLoadResultHandler(trackManager, messageChannel)); .loadItem(identifier, new CustomAudioLoadResultHandler(trackManager, messageChannel));
} }
} }

View file

@ -19,15 +19,15 @@ public class Queue implements Command {
public Queue() { public Queue() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("queue") .name("queue")
.description("See the queue") .description("See the queue")
.aliases(new String[] { "q" }) .aliases(new String[] { "q" })
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED, .checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED,
VoiceChecks.USER_CONNECTED, VoiceChecks.USER_CONNECTED,
VoiceChecks.SAME_VC_IF_CONNECTED }) VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
} }
@Override public CommandMeta getMeta() { @Override public CommandMeta getMeta() {
@ -37,7 +37,7 @@ public class Queue implements Command {
public void execute(MessageReceivedEvent messageEvent, String[] args) { public void execute(MessageReceivedEvent messageEvent, String[] args) {
Guild guild = messageEvent.getGuild(); Guild guild = messageEvent.getGuild();
VoiceChannel voiceChannel = Objects.requireNonNull(Objects.requireNonNull(messageEvent.getMember()) VoiceChannel voiceChannel = Objects.requireNonNull(Objects.requireNonNull(messageEvent.getMember())
.getVoiceState()).getChannel(); .getVoiceState()).getChannel();
MessageChannel messageChannel = messageEvent.getChannel(); MessageChannel messageChannel = messageEvent.getChannel();
TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel); TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel);

View file

@ -19,16 +19,28 @@ public class Remove implements Command {
public Remove() { public Remove() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("remove") .name("remove")
.aliases(new String[] { "delete" }) .aliases(new String[] { "delete" })
.description("Remove the chosen item.") .description("Remove the chosen item.")
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED, .checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED,
VoiceChecks.BOT_PLAYING, VoiceChecks.BOT_PLAYING,
VoiceChecks.USER_CONNECTED, VoiceChecks.USER_CONNECTED,
VoiceChecks.SAME_VC_IF_CONNECTED }) VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
}
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
int i = Integer.parseInt(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
} }
@Override public void execute(MessageReceivedEvent messageEvent, String[] args) { @Override public void execute(MessageReceivedEvent messageEvent, String[] args) {
@ -38,8 +50,8 @@ public class Remove implements Command {
// if there's the wrong amount of arguments send the usage // if there's the wrong amount of arguments send the usage
if (args.length != 1) { if (args.length != 1) {
messageChannel.sendMessage( messageChannel.sendMessage(
"Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.") "Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.")
.queue(); .queue();
return; return;
} }
@ -65,8 +77,8 @@ public class Remove implements Command {
parsedIndex = indexAliases.get(index); parsedIndex = indexAliases.get(index);
} else { } else {
messageChannel.sendMessage( messageChannel.sendMessage(
"Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.") "Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.")
.queue(); .queue();
return; return;
} }
@ -78,16 +90,4 @@ public class Remove implements Command {
String title = trackManager.removeQueueItem(parsedIndex - 1); String title = trackManager.removeQueueItem(parsedIndex - 1);
messageChannel.sendMessage("Removed " + title).queue(); messageChannel.sendMessage("Removed " + title).queue();
} }
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
int i = Integer.parseInt(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
} }

View file

@ -15,16 +15,16 @@ public class Skip implements Command {
public Skip() { public Skip() {
this.meta = CommandMeta.builder() this.meta = CommandMeta.builder()
.name("skip") .name("skip")
.description("Skip the current track!") .description("Skip the current track!")
.aliases(new String[] { "next" }) .aliases(new String[] { "next" })
.isNsfw(false) .isNsfw(false)
.isHidden(false) .isHidden(false)
.checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED, .checks(new VoiceChecks[] { VoiceChecks.BOT_CONNECTED,
VoiceChecks.BOT_PLAYING, VoiceChecks.BOT_PLAYING,
VoiceChecks.USER_CONNECTED, VoiceChecks.USER_CONNECTED,
VoiceChecks.SAME_VC_IF_CONNECTED }) VoiceChecks.SAME_VC_IF_CONNECTED })
.build(); .build();
} }
@Override public CommandMeta getMeta() { @Override public CommandMeta getMeta() {

View file

@ -49,9 +49,9 @@ public class CommandListener extends ListenerAdapter {
// check if the command is allowed and stop at first failure (order is important) // check if the command is allowed and stop at first failure (order is important)
for (VoiceChecks checkEnum : command.getMeta().getChecks()) { for (VoiceChecks checkEnum : command.getMeta().getChecks()) {
if (!checkEnum.getCheck() if (!checkEnum.getCheck()
.getResult(message.getMember(), .getResult(message.getMember(),
guild.getAudioManager().getConnectedChannel(), guild.getAudioManager().getConnectedChannel(),
TrackManagers.getGuildTrackManager(guild, messageChannel))) { TrackManagers.getGuildTrackManager(guild, messageChannel))) {
message.reply(checkEnum.getCheck().getErrorCode()).queue(); message.reply(checkEnum.getCheck().getErrorCode()).queue();
return; return;
} }
@ -63,9 +63,9 @@ public class CommandListener extends ListenerAdapter {
} catch (Exception e) { } catch (Exception e) {
//todo: nicer embed with error pls //todo: nicer embed with error pls
message.getChannel() message.getChannel()
.sendMessage( .sendMessage(
"uh oh something really bad happened and yeah so yeah everything is aborted and cancelled i give up this is too hard kthxbye") "uh oh something really bad happened and yeah so yeah everything is aborted and cancelled i give up this is too hard kthxbye")
.queue(); .queue();
throw e; throw e;
} }
} }

View file

@ -21,19 +21,17 @@ import java.util.regex.Pattern;
public class SpotifyApiHelper { public class SpotifyApiHelper {
private static final String CLIENT_ID = ZtereoMUSIC.getInstance() private static final String CLIENT_ID = ZtereoMUSIC.getInstance()
.getConfig() .getConfig()
.getPropreties() .getPropreties()
.get("spotify_client_id"); .get("spotify_client_id");
private static final String CLIENT_SECRET = ZtereoMUSIC.getInstance() private static final String CLIENT_SECRET = ZtereoMUSIC.getInstance()
.getConfig() .getConfig()
.getPropreties() .getPropreties()
.get("spotify_client_secret"); .get("spotify_client_secret");
private static final Pattern IDENTIFIER_PATTERN = Pattern.compile( private static final Pattern IDENTIFIER_PATTERN = Pattern.compile(
"(?:(?<=https://open\\.spotify\\.com/track/)|(?<=https://open\\.spotify\\.com/playlist/))(\\S+(?=\\?si=\\S))"); "(?:(?<=https://open\\.spotify\\.com/track/)|(?<=https://open\\.spotify\\.com/playlist/))(\\S+(?=\\?si=\\S))");
private static String spotifyToken;
private static final Json JSON = Json.json(); private static final Json JSON = Json.json();
private static String spotifyToken;
public static void startTokenTimer() { public static void startTokenTimer() {
Timer timer = new Timer(); Timer timer = new Timer();
@ -55,13 +53,13 @@ public class SpotifyApiHelper {
@SneakyThrows private static Optional<String> getToken() { @SneakyThrows private static Optional<String> getToken() {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create( HttpRequest request = HttpRequest.newBuilder(URI.create(
"https://accounts.spotify.com/api/token?grant_type=client_credentials")) "https://accounts.spotify.com/api/token?grant_type=client_credentials"))
.POST(HttpRequest.BodyPublishers.ofString("")) .POST(HttpRequest.BodyPublishers.ofString(""))
.header("Authorization", .header("Authorization",
"Basic " + Base64.getEncoder() "Basic " + Base64.getEncoder()
.encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes(StandardCharsets.UTF_8.toString()))) .encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes(StandardCharsets.UTF_8.toString())))
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.build(); .build();
JsonPath tokenPath = JsonPath.parse("access_token"); JsonPath tokenPath = JsonPath.parse("access_token");
try { try {
@ -98,10 +96,10 @@ public class SpotifyApiHelper {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(query)) HttpRequest request = HttpRequest.newBuilder(URI.create(query))
.GET() .GET()
.header("Authorization", "Bearer " + spotifyToken) .header("Authorization", "Bearer " + spotifyToken)
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.build(); .build();
JsonPath titlePath = JsonPath.parse("tracks[0].name"); JsonPath titlePath = JsonPath.parse("tracks[0].name");
// JsonPath authorPath = JsonPath.parse("tracks[0].artists[0].name"); // JsonPath authorPath = JsonPath.parse("tracks[0].artists[0].name");