Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite command system and write the tests #4

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions src/main/java/io/hypesquad/watsonbot/WatsonBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,26 @@

package io.hypesquad.watsonbot;

import io.hypesquad.watsonbot.commands.AbstractWatsonCommand;
import io.hypesquad.watsonbot.commands.WatsonExampleCommand;
import io.hypesquad.watsonbot.commands.WatsonHelpCommand;
import io.hypesquad.watsonbot.event.WatsonEventListener;
import sx.blah.discord.api.ClientBuilder;
import sx.blah.discord.api.IDiscordClient;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Represents the main class
*
* @author Kevin
*/
public class WatsonBot {

/**
* This stores all the commands.
*/
public static Map<String, AbstractWatsonCommand> commands = new ConcurrentHashMap<>();
private WatsonBot() {}

public static void main(final String... args) {
if (args.length < 1)
if (args.length < 1) {
throw new IllegalArgumentException("WatsonBot requires a token!");
}

IDiscordClient client = new ClientBuilder().withToken(args[0]).login();

client.getDispatcher().registerListener(new WatsonEventListener());

//Register the commands
registerCommands();
}

/**
* This method will register our commands.
*/
public static void registerCommands() {
// Add the commands
commands.put("help", new WatsonHelpCommand());
commands.put("example", new WatsonExampleCommand());
}
}

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/io/hypesquad/watsonbot/commands/ExampleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.hypesquad.watsonbot.commands;

import io.hypesquad.watsonbot.objects.command.ICommand;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;

/**
* This is a command to display how commands should be made
*
* @author Duncan
*/
public class ExampleCommand implements ICommand {
@Override
public void execute(final String invoke, final String[] args, final MessageReceivedEvent event) {
event.getChannel().sendMessage("This is an example");
}

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

@Override
public String getHelp() {
return "This is an example command\n" +
"Usage: `!" + getName() + "`";
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,19 @@

package io.hypesquad.watsonbot.event;

import io.hypesquad.watsonbot.WatsonBot;
import io.hypesquad.watsonbot.commands.AbstractWatsonCommand;
import io.hypesquad.watsonbot.managers.WatsonCommandManager;
import io.hypesquad.watsonbot.util.WatsonUtil;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;

import java.util.Arrays;
import java.util.Map;

/**
* Represents the global EventListener
*
* @author Kevin
*/
public class WatsonEventListener {

/**
* This is for easy access to your commands.
*/
private final transient Map<String, AbstractWatsonCommand> commands = WatsonBot.commands;
private static final WatsonCommandManager COMMAND_MANAGER = new WatsonCommandManager();

@EventSubscriber
public void onMessageReceivedEvent(MessageReceivedEvent event) {
Expand All @@ -47,18 +40,7 @@ public void onMessageReceivedEvent(MessageReceivedEvent event) {
if (!message.startsWith(prefix)) {
return;
}

//Handle command
final String[] split = message.substring(message.indexOf(prefix) + 1, message.length()).split(" ");
final String calledCommand = split[0];
final String[] args = Arrays.copyOfRange(split, 1, split.length);

// Check if the command exist and if it does, run it
if (commands.containsKey(calledCommand)) {
final boolean safe = commands.get(calledCommand).checkCommand(args, event);
if (safe) {
commands.get(calledCommand).executeCommand(args, event);
}
}
//Handle the command
COMMAND_MANAGER.dispatchCommand(prefix, event);
}
}
Loading