mirror of
https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
synced 2025-10-25 21:49:17 +02:00
change: reformat code
This commit is contained in:
parent
9471fc67e0
commit
880fd0bd9b
21 changed files with 199 additions and 198 deletions
|
|
@ -46,21 +46,19 @@ public class ZtereoMUSIC {
|
||||||
return ZtereoMUSIC.INSTANCE;
|
return ZtereoMUSIC.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows({ FileNotFoundException.class, LoginException.class,
|
@SneakyThrows({ FileNotFoundException.class, LoginException.class, InterruptedException.class, IOException.class })
|
||||||
InterruptedException.class, IOException.class })
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ZtereoMUSIC ztereoMUSIC = ZtereoMUSIC.getInstance();
|
ZtereoMUSIC ztereoMUSIC = ZtereoMUSIC.getInstance();
|
||||||
|
|
||||||
EnumSet<GatewayIntent> intents = EnumSet.of(
|
EnumSet<GatewayIntent> intents = EnumSet.of(GatewayIntent.GUILD_MESSAGES,
|
||||||
GatewayIntent.GUILD_MESSAGES,
|
|
||||||
GatewayIntent.GUILD_VOICE_STATES,
|
GatewayIntent.GUILD_VOICE_STATES,
|
||||||
GatewayIntent.GUILD_EMOJIS
|
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().get("token"), intents)
|
||||||
.enableCache(CacheFlag.VOICE_STATE)
|
.enableCache(CacheFlag.VOICE_STATE)
|
||||||
.build().awaitReady());
|
.build()
|
||||||
|
.awaitReady());
|
||||||
|
|
||||||
ztereoMUSIC.setupAudio();
|
ztereoMUSIC.setupAudio();
|
||||||
ztereoMUSIC.setCommands();
|
ztereoMUSIC.setCommands();
|
||||||
|
|
|
||||||
|
|
@ -14,19 +14,16 @@ public class AudioPlayerSendHandler implements AudioSendHandler {
|
||||||
this.audioPlayer = audioPlayer;
|
this.audioPlayer = audioPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public boolean canProvide() {
|
||||||
public boolean canProvide() {
|
|
||||||
lastFrame = audioPlayer.provide();
|
lastFrame = audioPlayer.provide();
|
||||||
return lastFrame != null;
|
return lastFrame != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public ByteBuffer provide20MsAudio() {
|
||||||
public ByteBuffer provide20MsAudio() {
|
|
||||||
return ByteBuffer.wrap(lastFrame.getData());
|
return ByteBuffer.wrap(lastFrame.getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public boolean isOpus() {
|
||||||
public boolean isOpus() {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,26 +16,23 @@ public class CustomAudioLoadResultHandler implements AudioLoadResultHandler {
|
||||||
trackManager.setInfoChannel(messageChannel);
|
trackManager.setInfoChannel(messageChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void trackLoaded(AudioTrack track) {
|
||||||
public void trackLoaded(AudioTrack track) {
|
|
||||||
this.trackManager.queue(track);
|
this.trackManager.queue(track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void playlistLoaded(AudioPlaylist playlist) {
|
||||||
public void playlistLoaded(AudioPlaylist playlist) {
|
|
||||||
for (AudioTrack track : playlist.getTracks()) {
|
for (AudioTrack track : playlist.getTracks()) {
|
||||||
this.trackManager.queue(track);
|
this.trackManager.queue(track);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void noMatches() {
|
||||||
public void noMatches() {
|
|
||||||
this.messageChannel.sendMessage("I found no matches for that song!").queue();
|
this.messageChannel.sendMessage("I found no matches for that song!").queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void loadFailed(FriendlyException throwable) {
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -82,21 +82,15 @@ public class TrackManager extends AudioEventAdapter {
|
||||||
infoChannel.sendMessage("Playing next track: " + nextTrack.getInfo().title).queue();
|
infoChannel.sendMessage("Playing next track: " + nextTrack.getInfo().title).queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void onPlayerPause(AudioPlayer player) {}
|
||||||
public void onPlayerPause(AudioPlayer player) {}
|
|
||||||
|
|
||||||
@Override
|
@Override public void onPlayerResume(AudioPlayer player) {}
|
||||||
public void onPlayerResume(AudioPlayer player) {}
|
|
||||||
|
|
||||||
@Override
|
@Override public void onTrackStart(AudioPlayer player, AudioTrack track) {}
|
||||||
public void onTrackStart(AudioPlayer player, AudioTrack track) {}
|
|
||||||
|
|
||||||
@Override
|
@Override public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
|
||||||
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
|
|
||||||
switch (endReason) {
|
switch (endReason) {
|
||||||
case FINISHED -> {
|
case FINISHED -> playNext();
|
||||||
playNext();
|
|
||||||
}
|
|
||||||
//todo: warning: this will create an infinite loop if a specific video has issues...
|
//todo: warning: this will create an infinite loop if a specific video has issues...
|
||||||
case LOAD_FAILED -> {
|
case LOAD_FAILED -> {
|
||||||
infoChannel.sendMessage("Loading failed, retrying...").queue();
|
infoChannel.sendMessage("Loading failed, retrying...").queue();
|
||||||
|
|
@ -111,7 +105,9 @@ public class TrackManager extends AudioEventAdapter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZtereoMUSIC.getInstance().getPlayerManager().loadItem(identifier, new CustomAudioLoadResultHandler(this, infoChannel));
|
ZtereoMUSIC.getInstance()
|
||||||
|
.getPlayerManager()
|
||||||
|
.loadItem(identifier, new CustomAudioLoadResultHandler(this, infoChannel));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,14 +118,13 @@ public class TrackManager extends AudioEventAdapter {
|
||||||
// endReason == CLEANUP: Player hasn't been queried for a while, if you want you can put a clone of this back to your queue
|
// endReason == CLEANUP: Player hasn't been queried for a while, if you want you can put a clone of this back to your queue
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void onTrackStuck(AudioPlayer player, AudioTrack track, long thresholdMs) {
|
||||||
public void onTrackStuck(AudioPlayer player, AudioTrack track, long thresholdMs) {
|
|
||||||
infoChannel.sendMessage("Unable to play track " + track.getInfo().title + ". Skipping...").queue();
|
infoChannel.sendMessage("Unable to play track " + track.getInfo().title + ". Skipping...").queue();
|
||||||
trackQueue.remove(track);
|
trackQueue.remove(track);
|
||||||
playNext();
|
playNext();
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,7 @@ import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class TrackManagers {
|
public class TrackManagers {
|
||||||
//note: maybe make infoChannel an optional? not sure how to make this better, ask rep
|
//note: maybe make infoChannel an optional? not sure how to make this better, ask rep
|
||||||
@Nullable
|
@Nullable public static TrackManager getGuildTrackManager(Guild guild, @Nullable MessageChannel infoChannel) {
|
||||||
public static TrackManager getGuildTrackManager(Guild guild, @Nullable MessageChannel infoChannel) {
|
|
||||||
long guildId = guild.getIdLong();
|
long guildId = guild.getIdLong();
|
||||||
|
|
||||||
TrackManager trackManager = ZtereoMUSIC.getInstance().getGuildTrackManagerMap().get(guildId);
|
TrackManager trackManager = ZtereoMUSIC.getInstance().getGuildTrackManagerMap().get(guildId);
|
||||||
|
|
@ -27,7 +26,9 @@ public class TrackManagers {
|
||||||
return trackManager;
|
return trackManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TrackManager getOrCreateGuildTrackManager(Guild guild, MessageChannel infoChannel, VoiceChannel requestedChannel) {
|
public static TrackManager getOrCreateGuildTrackManager(Guild guild,
|
||||||
|
MessageChannel infoChannel,
|
||||||
|
VoiceChannel requestedChannel) {
|
||||||
long guildId = guild.getIdLong();
|
long guildId = guild.getIdLong();
|
||||||
|
|
||||||
TrackManager trackManager = ZtereoMUSIC.getInstance().getGuildTrackManagerMap().get(guildId);
|
TrackManager trackManager = ZtereoMUSIC.getInstance().getGuildTrackManagerMap().get(guildId);
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,6 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
// TODO: add categories/groups and perms but in a smart way pls
|
// TODO: add categories/groups and perms but in a smart way pls
|
||||||
public interface Command {
|
public interface Command {
|
||||||
CommandMeta getMeta();
|
CommandMeta getMeta();
|
||||||
|
|
||||||
void execute(MessageReceivedEvent messageRecievedEvent, String[] args);
|
void execute(MessageReceivedEvent messageRecievedEvent, String[] args);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@ import codes.ztereohype.ztereomusic.command.permissions.VoiceChecks;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
@Getter @Builder
|
@Getter
|
||||||
|
@Builder
|
||||||
public class CommandMeta {
|
public class CommandMeta {
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String description;
|
private final String description;
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ 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)
|
||||||
|
|
@ -27,8 +27,7 @@ public class Clear implements Command {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void execute(MessageReceivedEvent messageEvent, String[] args) {
|
||||||
public void execute(MessageReceivedEvent messageEvent, String[] args) {
|
|
||||||
Guild guild = messageEvent.getGuild();
|
Guild guild = messageEvent.getGuild();
|
||||||
MessageChannel messageChannel = messageEvent.getChannel();
|
MessageChannel messageChannel = messageEvent.getChannel();
|
||||||
TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel);
|
TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel);
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,7 @@ public class Disconnect implements Command {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public CommandMeta getMeta() {
|
||||||
public CommandMeta getMeta() {
|
|
||||||
return this.meta;
|
return this.meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,13 +26,11 @@ public class Pause implements Command {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public CommandMeta getMeta() {
|
||||||
public CommandMeta getMeta() {
|
|
||||||
return this.meta;
|
return this.meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void execute(MessageReceivedEvent messageEvent, String[] args) {
|
||||||
public void execute(MessageReceivedEvent messageEvent, String[] args) {
|
|
||||||
Guild guild = messageEvent.getGuild();
|
Guild guild = messageEvent.getGuild();
|
||||||
MessageChannel messageChannel = messageEvent.getChannel();
|
MessageChannel messageChannel = messageEvent.getChannel();
|
||||||
TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel);
|
TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel);
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,7 @@ public class Ping implements Command {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public CommandMeta getMeta() {
|
||||||
public CommandMeta getMeta() {
|
|
||||||
return this.meta;
|
return this.meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,10 @@ import codes.ztereohype.ztereomusic.command.CommandMeta;
|
||||||
import codes.ztereohype.ztereomusic.command.permissions.VoiceChecks;
|
import codes.ztereohype.ztereomusic.command.permissions.VoiceChecks;
|
||||||
import codes.ztereohype.ztereomusic.networking.SpotifyApiHelper;
|
import codes.ztereohype.ztereomusic.networking.SpotifyApiHelper;
|
||||||
import codes.ztereohype.ztereomusic.networking.YoutubeSearch;
|
import codes.ztereohype.ztereomusic.networking.YoutubeSearch;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.*;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||||
|
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import net.shadew.util.data.Pair;
|
import net.shadew.util.data.Pair;
|
||||||
|
|
||||||
|
|
@ -19,8 +21,10 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Play implements Command {
|
public class Play implements Command {
|
||||||
private static final Pattern URL_PATTERN = Pattern.compile("^(http|https)://([a-z]+\\.[a-z]+)+/\\S+$", Pattern.CASE_INSENSITIVE);
|
private static final Pattern URL_PATTERN = Pattern.compile("^(http|https)://([a-z]+\\.[a-z]+)+/\\S+$",
|
||||||
private static final Pattern SPOTIFY_URL_PATTERN = Pattern.compile("^(?:https://open\\.spotify\\.com/(track|playlist)/)(\\S+(?:\\?si=\\S+))$");
|
Pattern.CASE_INSENSITIVE);
|
||||||
|
private static final Pattern SPOTIFY_URL_PATTERN = Pattern.compile(
|
||||||
|
"^(?:https://open\\.spotify\\.com/(track|playlist)/)(\\S+(?:\\?si=\\S+))$");
|
||||||
|
|
||||||
private final CommandMeta meta;
|
private final CommandMeta meta;
|
||||||
|
|
||||||
|
|
@ -31,13 +35,11 @@ public class Play implements Command {
|
||||||
.aliases(new String[] { "p" })
|
.aliases(new String[] { "p" })
|
||||||
.isNsfw(false)
|
.isNsfw(false)
|
||||||
.isHidden(false)
|
.isHidden(false)
|
||||||
.checks(new VoiceChecks[] { VoiceChecks.USER_CONNECTED,
|
.checks(new VoiceChecks[] { VoiceChecks.USER_CONNECTED, VoiceChecks.SAME_VC_IF_CONNECTED })
|
||||||
VoiceChecks.SAME_VC_IF_CONNECTED })
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public CommandMeta getMeta() {
|
||||||
public CommandMeta getMeta() {
|
|
||||||
return this.meta;
|
return this.meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,14 +49,14 @@ public class Play implements Command {
|
||||||
Guild guild = messageEvent.getGuild();
|
Guild guild = messageEvent.getGuild();
|
||||||
VoiceChannel voiceChannel = Objects.requireNonNull(author.getVoiceState()).getChannel();
|
VoiceChannel voiceChannel = Objects.requireNonNull(author.getVoiceState()).getChannel();
|
||||||
MessageChannel messageChannel = messageEvent.getChannel();
|
MessageChannel messageChannel = messageEvent.getChannel();
|
||||||
AudioPlayerManager playerManager = ZtereoMUSIC.getInstance().getPlayerManager();
|
|
||||||
|
|
||||||
// if there are no args use as play/pause
|
// if there are no args use as play/pause
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel);
|
TrackManager trackManager = TrackManagers.getGuildTrackManager(guild, messageChannel);
|
||||||
|
|
||||||
if (trackManager == null || !trackManager.getPlayer().isPaused()) {
|
if (trackManager == null || !trackManager.getPlayer().isPaused()) {
|
||||||
messageChannel.sendMessage("What should I play? Type the name of the song after the command or use a YouTube link!").queue();
|
messageChannel.sendMessage(
|
||||||
|
"What should I play? Type the name of the song after the command or use a YouTube link!").queue();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,6 +98,8 @@ public class Play implements Command {
|
||||||
|
|
||||||
TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel);
|
TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel);
|
||||||
|
|
||||||
playerManager.loadItem(identifier, new CustomAudioLoadResultHandler(trackManager, messageChannel));
|
ZtereoMUSIC.getInstance()
|
||||||
|
.getPlayerManager()
|
||||||
|
.loadItem(identifier, new CustomAudioLoadResultHandler(trackManager, messageChannel));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,21 +30,21 @@ public class Queue implements Command {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public CommandMeta getMeta() {
|
||||||
public CommandMeta getMeta() {
|
|
||||||
return this.meta;
|
return this.meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
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()).getVoiceState()).getChannel();
|
VoiceChannel voiceChannel = Objects.requireNonNull(Objects.requireNonNull(messageEvent.getMember())
|
||||||
|
.getVoiceState()).getChannel();
|
||||||
MessageChannel messageChannel = messageEvent.getChannel();
|
MessageChannel messageChannel = messageEvent.getChannel();
|
||||||
|
|
||||||
TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel);
|
TrackManager trackManager = TrackManagers.getOrCreateGuildTrackManager(guild, messageChannel, voiceChannel);
|
||||||
|
|
||||||
StringBuilder messageBuilder = new StringBuilder();
|
StringBuilder messageBuilder = new StringBuilder();
|
||||||
List<AudioTrack> trackList = trackManager.trackQueue;
|
List<AudioTrack> trackList = trackManager.trackQueue;
|
||||||
for (AudioTrack track: trackList) {
|
for (AudioTrack track : trackList) {
|
||||||
messageBuilder.append(trackList.indexOf(track) + 1).append(". ");
|
messageBuilder.append(trackList.indexOf(track) + 1).append(". ");
|
||||||
messageBuilder.append(track.getInfo().title);
|
messageBuilder.append(track.getInfo().title);
|
||||||
messageBuilder.append(System.getProperty("line.separator"));
|
messageBuilder.append(System.getProperty("line.separator"));
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ 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)
|
||||||
|
|
@ -31,14 +31,15 @@ public class Remove implements Command {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void execute(MessageReceivedEvent messageEvent, String[] args) {
|
||||||
public void execute(MessageReceivedEvent messageEvent, String[] args) {
|
|
||||||
Guild guild = messageEvent.getGuild();
|
Guild guild = messageEvent.getGuild();
|
||||||
MessageChannel messageChannel = messageEvent.getChannel();
|
MessageChannel messageChannel = messageEvent.getChannel();
|
||||||
|
|
||||||
// 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("Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.").queue();
|
messageChannel.sendMessage(
|
||||||
|
"Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.")
|
||||||
|
.queue();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +64,9 @@ public class Remove implements Command {
|
||||||
} else if (indexAliases.containsKey(index.toLowerCase(Locale.ROOT))) {
|
} else if (indexAliases.containsKey(index.toLowerCase(Locale.ROOT))) {
|
||||||
parsedIndex = indexAliases.get(index);
|
parsedIndex = indexAliases.get(index);
|
||||||
} else {
|
} else {
|
||||||
messageChannel.sendMessage("Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.").queue();
|
messageChannel.sendMessage(
|
||||||
|
"Usage: `remove [index of song to remove]/first/last`. Use the `queue` command to find the index.")
|
||||||
|
.queue();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,7 @@ public class Skip implements Command {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public CommandMeta getMeta() {
|
||||||
public CommandMeta getMeta() {
|
|
||||||
return this.meta;
|
return this.meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@ import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||||
|
|
||||||
public interface Check {
|
public interface Check {
|
||||||
boolean getResult(Member messageAuthor, VoiceChannel connectedChannel, TrackManager trackManager);
|
boolean getResult(Member messageAuthor, VoiceChannel connectedChannel, TrackManager trackManager);
|
||||||
|
|
||||||
String getErrorCode();
|
String getErrorCode();
|
||||||
}
|
}
|
||||||
|
|
@ -15,20 +15,20 @@ public enum VoiceChecks {
|
||||||
return connectedChannel != null;
|
return connectedChannel != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public String getErrorCode() {
|
||||||
public String getErrorCode() {
|
|
||||||
return "I am not playing anything.";
|
return "I am not playing anything.";
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
BOT_PLAYING(new Check() {
|
BOT_PLAYING(new Check() {
|
||||||
@Override
|
@Override
|
||||||
public boolean getResult(Member messageAuthor, VoiceChannel connectedChannel, @Nullable TrackManager trackManager) {
|
public boolean getResult(Member messageAuthor,
|
||||||
|
VoiceChannel connectedChannel,
|
||||||
|
@Nullable TrackManager trackManager) {
|
||||||
return trackManager != null && trackManager.getPlayer().getPlayingTrack() != null;
|
return trackManager != null && trackManager.getPlayer().getPlayingTrack() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public String getErrorCode() {
|
||||||
public String getErrorCode() {
|
|
||||||
return "I am not playing anything.";
|
return "I am not playing anything.";
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
@ -40,8 +40,7 @@ public enum VoiceChecks {
|
||||||
return messageAuthor.getVoiceState().inVoiceChannel();
|
return messageAuthor.getVoiceState().inVoiceChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public String getErrorCode() {
|
||||||
public String getErrorCode() {
|
|
||||||
return "You are not connected to a voice channel.";
|
return "You are not connected to a voice channel.";
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
@ -54,8 +53,7 @@ public enum VoiceChecks {
|
||||||
return Objects.equals(messageAuthor.getVoiceState().getChannel(), connectedChannel);
|
return Objects.equals(messageAuthor.getVoiceState().getChannel(), connectedChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public String getErrorCode() {
|
||||||
public String getErrorCode() {
|
|
||||||
return "We are not in the same voice channel.";
|
return "We are not in the same voice channel.";
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
@ -67,8 +65,7 @@ public enum VoiceChecks {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public String getErrorCode() {
|
||||||
public String getErrorCode() {
|
|
||||||
return "You don't have the *DJ role*.";
|
return "You don't have the *DJ role*.";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,10 @@ import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class AloneDisconnectListener extends ListenerAdapter {
|
public class AloneDisconnectListener extends ListenerAdapter {
|
||||||
//sorry reperak, i tried using a list of pairs but iterating over it to find it in onGuildVoiceLeave is too much effort
|
//sorry reperak, i tried using a list of pairs but iterating over it to find it in onGuildVoiceLeave is too much effort
|
||||||
|
|
@ -18,15 +21,13 @@ public class AloneDisconnectListener extends ListenerAdapter {
|
||||||
public AloneDisconnectListener() {
|
public AloneDisconnectListener() {
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.scheduleAtFixedRate(new TimerTask() {
|
timer.scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override public void run() {
|
||||||
public void run() {
|
|
||||||
checkIfAloneAfterThreshold();
|
checkIfAloneAfterThreshold();
|
||||||
}
|
}
|
||||||
}, 5000, 5000);
|
}, 5000, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void onGuildVoiceLeave(@Nonnull GuildVoiceLeaveEvent event) {
|
||||||
public void onGuildVoiceLeave(@Nonnull GuildVoiceLeaveEvent event) {
|
|
||||||
Guild guild = event.getGuild();
|
Guild guild = event.getGuild();
|
||||||
Member leavingMember = event.getMember();
|
Member leavingMember = event.getMember();
|
||||||
Member ztereoBotMember = event.getGuild().getMember(ZtereoMUSIC.getInstance().getJda().getSelfUser());
|
Member ztereoBotMember = event.getGuild().getMember(ZtereoMUSIC.getInstance().getJda().getSelfUser());
|
||||||
|
|
@ -47,8 +48,7 @@ public class AloneDisconnectListener extends ListenerAdapter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public void onGuildVoiceJoin(@Nonnull GuildVoiceJoinEvent event) {
|
||||||
public void onGuildVoiceJoin(@Nonnull GuildVoiceJoinEvent event) {
|
|
||||||
Guild guild = event.getGuild();
|
Guild guild = event.getGuild();
|
||||||
|
|
||||||
if (guild.getAudioManager().getConnectedChannel() == null) return; // if we're not connected ignore
|
if (guild.getAudioManager().getConnectedChannel() == null) return; // if we're not connected ignore
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ import codes.ztereohype.ztereomusic.ZtereoMUSIC;
|
||||||
import codes.ztereohype.ztereomusic.audio.TrackManagers;
|
import codes.ztereohype.ztereomusic.audio.TrackManagers;
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
import codes.ztereohype.ztereomusic.command.permissions.VoiceChecks;
|
import codes.ztereohype.ztereomusic.command.permissions.VoiceChecks;
|
||||||
import net.dv8tion.jda.api.entities.*;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
|
|
||||||
|
|
@ -18,8 +20,7 @@ public class CommandListener extends ListenerAdapter {
|
||||||
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();
|
||||||
|
|
||||||
@Override
|
@Override public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
|
||||||
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
|
|
||||||
Message message = event.getMessage();
|
Message message = event.getMessage();
|
||||||
MessageChannel messageChannel = event.getChannel();
|
MessageChannel messageChannel = event.getChannel();
|
||||||
String content = message.getContentRaw();
|
String content = message.getContentRaw();
|
||||||
|
|
@ -47,7 +48,10 @@ 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().getResult(message.getMember(), guild.getAudioManager().getConnectedChannel(), TrackManagers.getGuildTrackManager(guild, messageChannel))) {
|
if (!checkEnum.getCheck()
|
||||||
|
.getResult(message.getMember(),
|
||||||
|
guild.getAudioManager().getConnectedChannel(),
|
||||||
|
TrackManagers.getGuildTrackManager(guild, messageChannel))) {
|
||||||
message.reply(checkEnum.getCheck().getErrorCode()).queue();
|
message.reply(checkEnum.getCheck().getErrorCode()).queue();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -58,7 +62,10 @@ public class CommandListener extends ListenerAdapter {
|
||||||
command.execute(event, args);
|
command.execute(event, args);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//todo: nicer embed with error pls
|
//todo: nicer embed with error pls
|
||||||
message.getChannel().sendMessage("uh oh something really bad happened and yeah so yeah everything is aborted and cancelled i give up this is too hard kthxbye").queue();
|
message.getChannel()
|
||||||
|
.sendMessage(
|
||||||
|
"uh oh something really bad happened and yeah so yeah everything is aborted and cancelled i give up this is too hard kthxbye")
|
||||||
|
.queue();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,16 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class SpotifyApiHelper {
|
public class SpotifyApiHelper {
|
||||||
private static final String CLIENT_ID = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("spotify_client_id");
|
private static final String CLIENT_ID = ZtereoMUSIC.getInstance()
|
||||||
private static final String CLIENT_SECRET = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("spotify_client_secret");
|
.getConfig()
|
||||||
private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("(?:(?<=https://open\\.spotify\\.com/track/)|(?<=https://open\\.spotify\\.com/playlist/))(\\S+(?=\\?si=\\S))");
|
.getPropreties()
|
||||||
|
.get("spotify_client_id");
|
||||||
|
private static final String CLIENT_SECRET = ZtereoMUSIC.getInstance()
|
||||||
|
.getConfig()
|
||||||
|
.getPropreties()
|
||||||
|
.get("spotify_client_secret");
|
||||||
|
private static final Pattern IDENTIFIER_PATTERN = Pattern.compile(
|
||||||
|
"(?:(?<=https://open\\.spotify\\.com/track/)|(?<=https://open\\.spotify\\.com/playlist/))(\\S+(?=\\?si=\\S))");
|
||||||
|
|
||||||
private static String spotifyToken;
|
private static String spotifyToken;
|
||||||
|
|
||||||
|
|
@ -31,8 +38,7 @@ public class SpotifyApiHelper {
|
||||||
public static void startTokenTimer() {
|
public static void startTokenTimer() {
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.scheduleAtFixedRate(new TimerTask() {
|
timer.scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override public void run() {
|
||||||
public void run() {
|
|
||||||
spotifyToken = null; //remove old outdated token
|
spotifyToken = null; //remove old outdated token
|
||||||
Optional<String> parsedToken = getToken();
|
Optional<String> parsedToken = getToken();
|
||||||
|
|
||||||
|
|
@ -43,16 +49,17 @@ public class SpotifyApiHelper {
|
||||||
|
|
||||||
spotifyToken = parsedToken.get();
|
spotifyToken = parsedToken.get();
|
||||||
}
|
}
|
||||||
}, 0, 3599*1000);
|
}, 0, 3599 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows private static Optional<String> getToken() {
|
||||||
private static Optional<String> getToken() {
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
HttpRequest request = HttpRequest.newBuilder(
|
HttpRequest request = HttpRequest.newBuilder(URI.create(
|
||||||
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", "Basic " + Base64.getEncoder().encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes(StandardCharsets.UTF_8.toString())))
|
.header("Authorization",
|
||||||
|
"Basic " + Base64.getEncoder()
|
||||||
|
.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();
|
||||||
|
|
||||||
|
|
@ -86,30 +93,28 @@ public class SpotifyApiHelper {
|
||||||
return Pair.of(false, "Could not parse Spotify link. Try entering the song title directly.");
|
return Pair.of(false, "Could not parse Spotify link. Try entering the song title directly.");
|
||||||
}
|
}
|
||||||
|
|
||||||
String query = "https://api.spotify.com/v1/tracks?ids="
|
String query = "https://api.spotify.com/v1/tracks?ids=" + spotifyIdentifier + "&market=ES"; //españaaaa
|
||||||
+ spotifyIdentifier + "&market=ES"; //españaaaa
|
|
||||||
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.newBuilder(
|
HttpRequest request = HttpRequest.newBuilder(URI.create(query))
|
||||||
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");
|
||||||
try {
|
try {
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
String title = JSON.parse(response.body()).query(titlePath).asString();
|
String title = JSON.parse(response.body()).query(titlePath).asString();
|
||||||
// String author = JSON.parse(response.body()).query(authorPath).asString();
|
// String author = JSON.parse(response.body()).query(authorPath).asString();
|
||||||
|
|
||||||
return Pair.of(true, title);
|
return Pair.of(true, title);
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return Pair.of(false,"Something wrong happened with the spotify request.");
|
return Pair.of(false, "Something wrong happened with the spotify request.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ public class YoutubeSearch {
|
||||||
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) {
|
||||||
String query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22"
|
String query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22" + title.replace(' ',
|
||||||
+ title.replace(' ', '+') + "%22&type=video&key=" + API_KEY;
|
'+') + "%22&type=video&key=" + API_KEY;
|
||||||
|
|
||||||
String jsonResponse;
|
String jsonResponse;
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue