Skip to content

Commit

Permalink
Update 0.6.0 - Music Bot refined.
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostedCA committed Mar 13, 2023
1 parent 1790602 commit 46ecc5b
Show file tree
Hide file tree
Showing 19 changed files with 568 additions and 129 deletions.
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ca.tristan.easycommands</groupId>
<artifactId>EasyCommands</artifactId>
<version>0.3</version>
<version>0.6</version>
<packaging>jar</packaging>
<name>EasyCommands</name>

Expand All @@ -30,6 +30,10 @@
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -48,6 +52,11 @@
<artifactId>lavaplayer</artifactId>
<version>1.3.77</version>
</dependency>
<dependency>
<groupId>com.github.LowLevelSubmarine</groupId>
<artifactId>GeniusLyricsAPI</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>

</project>
81 changes: 56 additions & 25 deletions src/main/java/ca/tristan/easycommands/commands/EasyCommands.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package ca.tristan.easycommands.commands;

import ca.tristan.easycommands.commands.music.NowPlayingCmd;
import ca.tristan.easycommands.commands.music.PlayCmd;
import ca.tristan.easycommands.commands.music.SkipCmd;
import ca.tristan.easycommands.commands.music.StopCmd;
import ca.tristan.easycommands.commands.music.*;
import ca.tristan.easycommands.commands.prefix.PrefixCommands;
import ca.tristan.easycommands.commands.prefix.PrefixExecutor;
import ca.tristan.easycommands.commands.slash.CommandExecutor;
import ca.tristan.easycommands.commands.slash.SlashExecutor;
import ca.tristan.easycommands.events.AutoDisconnectEvent;
import ca.tristan.easycommands.events.ButtonEvents;
import ca.tristan.easycommands.utils.ConsoleColors;
import ca.tristan.easycommands.utils.LogType;
import ca.tristan.easycommands.utils.Logger;
import com.mysql.cj.log.Log;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.restaction.CommandEditAction;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.jetbrains.annotations.NotNull;

Expand All @@ -44,9 +48,14 @@ public class EasyCommands extends ListenerAdapter {

private PrefixCommands prefixCommands;

private static Channel musicChannel;

private Long millisStart, millisEnd;

public EasyCommands(String token, boolean enablePrefixCommands, boolean enableMusicBot) {
this.usePrefixCommands = enablePrefixCommands;
this.useMusicBot = enableMusicBot;
millisStart = System.currentTimeMillis();

loadIntents();

Expand All @@ -69,6 +78,7 @@ public JDA buildJDA() throws InterruptedException {

if(this.useMusicBot) {
enableMusicBot();
this.jda.addEventListener(new AutoDisconnectEvent(), new ButtonEvents());
}

if(usePrefixCommands) {
Expand All @@ -77,7 +87,8 @@ public JDA buildJDA() throws InterruptedException {

updateCommands();
logCurrentExecutors();

millisEnd = System.currentTimeMillis();
Logger.log(LogType.OK, "EasyCommands finished loading in " + ConsoleColors.GREEN_BOLD + (millisEnd - millisStart) + "ms" + ConsoleColors.GREEN + ".");
return jda;
}

Expand Down Expand Up @@ -156,8 +167,7 @@ public EasyCommands clearExecutors() {

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
if(getExecutors().containsKey(event.getName()) && (getExecutors().get(event.getName()) instanceof SlashExecutor || getExecutors().get(event.getName()) instanceof CommandExecutor)) {
IExecutor executor = getExecutors().get(event.getName());
if(getExecutors().containsKey(event.getName()) && getExecutors().get(event.getName()) instanceof SlashExecutor executor) {
Logger.log(LogType.SLASHCMD, "'" + executor.getName() + "' has been triggered.");
if(executor.isOwnerOnly() && ! (Objects.requireNonNull(event.getMember())).isOwner()) {
event.reply("This command can only be used by the server owner.").setEphemeral(true).queue();
Expand All @@ -172,40 +182,47 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
if(executor.getAuthorizedRoles(jda) != null && !executor.getAuthorizedRoles(jda).isEmpty()) {
for (Role authorizedRole : executor.getAuthorizedRoles(jda)) {
if(Objects.requireNonNull(event.getMember()).getRoles().contains(authorizedRole)) {
if(executor instanceof CommandExecutor ex) {
ex.execute(new EventData(event));
} else if(executor instanceof SlashExecutor ex) {
ex.execute(new EventData(event));
}
executor.execute(new EventData(event));
break;
}
}
return;
}

if(executor instanceof CommandExecutor ex) {
ex.execute(new EventData(event));
} else if(executor instanceof SlashExecutor ex) {
ex.execute(new EventData(event));
}
executor.execute(new EventData(event));
}
}

/**
* Used to debug executors. Serve to identify if the commands are registered to Discord correctly.
*/
private void logCurrentExecutors() {
Logger.log(LogType.OK, jda.retrieveCommands().complete().toString());

List<Command> commands = jda.retrieveCommands().complete();
Logger.log(LogType.EXECUTORS, "--- Registered SlashExecutors ---");
for (Command command : commands) {
Logger.logNoType("/" + command.getName() + ConsoleColors.RESET + ":" + ConsoleColors.CYAN + command.getId());
}

Logger.log(LogType.EXECUTORS, "--- Registered PrefixExecutors ---");
getExecutors().forEach((s, iExecutor) -> {
if(iExecutor instanceof PrefixExecutor) {
if(!iExecutor.getAliases().contains(s)) {
Logger.logNoType(getPrefixCommands().getPrefix() + s);
}
}
});

}

/**
* Updates all executors/commands to Discord Guild.
*/
public void updateCommands() {
private void updateCommands() {
List<CommandData> commands = new ArrayList<>();
getExecutors().forEach((name, executor) -> {
if(executor instanceof SlashExecutor executor1) {
commands.add(Commands.slash(executor1.getName(), executor1.getDescription()).addOptions(executor1.getOptions()));
commands.add(Commands.slash(name, executor1.getDescription()).addOptions(executor1.getOptions()));
}
});
jda.updateCommands().addCommands(commands).queue();
Expand All @@ -226,9 +243,23 @@ public EasyCommands registerListeners(ListenerAdapter... listeners) {
return this;
}

public void enableMusicBot() {
this.addExecutor(new PlayCmd(), new StopCmd(), new NowPlayingCmd(), new SkipCmd());
Logger.log(LogType.OK, "Music bot enabled.");
private void enableMusicBot() {
this.addExecutor(new PlayCmd(), new StopCmd(), new NowPlayingCmd(), new SkipCmd(), new PauseCmd(), new LyricsCmd());
Logger.log(LogType.OK, "EasyCommands MusicBot has been enabled successfully.");
}

public void setMusicChannel(String id) {

if(this.jda == null) {
Logger.log(LogType.ERROR, "Can't set the music channel before building JDA.");
return;
}

musicChannel = this.jda.getTextChannelById(id);
}

public static Channel getMusicChannel() {
return musicChannel;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ public GuildVoiceState getMemberVoiceState() {
return memberVoiceState;
}

public void deferReply(boolean isEphemeral) {
event.deferReply(isEphemeral).queue();
}

public void deferReply() {
event.deferReply().queue();
this.deferReply(false);
}

public ReplyCallbackAction reply(String content, boolean ephemeral) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import ca.tristan.easycommands.commands.EventData;
import ca.tristan.easycommands.commands.prefix.PrefixExecutor;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

import java.awt.*;
import java.util.List;

public class HelpCmd extends SlashExecutor {

Expand Down Expand Up @@ -39,21 +41,25 @@ public void execute(EventData data) {
builder.addField("Slash Commands", "--------------------", false);
easyCommands.getExecutors().forEach((name, commandExecutor) -> {
if(commandExecutor instanceof SlashExecutor && !commandExecutor.isOwnerOnly() && !commandExecutor.getName().equals("help") && (commandExecutor.getDescription() != null || !commandExecutor.getDescription().isEmpty())) {
builder.addField("/" + commandExecutor.getName(), commandExecutor.getDescription(), false);
if(!commandExecutor.getAliases().contains(name)) {
builder.addField("/" + name, commandExecutor.getDescription(), false);
}
}
});
builder.addField("Prefix Commands", "--------------------", false);
easyCommands.getExecutors().forEach((name, commandExecutor) -> {
if(commandExecutor instanceof PrefixExecutor && !commandExecutor.isOwnerOnly() && !commandExecutor.getName().equals("help") && (commandExecutor.getDescription() != null || !commandExecutor.getDescription().isEmpty())) {
builder.addField(easyCommands.getPrefixCommands().getPrefix() + commandExecutor.getName(), commandExecutor.getDescription(), false);
if(!commandExecutor.getAliases().contains(name)) {
builder.addField(easyCommands.getPrefixCommands().getPrefix() + name, commandExecutor.getDescription(), false);
}
}
});
if(builder.getFields().isEmpty()) {
builder.addField("There's no command to show for this server.", "", false);
builder.setDescription("There's no command to show for this server.");
}else {
builder.setDescription("Here's a list of command you might be able to use on this server.");
}
builder.setFooter("This help message was generated by EasyCommands.", "https://raw.githubusercontent.com/FrostedCA/EasyCommands/master/LEAFSTACKv2.png");
builder.setFooter("This help message was generated by EasyCommands.", "https://raw.githubusercontent.com/FrostedCA/EasyCommands/master/ECLogo_new.png");
data.reply(builder.build(), true).queue();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ca.tristan.easycommands.commands.music;

import ca.tristan.easycommands.commands.EasyCommands;
import ca.tristan.easycommands.commands.EventData;
import ca.tristan.easycommands.commands.slash.SlashExecutor;
import ca.tristan.easycommands.embeds.MusicEB;
import ca.tristan.easycommands.lavaplayer.GuildMusicManager;
import ca.tristan.easycommands.lavaplayer.PlayerManager;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import core.GLA;
import genius.SongSearch;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.Channel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LyricsCmd extends SlashExecutor {

@Override
public String getName() {
return "lyrics";
}

@Override
public String getDescription() {
return "Retrieves the lyrics of the current playing music.";
}

@Override
public List<Channel> getAuthorizedChannels(JDA jda) {
List<Channel> channels = new ArrayList<>();
if(EasyCommands.getMusicChannel() != null) {
channels.add(EasyCommands.getMusicChannel());
}
return channels;
}

@Override
public void execute(EventData data) {
final GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(data.getGuild());
final AudioPlayer audioPlayer = musicManager.audioPlayer;

MusicEB musicEB = new MusicEB();

if(audioPlayer.getPlayingTrack() == null){
musicEB.getBuilder().setDescription("There is no music currently playing.");
data.reply(musicEB.getBuilder().build(), true).queue();
return;
}

String title = audioPlayer.getPlayingTrack().getInfo().title;

GLA gla = new GLA();
try {

SongSearch search = gla.search(title.toLowerCase().replaceAll("official", "").replaceAll("music", "").replaceAll("video", "").replaceAll("Audio", ""));

String url = search.getHits().isEmpty() ? "" : search.getHits().getFirst().getUrl();
if(url.isBlank()){
musicEB.getBuilder().setDescription("Sorry, I haven't found any lyrics for that song.");
data.reply(musicEB.getBuilder().build(), true).queue();
return;
}
musicEB.getBuilder().setDescription("Here's what I've found: " + url);
data.reply(musicEB.getBuilder().build(), true).queue();
} catch (IOException e) {
musicEB.getBuilder().setDescription("Sorry, I haven't found any lyrics for that song.");
data.reply(musicEB.getBuilder().build(), true).queue();
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package ca.tristan.easycommands.commands.music;

import ca.tristan.easycommands.commands.EasyCommands;
import ca.tristan.easycommands.commands.slash.SlashExecutor;
import ca.tristan.easycommands.commands.EventData;
import ca.tristan.easycommands.lavaplayer.GuildMusicManager;
import ca.tristan.easycommands.lavaplayer.PlayerManager;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.Channel;

import java.util.ArrayList;
import java.util.List;

public class NowPlayingCmd extends SlashExecutor {

Expand All @@ -18,6 +24,15 @@ public String getDescription() {
return "Shows the current playing track.";
}

@Override
public List<Channel> getAuthorizedChannels(JDA jda) {
List<Channel> channels = new ArrayList<>();
if(EasyCommands.getMusicChannel() != null) {
channels.add(EasyCommands.getMusicChannel());
}
return channels;
}

@Override
public void execute(EventData data) {
final GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(data.getGuild());
Expand Down
Loading

0 comments on commit 46ecc5b

Please sign in to comment.