mirror of
				https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
				synced 2025-10-26 12:59:19 +01: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; | ||||
| 
 | ||||
| import codes.ztereohype.ztereomusic.audio.TrackManager; | ||||
| import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer; | ||||
| import codes.ztereohype.ztereomusic.command.Command; | ||||
| import codes.ztereohype.ztereomusic.command.commands.Ping; | ||||
| import codes.ztereohype.ztereomusic.command.commands.Play; | ||||
|  | @ -13,11 +13,13 @@ import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers; | |||
| import lombok.Getter; | ||||
| import net.dv8tion.jda.api.JDA; | ||||
| import net.dv8tion.jda.api.JDABuilder; | ||||
| import net.dv8tion.jda.api.entities.VoiceChannel; | ||||
| 
 | ||||
| import java.util.HashMap; | ||||
| 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 { | ||||
|     private static @Getter Config config; | ||||
|     private static @Getter JDA bot; | ||||
|  | @ -26,14 +28,14 @@ public class Bot { | |||
|     private static @Getter final Map<String, String> commandAliases = new HashMap<>(); | ||||
| 
 | ||||
|     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 { | ||||
|         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(); | ||||
|         setCommands(); | ||||
|         setListeners(); | ||||
|     } | ||||
| 
 | ||||
|  | @ -48,9 +50,7 @@ public class Bot { | |||
|         commandMap.put(skip.getMeta().getName(), skip); | ||||
| 
 | ||||
|         for (String commandName : commandMap.keySet()) { | ||||
|             System.out.println("loading aliases from: " + commandName); | ||||
|             for (String aliasName : commandMap.get(commandName).getMeta().getAliases()) { | ||||
|                 System.out.println("loaded " + aliasName + " from command " + commandName); | ||||
|                 commandAliases.put(aliasName, commandName); | ||||
|             } | ||||
|         } | ||||
|  | @ -59,6 +59,7 @@ public class Bot { | |||
|     public static void setupAudio() { | ||||
|         playerManager = new DefaultAudioPlayerManager(); | ||||
|         AudioSourceManagers.registerRemoteSources(playerManager); | ||||
|         AudioSourceManagers.registerLocalSource(playerManager); | ||||
|     } | ||||
| 
 | ||||
|     public static void setListeners() { | ||||
|  |  | |||
|  | @ -13,7 +13,7 @@ import java.util.List; | |||
| 
 | ||||
| public class TrackManager extends AudioEventAdapter { | ||||
|     private final @Getter AudioPlayer player; | ||||
|     private final List<AudioTrack> trackQueue = new ArrayList<>(); | ||||
|     public final List<AudioTrack> trackQueue = new ArrayList<>(); | ||||
|     private final MessageChannel infoChannel; | ||||
| 
 | ||||
|     public TrackManager(AudioPlayer player, MessageChannel infoChannel) { | ||||
|  | @ -22,20 +22,10 @@ public class TrackManager extends AudioEventAdapter { | |||
|     } | ||||
| 
 | ||||
|     public void queue(AudioTrack track) { | ||||
|         // change this to add to queue and call onTrackEnd! | ||||
|         if (player.getPlayingTrack() == null) { | ||||
|             play(track); | ||||
|         } else { | ||||
|             trackQueue.add(track); | ||||
|             infoChannel.sendMessage("Queued " + track.getInfo().title); | ||||
|         } | ||||
|         trackQueue.add(track); | ||||
|     } | ||||
| 
 | ||||
|     private void play(AudioTrack track) { | ||||
|         player.playTrack(track); | ||||
|     } | ||||
| 
 | ||||
|     private void playNext() { | ||||
|     public void playNext() { | ||||
|         // if the player was playing a track (probably means it's a skip), stop it | ||||
|         if (player.getPlayingTrack() != null) { | ||||
|             player.stopTrack(); | ||||
|  | @ -48,36 +38,18 @@ public class TrackManager extends AudioEventAdapter { | |||
| 
 | ||||
|         AudioTrack nextTrack = trackQueue.get(0); | ||||
|         trackQueue.remove(nextTrack); | ||||
|         play(nextTrack); | ||||
|         player.playTrack(nextTrack); | ||||
|         infoChannel.sendMessage("Playing next track: " + nextTrack.getInfo().title).queue(); | ||||
|     } | ||||
| 
 | ||||
|     public void pause() { | ||||
|         player.setPaused(true); | ||||
|     } | ||||
| 
 | ||||
|     public void resume() { | ||||
|         player.setPaused(false); | ||||
|     } | ||||
| 
 | ||||
|     public void skip() { | ||||
|         playNext(); | ||||
|     } | ||||
|     @Override | ||||
|     public void onPlayerPause(AudioPlayer player) {} | ||||
| 
 | ||||
|     @Override | ||||
|     public void onPlayerPause(AudioPlayer player) { | ||||
| //        infoChannel.sendMessage("Pausing...").queue(); | ||||
|     } | ||||
|     public void onPlayerResume(AudioPlayer player) {} | ||||
| 
 | ||||
|     @Override | ||||
|     public void onPlayerResume(AudioPlayer player) { | ||||
| //        infoChannel.sendMessage("Resuming...").queue(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onTrackStart(AudioPlayer player, AudioTrack track) { | ||||
| //        infoChannel.sendMessage("Starting track: " + track.getInfo().title).queue(); | ||||
|     } | ||||
|     public void onTrackStart(AudioPlayer player, AudioTrack track) {} | ||||
| 
 | ||||
|     @Override | ||||
|     public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) { | ||||
|  | @ -85,6 +57,10 @@ public class TrackManager extends AudioEventAdapter { | |||
|             playNext(); | ||||
|         } | ||||
| 
 | ||||
|         if (endReason.equals(AudioTrackEndReason.CLEANUP)) { | ||||
|             // todo: leave the vc? | ||||
|         } | ||||
| 
 | ||||
|         // endReason == FINISHED: A track finished or died by an exception (mayStartNext = true). | ||||
|         // endReason == LOAD_FAILED: Loading of a track failed (mayStartNext = true). | ||||
|         // endReason == STOPPED: The player was stopped. | ||||
|  |  | |||
|  | @ -1,12 +1,11 @@ | |||
| package codes.ztereohype.ztereomusic.command.commands; | ||||
| 
 | ||||
| import codes.ztereohype.ztereomusic.Bot; | ||||
| import codes.ztereohype.ztereomusic.audio.AudioPlayerSendHandler; | ||||
| import codes.ztereohype.ztereomusic.audio.TrackManager; | ||||
| import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer; | ||||
| import codes.ztereohype.ztereomusic.audio.GuildMusicPlayers; | ||||
| import codes.ztereohype.ztereomusic.command.Command; | ||||
| import codes.ztereohype.ztereomusic.command.CommandMeta; | ||||
| 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.tools.FriendlyException; | ||||
| import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; | ||||
|  | @ -63,54 +62,18 @@ public class Play implements Command { | |||
|             identifier = mergedArgs; | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         /* 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); | ||||
|         } | ||||
|         GuildMusicPlayer musicPlayer = GuildMusicPlayers.getGuildAudioPlayer(guild, messageChannel, manager.getConnectedChannel(), voiceChannel); | ||||
| 
 | ||||
|         playerManager.loadItem(identifier, new AudioLoadResultHandler() { | ||||
|             @Override | ||||
|             public void trackLoaded(AudioTrack track) { | ||||
|                 trackManager.queue(track); | ||||
|                 musicPlayer.queue(track); | ||||
|             } | ||||
| 
 | ||||
|             @Override | ||||
|             public void playlistLoaded(AudioPlaylist playlist) { | ||||
|                 for (AudioTrack track : playlist.getTracks()) { | ||||
|                     trackManager.queue(track); | ||||
|                     musicPlayer.queue(track); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,11 +1,12 @@ | |||
| package codes.ztereohype.ztereomusic.command.commands; | ||||
| 
 | ||||
| import codes.ztereohype.ztereomusic.Bot; | ||||
| import codes.ztereohype.ztereomusic.audio.TrackManager; | ||||
| import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer; | ||||
| import codes.ztereohype.ztereomusic.audio.GuildMusicPlayers; | ||||
| import codes.ztereohype.ztereomusic.command.Command; | ||||
| import codes.ztereohype.ztereomusic.command.CommandMeta; | ||||
| import net.dv8tion.jda.api.entities.Guild; | ||||
| 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.managers.AudioManager; | ||||
|  | @ -28,6 +29,12 @@ public class Skip implements Command { | |||
|             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()) { | ||||
|             messageEvent.getMessage().reply("You are not in a voice channel!").queue(); | ||||
|             return; | ||||
|  | @ -35,25 +42,28 @@ public class Skip implements Command { | |||
| 
 | ||||
|         Guild guild = messageEvent.getGuild(); | ||||
|         VoiceChannel voiceChannel = author.getVoiceState().getChannel(); | ||||
|         MessageChannel messageChannel = messageEvent.getChannel(); | ||||
|         AudioManager manager = guild.getAudioManager(); | ||||
| 
 | ||||
|         boolean isInVC = manager.isConnected(); | ||||
|         boolean isInSameVC = isInVC && Objects.equals(manager.getConnectedChannel(), voiceChannel); | ||||
| 
 | ||||
|         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; | ||||
|             } | ||||
| 
 | ||||
|             trackManager.skip(); | ||||
| 
 | ||||
|         } else { | ||||
|             messageEvent.getMessage().reply("No track is playing...").queue(); | ||||
|         if (manager.getConnectedChannel() == null) { | ||||
|             messageChannel.sendMessage("I am not even playing anything!").queue(); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
|         // Check if we are in the same vc | ||||
|         if (!Objects.equals(author.getVoiceState().getChannel(), manager.getConnectedChannel())) { | ||||
|             messageChannel.sendMessage("We aren't in the same channel").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