feat: added Command system, added SetupFeatureCommand

This commit is contained in:
limited_dev 2022-09-28 23:03:48 +02:00
parent 30622adb10
commit fec77bfbab
11 changed files with 195 additions and 10 deletions

View file

@ -1,9 +1,11 @@
package de.limited_dev.lil_judd;
import de.limited_dev.lil_judd.commands.components.CommandManager;
import de.limited_dev.lil_judd.listeners.GuildStateListener;
import de.limited_dev.lil_judd.listeners.ReadyListener;
import de.limited_dev.lil_judd.listeners.SlashCommandInteractionListener;
import de.limited_dev.lil_judd.util.Logger;
import de.limited_dev.lil_judd.util.SlashCommandHelper;
import de.limited_dev.lil_judd.util.managers.TokenManager;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
@ -30,6 +32,8 @@ public class Main {
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.build();
CommandManager.registerCommands();
jda.addEventListener(new ReadyListener());
jda.addEventListener(new SlashCommandInteractionListener());
jda.addEventListener(new GuildStateListener());

View file

@ -0,0 +1,20 @@
package de.limited_dev.lil_judd.commands;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.commands.components.Command;
import de.limited_dev.lil_judd.commands.components.Option;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class InfoCommand extends Command {
public InfoCommand() {
super("info", "Let me tell you about me", null);
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
EmbeddedMessageHelper.sendSimpleOneLiner(event,
"Bot information","I'm li'l Judd.\nI was created to help you & your Splatoon competitive Team.\nContact me with \"/\"-commands",
"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fvignette.wikia.nocookie.net%2Fsplatoon%2Fimages%2Fa%2Fa3%2FLittle_Judd_Animated.gif%2Frevision%2Flatest%3Fcb%3D20170605172120&f=1&nofb=1&ipt=f6197a3fdaf8bd6755347ba4e4c67a91320ff047969a9fa1ef4ee3bc88840ffe&ipo=images");
}
}

View file

@ -0,0 +1,19 @@
package de.limited_dev.lil_judd.commands;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.commands.components.Command;
import de.limited_dev.lil_judd.commands.components.Option;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class PingCommand extends Command {
public PingCommand() {
super("ping", "Pong! Get the Ping to my base", null);
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
EmbeddedMessageHelper.sendSimpleOneLiner(event, "Connection speed", "My connection to base takes **" + Main.getJda().getGatewayPing() + "ms**", null);
}
}

View file

@ -0,0 +1,22 @@
package de.limited_dev.lil_judd.commands;
import de.limited_dev.lil_judd.commands.components.Command;
import de.limited_dev.lil_judd.commands.components.Option;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SetupFeatureCommand extends Command {
public SetupFeatureCommand() {
super("setup", "Setup a Feature", new Option[]{ new Option(null, "Feature name", "Des", false)});
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
if(!event.getGuild().getMemberById(event.getUser().getId()).hasPermission(Permission.ADMINISTRATOR)){
EmbeddedMessageHelper.sendSimpleOneLiner(event, "You do not have the Permission", "Sorry, but you don't have the Permission to run this command", null);
return;
}
}
}

View file

@ -0,0 +1,46 @@
package de.limited_dev.lil_judd.commands.components;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.commands.components.Option;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class Command {
private final String name;
private final String description;
private final Option[] options;
public Command(String name, String description, Option[] options){
this.name = name;
this.description = description;
this.options = options;
}
public void onSlashCommandInteraction(SlashCommandInteractionEvent event){
}
protected void sendReply(SlashCommandInteractionEvent event, String msg){
event.reply(msg).queue();
}
public String getName() {
return name.toLowerCase();
}
public String getDescription() {
return description;
}
public Option[] getOptions() {
return options;
}
public boolean hasOptions(){
return options != null;
}
public int getAmountOfOptions(){
return options.length;
}
}

View file

@ -0,0 +1,22 @@
package de.limited_dev.lil_judd.commands.components;
import de.limited_dev.lil_judd.commands.InfoCommand;
import de.limited_dev.lil_judd.commands.PingCommand;
import de.limited_dev.lil_judd.commands.SetupFeatureCommand;
import java.util.concurrent.CopyOnWriteArrayList;
public class CommandManager {
private static CopyOnWriteArrayList<Command> commands = new CopyOnWriteArrayList<>();
public static void registerCommands(){
commands.add(new PingCommand());
commands.add(new InfoCommand());
commands.add(new SetupFeatureCommand());
}
public static CopyOnWriteArrayList<Command> getCommands() {
return commands;
}
}

View file

@ -0,0 +1,34 @@
package de.limited_dev.lil_judd.commands.components;
import net.dv8tion.jda.api.interactions.commands.OptionType;
public class Option {
private final OptionType type;
private final String name;
private final String description;
private final boolean optional;
public Option(OptionType type, String name, String description, boolean optional){
this.type = type;
this.name = name;
this.description = description;
this.optional = optional;
}
public OptionType getType() {
return type;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public boolean isOptional() {
return optional;
}
}

View file

@ -1,6 +1,8 @@
package de.limited_dev.lil_judd.listeners;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.commands.components.Command;
import de.limited_dev.lil_judd.commands.components.CommandManager;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -10,11 +12,10 @@ public class SlashCommandInteractionListener extends ListenerAdapter {
@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
if(event.getName().equals("ping")){
event.reply("My connection to the home base takes **" + Main.getJda().getGatewayPing() + "ms**").queue();
}
else if(event.getName().equals("info")){
EmbeddedMessageHelper.sendSimpleOneLiner(event, Main.getJda().getSelfUser().getName(), "Bot information","I'm li'l Judd. I'm better then him, and you know it.", "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fvignette.wikia.nocookie.net%2Fsplatoon%2Fimages%2Fa%2Fa3%2FLittle_Judd_Animated.gif%2Frevision%2Flatest%3Fcb%3D20170605172120&f=1&nofb=1&ipt=f6197a3fdaf8bd6755347ba4e4c67a91320ff047969a9fa1ef4ee3bc88840ffe&ipo=images");
for(Command c : CommandManager.getCommands()){
Main.getLgr().info("New Command: /" + event.getName());
if(event.getName().equals(c.getName()))
c.onSlashCommandInteraction(event);
}
}
}

View file

@ -0,0 +1,5 @@
package de.limited_dev.lil_judd.util;
public class Constants {
public static final String CreatorID = "372703841151614976";
}

View file

@ -1,5 +1,6 @@
package de.limited_dev.lil_judd.util;
import de.limited_dev.lil_judd.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@ -9,16 +10,17 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class EmbeddedMessageHelper {
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss | dd/MM/yyyy");
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy @ HH:mm:ss");
public static void sendSimpleOneLiner(SlashCommandInteractionEvent event, String author, String title, String description, String thumbnailURL){
public static void sendSimpleOneLiner(SlashCommandInteractionEvent event, String title, String description, String thumbnailURL){
LocalDateTime now = LocalDateTime.now();
EmbedBuilder eb = new EmbedBuilder();
eb.setAuthor(author);
eb.setAuthor(Main.getJda().getSelfUser().getName());
eb.setTitle(title);
eb.setColor(Color.ORANGE);
eb.setDescription(description);
if(thumbnailURL != null)
eb.setThumbnail(thumbnailURL);
eb.setFooter(">" + dtf.format(now) + " - " + event.getUser().getName() + "#" + event.getUser().getDiscriminator());
event.replyEmbeds(eb.build()).queue();

View file

@ -1,14 +1,24 @@
package de.limited_dev.lil_judd.util;
import de.limited_dev.lil_judd.commands.components.Command;
import de.limited_dev.lil_judd.commands.components.CommandManager;
import de.limited_dev.lil_judd.commands.components.Option;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
public class SlashCommandHelper {
public static void addSlashCommandsToGuild(Guild g){
g.updateCommands().addCommands(
Commands.slash("ping", "Pong! Display the Ping"),
Commands.slash("info", "Shows Info about me")
Commands.slash("info", "Shows Info about me"),
Commands.slash("setup", "Setup a feature")
.addOptions(
new OptionData(OptionType.STRING, "feature", "The Feature you want to setup")
.addChoice("Send Time finder", "timefinder")
)
).queue();
}
}