Skip to content

Commit

Permalink
Merge pull request #2 from madeyoga/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
madeyoga committed Sep 24, 2021
2 parents 801ca5d + 876dfb1 commit e08186b
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 26 deletions.
28 changes: 17 additions & 11 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import commands.HelpCommand;
import commands.PingCommand;
import commands.SayCommand;
import commands.music.*;
import interactions.ButtonEventHandler;
import exceptions.DuplicateCommandException;
import guild.GuildAudioManager;
import interactions.CommandCategory;
import interactions.CommandListener;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand All @@ -25,6 +27,9 @@ public static void main(String[] args) throws LoginException, DuplicateCommandEx
String prefix = scanner.nextLine().split(":", 2)[1].trim();
scanner.close();

CommandCategory audioCategory = new CommandCategory("Audio");
CommandCategory generalCategory = new CommandCategory("General");

JDABuilder builder = JDABuilder.createDefault(botToken);
configureMemoryUsage(builder);

Expand All @@ -33,17 +38,18 @@ public static void main(String[] args) throws LoginException, DuplicateCommandEx

CommandListener commandListener = new CommandListener(new ButtonEventHandler());
commandListener.setPrefix(prefix);
commandListener.addCommand(new SayCommand());
commandListener.addCommand(new PingCommand());
commandListener.addCommand(new JoinCommand());
commandListener.addCommand(new NowPlayingCommand(audioManager));
commandListener.addCommand(new PauseCommand(audioManager));
commandListener.addCommand(new PlayCommand(audioManager));
commandListener.addCommand(new RepeatModeCommand(audioManager));
commandListener.addCommand(new ShuffleQueueCommand(audioManager));
commandListener.addCommand(new SkipCommand(audioManager));
commandListener.addCommand(new StopCommand(audioManager));
commandListener.addCommand(new YoutubeSearchCommand(audioManager, searchCommandWaiter));
commandListener.addCommand(new SayCommand(generalCategory));
commandListener.addCommand(new PingCommand(generalCategory));
commandListener.addCommand(new JoinCommand(audioCategory));
commandListener.addCommand(new NowPlayingCommand(audioManager, audioCategory));
commandListener.addCommand(new PauseCommand(audioManager, audioCategory));
commandListener.addCommand(new PlayCommand(audioManager, audioCategory));
commandListener.addCommand(new RepeatModeCommand(audioManager, audioCategory));
commandListener.addCommand(new ShuffleQueueCommand(audioManager, audioCategory));
commandListener.addCommand(new SkipCommand(audioManager, audioCategory));
commandListener.addCommand(new StopCommand(audioManager, audioCategory));
commandListener.addCommand(new YoutubeSearchCommand(audioManager, searchCommandWaiter, audioCategory));
commandListener.addCommand(new HelpCommand(commandListener.getCommandMap()));

builder.addEventListeners(searchCommandWaiter);
builder.addEventListeners(commandListener);
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/commands/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package commands;

import interactions.Command;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class HelpCommand extends Command {
private final Map<String, Command> commandMap;
private EmbedBuilder embedBuilder;

public HelpCommand(Map<String, Command> commandMap) {
this.commandData = new CommandData("help", "Get command list");
// .addOption(OptionType.STRING, "command name",
// "Use this parameter to get command description", false);
this.commandMap = commandMap;
this.addDataToEmbedBuilder();
}

@Override
public void execute(SlashCommandEvent event) {
OptionMapping option = event.getOption("command name");
if (option == null) {
String avaUrl = event.getJDA().getSelfUser().getAvatarUrl();
this.embedBuilder.setThumbnail(avaUrl);
event.replyEmbeds(this.embedBuilder.build()).queue();
}
}

@Override
public void execute(MessageReceivedEvent event, String arguments) {
if (arguments.equals("")) {
String avaUrl = event.getJDA().getSelfUser().getAvatarUrl();
this.embedBuilder.setThumbnail(avaUrl);
event.getChannel().sendMessageEmbeds(this.embedBuilder.build()).queue();
}
}

private void addDataToEmbedBuilder() {
embedBuilder = new EmbedBuilder();

embedBuilder.setColor(new Color(100, 100, 255));
embedBuilder.setFooter(
"For additional help, feel free to open an issue on Github repo or Join the support server");

Map <String, String> categoryMap = new HashMap<>();
for (Command command : commandMap.values()) {
CommandData data = command.getCommandData();
String categoryName = command.getCategory().getName();
if (!categoryMap.containsKey(categoryName)) {
categoryMap.put(categoryName, "");
}
String currentValue = categoryMap.get(categoryName);
currentValue += String.format("`%s` ", command.getCommandData().getName());
categoryMap.put(categoryName, currentValue);
}
for (String categoryName : categoryMap.keySet()) {
embedBuilder.addField(categoryName, categoryMap.get(categoryName), false);
}

embedBuilder.addField(":tools: Helpful Links",
"[Report bugs](https://github.com/madeyoga/PortableAudioBot/issues) - [Support Server](https://discord.gg/Y8sB4ay)",
false);
}
}
4 changes: 3 additions & 1 deletion src/main/java/commands/PingCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package commands;

import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

public class PingCommand extends Command {

public PingCommand() {
public PingCommand(CommandCategory category) {
this.commandData = new CommandData("ping", "Makes the bot says Pong!");
this.category = category;
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/commands/SayCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands;

import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
Expand All @@ -13,15 +14,15 @@
import java.util.Map;

public class SayCommand extends Command {

Map<String, String[]> signatureCache;

public SayCommand() {
public SayCommand(CommandCategory category) {
this.signatureCache = new HashMap<>();
this.commandData = new CommandData("say","Makes the bot say what you tell it to")
.addOptions(new OptionData(OptionType.STRING,
"content","What the bot should say?", true)
);
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/JoinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import interactions.Command;
import guild.GuildAudioManager;
import interactions.CommandCategory;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

public class JoinCommand extends Command {

public JoinCommand() {
public JoinCommand(CommandCategory category) {
this.guildOnly = true;
this.commandData = new CommandData("join", "Join author voice channel");
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/NowPlayingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

public class NowPlayingCommand extends Command {
final GuildAudioManager audioManager;

public NowPlayingCommand(GuildAudioManager audioManager) {
public NowPlayingCommand(GuildAudioManager audioManager, CommandCategory category) {
this.audioManager = audioManager;
this.guildOnly = true;
this.commandData = new CommandData("song", "Shows current playing audio");
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/PauseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
Expand All @@ -12,10 +13,11 @@
public class PauseCommand extends Command {
final private GuildAudioManager audioManager;

public PauseCommand(GuildAudioManager audioManager) {
public PauseCommand(GuildAudioManager audioManager, CommandCategory category) {
this.audioManager = audioManager;
this.guildOnly = true;
this.commandData = new CommandData("pause", "Pause or unpause audio player");
this.category = category;
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/commands/music/PlayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import interactions.Command;
import guild.GuildAudioManager;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
Expand All @@ -12,14 +13,14 @@
public class PlayCommand extends Command {
GuildAudioManager audioManager;

public PlayCommand(GuildAudioManager audioManager) {
public PlayCommand(GuildAudioManager audioManager, CommandCategory category) {
this.guildOnly = true;
this.audioManager = audioManager;

this.commandData = new CommandData("play","Joins your voice channel and starts playing")
.addOptions(new OptionData(OptionType.STRING,
"query","Query can be an url or keywords", true)
);
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/RepeatModeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
Expand All @@ -11,10 +12,11 @@
public class RepeatModeCommand extends Command {
private final GuildAudioManager audioManager;

public RepeatModeCommand(GuildAudioManager audioManager) {
public RepeatModeCommand(GuildAudioManager audioManager, CommandCategory category) {
this.audioManager = audioManager;
this.guildOnly = true;
this.commandData = new CommandData("repeat", "Audio track will be added to the queue again after it finished playing");
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/ShuffleQueueCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
Expand All @@ -15,10 +16,11 @@
public class ShuffleQueueCommand extends Command {
private final GuildAudioManager audioManager;

public ShuffleQueueCommand(GuildAudioManager audioManager) {
public ShuffleQueueCommand(GuildAudioManager audioManager, CommandCategory category) {
this.audioManager = audioManager;
this.guildOnly = true;
this.commandData = new CommandData("shuffle", "Shuffles current queue state");
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/SkipCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
Expand All @@ -11,9 +12,10 @@
public class SkipCommand extends Command {
private final GuildAudioManager audioManager;

public SkipCommand(GuildAudioManager audioManager) {
public SkipCommand(GuildAudioManager audioManager, CommandCategory category) {
this.audioManager = audioManager;
this.commandData = new CommandData("skip", "Skip current playing audio, only author can skip.");
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/StopCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import interactions.Command;
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.CommandCategory;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
Expand All @@ -13,10 +14,11 @@
public class StopCommand extends Command {
private final GuildAudioManager audioManager;

public StopCommand(GuildAudioManager audioManager) {
public StopCommand(GuildAudioManager audioManager, CommandCategory category) {
this.audioManager = audioManager;
this.guildOnly = true;
this.commandData = new CommandData("stop", "Stop playing music and leave voice channel");
this.category = category;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/music/VolumeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
Expand All @@ -12,11 +13,12 @@
public class VolumeCommand extends Command {
private final GuildAudioManager audioManager;

public VolumeCommand(GuildAudioManager audioManager) {
public VolumeCommand(GuildAudioManager audioManager, CommandCategory category) {
this.audioManager = audioManager;
this.commandData = new CommandData("volume", "Check or change audio player volume")
.addOption(OptionType.NUMBER, "number",
"Provide a number to change volume", false);
this.category = category;
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/commands/music/YoutubeSearchCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import guild.GuildAudioManager;
import guild.GuildAudioState;
import interactions.Command;
import interactions.CommandCategory;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
Expand All @@ -22,12 +23,14 @@ public class YoutubeSearchCommand extends Command {
private final GuildAudioManager audioManager;
private final SearchCommandResponseWaiter waiter;

public YoutubeSearchCommand(GuildAudioManager audioManager, SearchCommandResponseWaiter waiter) {
public YoutubeSearchCommand(GuildAudioManager audioManager, SearchCommandResponseWaiter waiter,
CommandCategory category) {
this.audioManager = audioManager;
this.guildOnly = true;
this.waiter = waiter;
this.commandData = new CommandData("search", "Search by query from youtube")
.addOption(OptionType.STRING, "query", "a keywords", true);
this.category = category;
}

@Override
Expand Down

0 comments on commit e08186b

Please sign in to comment.