mirror of
https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
synced 2025-10-25 13:39:19 +02:00
change: refactor to use the new stuff
This commit is contained in:
parent
e76d236278
commit
6f4c00d4dc
4 changed files with 53 additions and 103 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
package codes.ztereohype.ztereomusic;
|
package codes.ztereohype.ztereomusic;
|
||||||
|
|
||||||
import codes.ztereohype.ztereomusic.audio.TrackManager;
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer;
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
import codes.ztereohype.ztereomusic.command.commands.Ping;
|
import codes.ztereohype.ztereomusic.command.commands.Ping;
|
||||||
import codes.ztereohype.ztereomusic.command.commands.Play;
|
import codes.ztereohype.ztereomusic.command.commands.Play;
|
||||||
|
|
@ -13,11 +13,13 @@ import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_MESSAGES;
|
||||||
|
import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_VOICE_STATES;
|
||||||
|
|
||||||
public class Bot {
|
public class Bot {
|
||||||
private static @Getter Config config;
|
private static @Getter Config config;
|
||||||
private static @Getter JDA bot;
|
private static @Getter JDA bot;
|
||||||
|
|
@ -26,14 +28,14 @@ public class Bot {
|
||||||
private static @Getter final Map<String, String> commandAliases = new HashMap<>();
|
private static @Getter final Map<String, String> commandAliases = new HashMap<>();
|
||||||
|
|
||||||
public static AudioPlayerManager playerManager;
|
public static AudioPlayerManager playerManager;
|
||||||
public static Map<VoiceChannel, TrackManager> trackScheduerMap = new HashMap<>();
|
public static Map<Long, GuildMusicPlayer> guildMusicPlayerMap = new HashMap<>();
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
config = new Config("./config.json5");
|
config = new Config("./config.json5");
|
||||||
bot = JDABuilder.createDefault(config.getPropreties().get("token")).build().awaitReady();
|
bot = JDABuilder.createDefault(config.getPropreties().get("token"), GUILD_MESSAGES, GUILD_VOICE_STATES).build().awaitReady();
|
||||||
|
|
||||||
setCommands();
|
|
||||||
setupAudio();
|
setupAudio();
|
||||||
|
setCommands();
|
||||||
setListeners();
|
setListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,9 +50,7 @@ public class Bot {
|
||||||
commandMap.put(skip.getMeta().getName(), skip);
|
commandMap.put(skip.getMeta().getName(), skip);
|
||||||
|
|
||||||
for (String commandName : commandMap.keySet()) {
|
for (String commandName : commandMap.keySet()) {
|
||||||
System.out.println("loading aliases from: " + commandName);
|
|
||||||
for (String aliasName : commandMap.get(commandName).getMeta().getAliases()) {
|
for (String aliasName : commandMap.get(commandName).getMeta().getAliases()) {
|
||||||
System.out.println("loaded " + aliasName + " from command " + commandName);
|
|
||||||
commandAliases.put(aliasName, commandName);
|
commandAliases.put(aliasName, commandName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -59,6 +59,7 @@ public class Bot {
|
||||||
public static void setupAudio() {
|
public static void setupAudio() {
|
||||||
playerManager = new DefaultAudioPlayerManager();
|
playerManager = new DefaultAudioPlayerManager();
|
||||||
AudioSourceManagers.registerRemoteSources(playerManager);
|
AudioSourceManagers.registerRemoteSources(playerManager);
|
||||||
|
AudioSourceManagers.registerLocalSource(playerManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setListeners() {
|
public static void setListeners() {
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ import java.util.List;
|
||||||
|
|
||||||
public class TrackManager extends AudioEventAdapter {
|
public class TrackManager extends AudioEventAdapter {
|
||||||
private final @Getter AudioPlayer player;
|
private final @Getter AudioPlayer player;
|
||||||
private final List<AudioTrack> trackQueue = new ArrayList<>();
|
public final List<AudioTrack> trackQueue = new ArrayList<>();
|
||||||
private final MessageChannel infoChannel;
|
private final MessageChannel infoChannel;
|
||||||
|
|
||||||
public TrackManager(AudioPlayer player, MessageChannel infoChannel) {
|
public TrackManager(AudioPlayer player, MessageChannel infoChannel) {
|
||||||
|
|
@ -22,20 +22,10 @@ public class TrackManager extends AudioEventAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void queue(AudioTrack track) {
|
public void queue(AudioTrack track) {
|
||||||
// change this to add to queue and call onTrackEnd!
|
|
||||||
if (player.getPlayingTrack() == null) {
|
|
||||||
play(track);
|
|
||||||
} else {
|
|
||||||
trackQueue.add(track);
|
trackQueue.add(track);
|
||||||
infoChannel.sendMessage("Queued " + track.getInfo().title);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void play(AudioTrack track) {
|
public void playNext() {
|
||||||
player.playTrack(track);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void playNext() {
|
|
||||||
// if the player was playing a track (probably means it's a skip), stop it
|
// if the player was playing a track (probably means it's a skip), stop it
|
||||||
if (player.getPlayingTrack() != null) {
|
if (player.getPlayingTrack() != null) {
|
||||||
player.stopTrack();
|
player.stopTrack();
|
||||||
|
|
@ -48,36 +38,18 @@ public class TrackManager extends AudioEventAdapter {
|
||||||
|
|
||||||
AudioTrack nextTrack = trackQueue.get(0);
|
AudioTrack nextTrack = trackQueue.get(0);
|
||||||
trackQueue.remove(nextTrack);
|
trackQueue.remove(nextTrack);
|
||||||
play(nextTrack);
|
player.playTrack(nextTrack);
|
||||||
infoChannel.sendMessage("Playing next track: " + nextTrack.getInfo().title).queue();
|
infoChannel.sendMessage("Playing next track: " + nextTrack.getInfo().title).queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause() {
|
@Override
|
||||||
player.setPaused(true);
|
public void onPlayerPause(AudioPlayer player) {}
|
||||||
}
|
|
||||||
|
|
||||||
public void resume() {
|
|
||||||
player.setPaused(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void skip() {
|
|
||||||
playNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerPause(AudioPlayer player) {
|
public void onPlayerResume(AudioPlayer player) {}
|
||||||
// infoChannel.sendMessage("Pausing...").queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerResume(AudioPlayer player) {
|
public void onTrackStart(AudioPlayer player, AudioTrack track) {}
|
||||||
// infoChannel.sendMessage("Resuming...").queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTrackStart(AudioPlayer player, AudioTrack track) {
|
|
||||||
// infoChannel.sendMessage("Starting track: " + track.getInfo().title).queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
|
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
|
||||||
|
|
@ -85,6 +57,10 @@ public class TrackManager extends AudioEventAdapter {
|
||||||
playNext();
|
playNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (endReason.equals(AudioTrackEndReason.CLEANUP)) {
|
||||||
|
// todo: leave the vc?
|
||||||
|
}
|
||||||
|
|
||||||
// endReason == FINISHED: A track finished or died by an exception (mayStartNext = true).
|
// endReason == FINISHED: A track finished or died by an exception (mayStartNext = true).
|
||||||
// endReason == LOAD_FAILED: Loading of a track failed (mayStartNext = true).
|
// endReason == LOAD_FAILED: Loading of a track failed (mayStartNext = true).
|
||||||
// endReason == STOPPED: The player was stopped.
|
// endReason == STOPPED: The player was stopped.
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
package codes.ztereohype.ztereomusic.command.commands;
|
package codes.ztereohype.ztereomusic.command.commands;
|
||||||
|
|
||||||
import codes.ztereohype.ztereomusic.Bot;
|
import codes.ztereohype.ztereomusic.Bot;
|
||||||
import codes.ztereohype.ztereomusic.audio.AudioPlayerSendHandler;
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer;
|
||||||
import codes.ztereohype.ztereomusic.audio.TrackManager;
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayers;
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
import codes.ztereohype.ztereomusic.command.CommandMeta;
|
import codes.ztereohype.ztereomusic.command.CommandMeta;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
||||||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||||
|
|
@ -63,54 +62,18 @@ public class Play implements Command {
|
||||||
identifier = mergedArgs;
|
identifier = mergedArgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GuildMusicPlayer musicPlayer = GuildMusicPlayers.getGuildAudioPlayer(guild, messageChannel, manager.getConnectedChannel(), voiceChannel);
|
||||||
/* Generally you would want to create a player per every
|
|
||||||
different target you might want to separately stream audio to.
|
|
||||||
It is totally fine to create them even if they are unlikely to be used,
|
|
||||||
as they do not use any resources on their own without an active track. */
|
|
||||||
AudioPlayer player;
|
|
||||||
TrackManager trackManager;
|
|
||||||
|
|
||||||
boolean isInVC = manager.isConnected();
|
|
||||||
boolean isInSameVC = isInVC && Objects.equals(manager.getConnectedChannel(), voiceChannel);
|
|
||||||
|
|
||||||
if (isInSameVC && Bot.trackScheduerMap.containsKey(voiceChannel)) {
|
|
||||||
System.out.println("Found a trackScheduler for this VC already! reusing...");
|
|
||||||
|
|
||||||
trackManager = Bot.trackScheduerMap.get(voiceChannel);
|
|
||||||
player = trackManager.getPlayer();
|
|
||||||
|
|
||||||
player.addListener(trackManager);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Maybe we don't wanna clear the whole queue when he gets kicked out?
|
|
||||||
if (Bot.trackScheduerMap.containsKey(voiceChannel)) {
|
|
||||||
System.out.println("Found old trackScheduler for this channel. Cleaning it up...");
|
|
||||||
Bot.trackScheduerMap.remove(voiceChannel);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Creating a new trackScheduler...");
|
|
||||||
|
|
||||||
player = playerManager.createPlayer();
|
|
||||||
trackManager = new TrackManager(player, messageChannel);
|
|
||||||
|
|
||||||
player.addListener(trackManager);
|
|
||||||
manager.setSendingHandler(new AudioPlayerSendHandler(player));
|
|
||||||
manager.openAudioConnection(voiceChannel);
|
|
||||||
|
|
||||||
Bot.trackScheduerMap.put(voiceChannel, trackManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
playerManager.loadItem(identifier, new AudioLoadResultHandler() {
|
playerManager.loadItem(identifier, new AudioLoadResultHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void trackLoaded(AudioTrack track) {
|
public void trackLoaded(AudioTrack track) {
|
||||||
trackManager.queue(track);
|
musicPlayer.queue(track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playlistLoaded(AudioPlaylist playlist) {
|
public void playlistLoaded(AudioPlaylist playlist) {
|
||||||
for (AudioTrack track : playlist.getTracks()) {
|
for (AudioTrack track : playlist.getTracks()) {
|
||||||
trackManager.queue(track);
|
musicPlayer.queue(track);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
package codes.ztereohype.ztereomusic.command.commands;
|
package codes.ztereohype.ztereomusic.command.commands;
|
||||||
|
|
||||||
import codes.ztereohype.ztereomusic.Bot;
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer;
|
||||||
import codes.ztereohype.ztereomusic.audio.TrackManager;
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayers;
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
import codes.ztereohype.ztereomusic.command.CommandMeta;
|
import codes.ztereohype.ztereomusic.command.CommandMeta;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
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.entities.VoiceChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import net.dv8tion.jda.api.managers.AudioManager;
|
import net.dv8tion.jda.api.managers.AudioManager;
|
||||||
|
|
@ -28,6 +29,12 @@ public class Skip implements Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo: make this part of a perms system (only people in same vc or vc at all have perms on music control)
|
||||||
|
/* Note to self: Things to check before executing command
|
||||||
|
- if he's in vc
|
||||||
|
- if we are in vc
|
||||||
|
- if we are in the same vc
|
||||||
|
*/
|
||||||
if (!author.getVoiceState().inVoiceChannel()) {
|
if (!author.getVoiceState().inVoiceChannel()) {
|
||||||
messageEvent.getMessage().reply("You are not in a voice channel!").queue();
|
messageEvent.getMessage().reply("You are not in a voice channel!").queue();
|
||||||
return;
|
return;
|
||||||
|
|
@ -35,25 +42,28 @@ public class Skip implements Command {
|
||||||
|
|
||||||
Guild guild = messageEvent.getGuild();
|
Guild guild = messageEvent.getGuild();
|
||||||
VoiceChannel voiceChannel = author.getVoiceState().getChannel();
|
VoiceChannel voiceChannel = author.getVoiceState().getChannel();
|
||||||
|
MessageChannel messageChannel = messageEvent.getChannel();
|
||||||
AudioManager manager = guild.getAudioManager();
|
AudioManager manager = guild.getAudioManager();
|
||||||
|
|
||||||
boolean isInVC = manager.isConnected();
|
if (manager.getConnectedChannel() == null) {
|
||||||
boolean isInSameVC = isInVC && Objects.equals(manager.getConnectedChannel(), voiceChannel);
|
messageChannel.sendMessage("I am not even playing anything!").queue();
|
||||||
|
|
||||||
if (isInSameVC && Bot.trackScheduerMap.containsKey(voiceChannel)) {
|
|
||||||
TrackManager trackManager = Bot.trackScheduerMap.get(voiceChannel);
|
|
||||||
|
|
||||||
if (trackManager.getPlayer().getPlayingTrack() == null) {
|
|
||||||
messageEvent.getMessage().reply("No track is playing.").queue();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
trackManager.skip();
|
// Check if we are in the same vc
|
||||||
|
if (!Objects.equals(author.getVoiceState().getChannel(), manager.getConnectedChannel())) {
|
||||||
} else {
|
messageChannel.sendMessage("We aren't in the same channel").queue();
|
||||||
messageEvent.getMessage().reply("No track is playing...").queue();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GuildMusicPlayer musicPlayer = GuildMusicPlayers.getGuildAudioPlayer(guild, messageChannel, manager.getConnectedChannel(), voiceChannel);
|
||||||
|
|
||||||
|
// Check if we are playing anything
|
||||||
|
if (musicPlayer.getPlayer().getPlayingTrack() == null) {
|
||||||
|
messageChannel.sendMessage("I am not even playing anything!").queue();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
musicPlayer.skip();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue