Refactored commands and optimized music manager
This commit is contained in:
1 parent
c11af0da51
commit
1b24a55ca6
14 files changed
+89
-91
No files matched your search
@@ -21,7 +21,7 @@ import javax.security.auth.login.LoginException;
|
||||
public class MusicBot {
|
||||
|
||||
private static JDA jda;
|
||||
private final static Logger logger = LoggerFactory.getLogger(MusicBot.class);
|
||||
public final static Logger logger = LoggerFactory.getLogger(MusicBot.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("log4j.skipJansi", "false");
|
||||
|
||||
@@ -3,9 +3,7 @@ package netneutrality.command;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
|
||||
public abstract class AbstractCommand {
|
||||
public abstract class AbstractCommand implements ICommand {
|
||||
|
||||
public AbstractCommand() {
|
||||
|
||||
@@ -41,12 +39,6 @@ public abstract class AbstractCommand {
|
||||
|
||||
public abstract CommandType getType();
|
||||
|
||||
/**
|
||||
* @param args the arguments to the command
|
||||
*/
|
||||
public abstract String execute(MessageReceivedEvent event, String args);
|
||||
|
||||
|
||||
|
||||
public int countArgs(String command) {
|
||||
if (command.isEmpty())
|
||||
|
||||
@@ -7,7 +7,6 @@ package netneutrality.command;
|
||||
* https://discord.gg/CdFcyQ
|
||||
*/
|
||||
|
||||
import netneutrality.MusicBot;
|
||||
import org.reflections.Reflections;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package netneutrality.command;/*
|
||||
* Copyright (c) anthorob
|
||||
*
|
||||
* https://xtremcommunity.com/
|
||||
* https://discord.gg/CdFcyQ
|
||||
*/
|
||||
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
|
||||
public interface ICommand {
|
||||
|
||||
/**
|
||||
* @param args the arguments to the command
|
||||
*/
|
||||
String execute(MessageReceivedEvent event, String args);
|
||||
}
|
||||
@@ -15,8 +15,8 @@ import netneutrality.http.WebModule;
|
||||
|
||||
public class BitcoinPriceCommand extends AbstractCommand {
|
||||
|
||||
private String currency = "USD";
|
||||
private String currencyUrl = "https://api.coindesk.com/v1/bpi/currentprice/";
|
||||
private String currency;
|
||||
private final String currencyUrl = "https://api.coindesk.com/v1/bpi/currentprice/";
|
||||
private String price;
|
||||
|
||||
public BitcoinPriceCommand() {
|
||||
|
||||
@@ -14,8 +14,8 @@ import netneutrality.http.WebModule;
|
||||
|
||||
public class EthereumPriceCommand extends AbstractCommand {
|
||||
|
||||
private String currency = "USD";
|
||||
private String currencyUrl = "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=";
|
||||
private String currency;
|
||||
private final String currencyUrl = "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=";
|
||||
private String price = "";
|
||||
|
||||
public EthereumPriceCommand() {
|
||||
|
||||
@@ -14,8 +14,8 @@ import netneutrality.http.WebModule;
|
||||
|
||||
public class LitecoinPriceCommand extends AbstractCommand {
|
||||
|
||||
private String currency = "USD";
|
||||
private String currencyUrl = "https://api.coinmarketcap.com/v1/ticker/litecoin/?convert=";
|
||||
private String currency;
|
||||
private final String currencyUrl = "https://api.coinmarketcap.com/v1/ticker/litecoin/?convert=";
|
||||
private String price;
|
||||
|
||||
public LitecoinPriceCommand() {
|
||||
|
||||
@@ -65,7 +65,6 @@ public class StopCommand extends AbstractCommand {
|
||||
|
||||
if (player.isConnected()) {
|
||||
if (player.isConnectedTo(vChannel)) {
|
||||
player.stopMusic();
|
||||
player.leave();
|
||||
} else
|
||||
event.getTextChannel().sendMessage("Not in your voice channel").queue();
|
||||
|
||||
@@ -63,6 +63,7 @@ public class DatabaseCommand extends AbstractCommand {
|
||||
@Override
|
||||
public String execute(MessageReceivedEvent event, String args) {
|
||||
//TODO: Clean up the mess
|
||||
|
||||
event.getMessage().delete().queue();
|
||||
|
||||
if (!event.getAuthor().getDiscriminator().equals(Settings.getInstance().getCreator().getDiscriminator()))
|
||||
|
||||
@@ -30,23 +30,13 @@ public class AudioPlayerSendHandler implements AudioSendHandler {
|
||||
|
||||
@Override
|
||||
public boolean canProvide() {
|
||||
if (lastFrame == null) {
|
||||
lastFrame = audioPlayer.provide();
|
||||
}
|
||||
|
||||
return lastFrame != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer provide20MsAudio() {
|
||||
if (lastFrame == null) {
|
||||
lastFrame = audioPlayer.provide();
|
||||
}
|
||||
|
||||
byte[] data = lastFrame != null ? lastFrame.getData() : null;
|
||||
lastFrame = null;
|
||||
|
||||
return ByteBuffer.wrap(data);
|
||||
return ByteBuffer.wrap(lastFrame.getData());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,26 +9,58 @@ import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
||||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import netneutrality.handler.LogHandler;
|
||||
import netneutrality.utils.MessageFunction;
|
||||
|
||||
public class LoadResultHandler implements AudioLoadResultHandler {
|
||||
|
||||
private final TextChannel channel;
|
||||
private final Member user;
|
||||
private final TrackScheduler scheduler;
|
||||
private final String identifier;
|
||||
private final boolean silent;
|
||||
|
||||
public LoadResultHandler(String identifier, TextChannel channel, Member user, TrackScheduler scheduler, boolean silent) {
|
||||
this.identifier = identifier;
|
||||
this.channel = channel;
|
||||
this.user = user;
|
||||
this.scheduler = scheduler;
|
||||
this.silent = silent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack audioTrack) {
|
||||
if (!silent) {
|
||||
channel.sendMessage(MessageFunction.buildAddingTrackMessage(audioTrack, user)).queue();
|
||||
}
|
||||
|
||||
scheduler.queue(audioTrack);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist audioPlaylist) {
|
||||
if (identifier.startsWith("ytsearch:")) {
|
||||
AudioTrack track = audioPlaylist.getTracks().get(0);
|
||||
|
||||
channel.sendMessage(MessageFunction.buildAddingTrackMessage(track, user)).queue();
|
||||
scheduler.queue(track);
|
||||
} else {
|
||||
for (AudioTrack track : audioPlaylist.getTracks()) {
|
||||
scheduler.queue(track);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
|
||||
channel.sendMessage("No matches found").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException e) {
|
||||
|
||||
LogHandler.log(e);
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,13 @@ package netneutrality.music;
|
||||
* This file is closed-source. It should not be distributed or copied in any way or form
|
||||
*/
|
||||
|
||||
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.DefaultAudioPlayerManager;
|
||||
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
||||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import netneutrality.MusicBot;
|
||||
import netneutrality.ThreadManager;
|
||||
import netneutrality.handler.LogHandler;
|
||||
import netneutrality.utils.MessageFunction;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -32,18 +26,18 @@ import java.util.concurrent.TimeUnit;
|
||||
public class MusicManager {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(MusicManager.class);
|
||||
private final Guild guild;
|
||||
private final static AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
|
||||
private final AudioPlayer player;
|
||||
private final TrackScheduler scheduler;
|
||||
private final static Map<String, MusicManager> managers = new ConcurrentHashMap<>();
|
||||
private final String guildId;
|
||||
|
||||
private MusicManager(Guild guild) {
|
||||
|
||||
this.guildId = guild.getId();
|
||||
player = playerManager.createPlayer();
|
||||
this.guild = guild;
|
||||
this.player = playerManager.createPlayer();
|
||||
this.scheduler = new TrackScheduler(player);
|
||||
guild.getAudioManager().setSendingHandler(new AudioPlayerSendHandler(player));
|
||||
scheduler = new TrackScheduler(player);
|
||||
player.addListener(scheduler);
|
||||
player.setVolume(30);
|
||||
|
||||
@@ -62,12 +56,11 @@ public class MusicManager {
|
||||
}
|
||||
|
||||
public boolean isConnectedTo(VoiceChannel vChannel) {
|
||||
return vChannel != null && vChannel.equals(MusicBot.getJDA().getGuildById(guildId).getAudioManager().getConnectedChannel());
|
||||
return vChannel != null && vChannel.equals(guild.getAudioManager().getConnectedChannel());
|
||||
}
|
||||
|
||||
public synchronized void connect(VoiceChannel vChannel) {
|
||||
if (vChannel != null && !isConnectedTo(vChannel)) {
|
||||
Guild guild = MusicBot.getJDA().getGuildById(guildId);
|
||||
guild.getAudioManager().openAudioConnection(vChannel);
|
||||
|
||||
((ScheduledExecutorService) ThreadManager.getExecutor("music", ThreadManager.ExecutorType.SCHEDULED, 5)).scheduleWithFixedDelay(() -> {
|
||||
@@ -79,7 +72,6 @@ public class MusicManager {
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
Guild guild = MusicBot.getJDA().getGuildById(guildId);
|
||||
return guild != null && guild.getAudioManager().getConnectedChannel() != null;
|
||||
}
|
||||
|
||||
@@ -89,24 +81,18 @@ public class MusicManager {
|
||||
|
||||
public synchronized void leave() {
|
||||
if (isConnected()) {
|
||||
stopMusic();
|
||||
player.destroy();
|
||||
}
|
||||
|
||||
scheduler.resetTopic();
|
||||
|
||||
Guild guild = MusicBot.getJDA().getGuildById(guildId);
|
||||
if (guild != null) {
|
||||
guild.getAudioManager().closeAudioConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public synchronized void stopMusic() {
|
||||
scheduler.resetTopic();
|
||||
player.destroy();
|
||||
}
|
||||
|
||||
public synchronized static void disconnectPlayers() {
|
||||
public static void disconnectPlayers() {
|
||||
for (MusicManager mm : managers.values()) {
|
||||
mm.leave();
|
||||
}
|
||||
@@ -119,43 +105,7 @@ public class MusicManager {
|
||||
scheduler.setTextChannel(channel);
|
||||
|
||||
if (identifier != null) {
|
||||
playerManager.loadItem(identifier, new AudioLoadResultHandler() {
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack audioTrack) {
|
||||
|
||||
if (!silent) {
|
||||
channel.sendMessage(MessageFunction.buildAddingTrackMessage(audioTrack, user)).queue();
|
||||
}
|
||||
|
||||
scheduler.queue(audioTrack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
|
||||
if (identifier.startsWith("ytsearch:")) {
|
||||
AudioTrack track = playlist.getTracks().get(0);
|
||||
|
||||
channel.sendMessage(MessageFunction.buildAddingTrackMessage(track, user)).queue();
|
||||
scheduler.queue(track);
|
||||
} else {
|
||||
for (AudioTrack track : playlist.getTracks()) {
|
||||
scheduler.queue(track);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
channel.sendMessage("No matches found").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException e) {
|
||||
LogHandler.log(e);
|
||||
}
|
||||
});
|
||||
|
||||
playerManager.loadItem(identifier, new LoadResultHandler(identifier, channel, user, scheduler, silent));
|
||||
|
||||
} else {
|
||||
channel.sendMessage("No source found to play the given url").queue();
|
||||
@@ -167,7 +117,6 @@ public class MusicManager {
|
||||
if (!scheduler.queue.isEmpty()) {
|
||||
scheduler.nextTrack();
|
||||
} else {
|
||||
stopMusic();
|
||||
leave();
|
||||
}
|
||||
}
|
||||
@@ -183,6 +132,7 @@ public class MusicManager {
|
||||
public List<String> getAllQueuedTrack() {
|
||||
List<String> queuedTracks = new LinkedList<>();
|
||||
scheduler.queue.forEach((a) -> queuedTracks.add(a.getInfo().title));
|
||||
|
||||
return queuedTracks;
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class ChannelFunction {
|
||||
private synchronized static void deleteFromList(List<Message> messages, TextChannel channel) {
|
||||
try {
|
||||
channel.deleteMessages(messages).complete();
|
||||
logger.debug("Deleted " + messages.size() + " messages");
|
||||
logger.debug("Deleted " + messages.size() + " messages from channel ["+ channel.getName() + "] in guild [" + channel.getGuild().getName() + "]");
|
||||
|
||||
} catch (IllegalArgumentException e) {
|
||||
messages.forEach(m -> {
|
||||
|
||||
@@ -7,12 +7,15 @@ package netneutrality.utils;
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
@@ -58,10 +61,26 @@ public class MessageFunction {
|
||||
}
|
||||
|
||||
public synchronized static MessageEmbed buildAddingTrackMessage(AudioTrack track, Member user) {
|
||||
Duration d = Duration.ofMillis(track.getDuration());
|
||||
|
||||
EmbedBuilder message = new EmbedBuilder();
|
||||
message.setTitle("Adding track to queue..");
|
||||
message.setColor(Color.BLUE);
|
||||
message.addField("Track title: ", track.getInfo().title, false);
|
||||
message.addField("Duration: ", d.toMinutesPart() + ":" + d.toSecondsPart(), false);
|
||||
message.setFooter("Requested by " + user.getUser().getName(), user.getUser().getAvatarUrl());
|
||||
message.setTimestamp(OffsetDateTime.now());
|
||||
|
||||
|
||||
return message.build();
|
||||
}
|
||||
|
||||
public synchronized static MessageEmbed buildAddingPlaylistMessage(AudioPlaylist playlist, Member user) {
|
||||
EmbedBuilder message = new EmbedBuilder();
|
||||
message.setTitle("Adding playlist to queue..");
|
||||
message.setColor(Color.BLUE);
|
||||
message.addField("Playlist: ", playlist.getName(), false);
|
||||
message.addField("Number of tracks: ", String.valueOf(playlist.getTracks().size()), false);
|
||||
message.setFooter("Requested by " + user.getUser().getName(), user.getUser().getAvatarUrl());
|
||||
message.setTimestamp(OffsetDateTime.now());
|
||||
|
||||
|
||||
Reference in new issue
Block a user