-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
222 additions
and
411 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 27 additions & 5 deletions
32
src/main/java/de/tosoxdev/tosoxjr/commands/CommandBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
package de.tosoxdev.tosoxjr.commands; | ||
|
||
import de.tosoxdev.tosoxjr.GenericCommandBase; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import net.dv8tion.jda.api.interactions.commands.build.OptionData; | ||
|
||
public abstract class CommandBase extends GenericCommandBase<MessageReceivedEvent> { | ||
public CommandBase(String name, String description) { | ||
super(name, description); | ||
import java.util.List; | ||
|
||
public abstract class CommandBase { | ||
private final String name; | ||
private final String description; | ||
private final List<OptionData> options; | ||
|
||
public CommandBase(String name, String description, List<OptionData> options) { | ||
this.name = name; | ||
this.description = description; | ||
this.options = options; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public List<OptionData> getOptions() { | ||
return options; | ||
} | ||
|
||
public abstract void handle(SlashCommandInteractionEvent event); | ||
} |
76 changes: 35 additions & 41 deletions
76
src/main/java/de/tosoxdev/tosoxjr/commands/CommandManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,50 @@ | ||
package de.tosoxdev.tosoxjr.commands; | ||
|
||
import de.tosoxdev.tosoxjr.GenericManagerBase; | ||
import de.tosoxdev.tosoxjr.Main; | ||
import de.tosoxdev.tosoxjr.commands.cat.CatCmd; | ||
import de.tosoxdev.tosoxjr.commands.csstats.CSStatsCmd; | ||
import de.tosoxdev.tosoxjr.commands.help.HelpCmd; | ||
import de.tosoxdev.tosoxjr.commands.list.ListCmd; | ||
import de.tosoxdev.tosoxjr.commands.ping.PingCmd; | ||
import de.tosoxdev.tosoxjr.commands.hangman.HangmanCmd; | ||
import de.tosoxdev.tosoxjr.commands.quote.QuoteCmd; | ||
import de.tosoxdev.tosoxjr.commands.say.SayCmd; | ||
import de.tosoxdev.tosoxjr.games.GameBase; | ||
import de.tosoxdev.tosoxjr.utils.Constants; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CommandManager { | ||
private final List<CommandBase> commands = new ArrayList<>(); | ||
|
||
public class CommandManager extends GenericManagerBase<CommandBase, MessageReceivedEvent> { | ||
public CommandManager() { | ||
addElement(new PingCmd()); | ||
addElement(new SayCmd()); | ||
addElement(new HelpCmd()); | ||
addElement(new ListCmd()); | ||
addElement(new QuoteCmd()); | ||
addElement(new CSStatsCmd()); | ||
addElement(new CatCmd()); | ||
addCommand(new SayCmd()); | ||
addCommand(new CatCmd()); | ||
addCommand(new QuoteCmd()); | ||
addCommand(new CSStatsCmd()); | ||
addCommand(new HangmanCmd()); | ||
} | ||
|
||
public List<CommandBase> getCommands() { | ||
return commands; | ||
} | ||
|
||
@Override | ||
public void handle(MessageReceivedEvent event) { | ||
// Check if message is valid | ||
if (!event.isFromGuild()) return; | ||
if (!event.getMessage().getContentDisplay().startsWith(Constants.BOT_PREFIX)) return; | ||
if (event.getAuthor().isBot()) return; | ||
if (event.isWebhookMessage()) return; | ||
|
||
// Remove prefix, split arguments | ||
String[] split = event.getMessage().getContentDisplay().substring(Constants.BOT_PREFIX.length()).split(" "); | ||
String command = split[0].toLowerCase(); | ||
CommandBase cmd = getElement(command); | ||
|
||
event.getChannel().sendTyping().queue(); | ||
|
||
if (cmd == null) { | ||
// Check for games | ||
GameBase game = Main.getGameManager().getElement(command); | ||
if (game == null) { | ||
event.getChannel().sendMessage("Seems like I don't know this command").queue(); | ||
return; | ||
} | ||
|
||
game.run(event); | ||
return; | ||
public void addCommand(CommandBase cmd) throws IllegalArgumentException { | ||
boolean cmdExists = commands.stream().anyMatch(i -> i.getName().equalsIgnoreCase(cmd.getName())); | ||
if (cmdExists) { | ||
throw new IllegalArgumentException("Found a duplicate item in the list"); | ||
} | ||
commands.add(cmd); | ||
} | ||
|
||
@NotNull | ||
public CommandBase getCommand(String name) { | ||
return commands.stream() | ||
.filter(cmd -> cmd.getName().equalsIgnoreCase(name)) | ||
.findFirst() | ||
.orElseThrow(); | ||
} | ||
|
||
public void handle(SlashCommandInteractionEvent event) { | ||
String command = event.getName(); | ||
CommandBase cmd = getCommand(command); | ||
cmd.handle(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.tosoxdev.tosoxjr.commands; | ||
|
||
import net.dv8tion.jda.api.events.Event; | ||
import net.dv8tion.jda.api.interactions.commands.build.OptionData; | ||
|
||
import java.util.List; | ||
|
||
public abstract class GameBase extends CommandBase { | ||
public GameBase(String name, String description, List<OptionData> options) { | ||
super(name, description, options); | ||
} | ||
|
||
public abstract void handleEvent(Event event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.