Skip to content

Commit

Permalink
doc'd
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Jan 17, 2016
1 parent 4910e19 commit f6ebab7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
4 changes: 2 additions & 2 deletions examples/bot/CommandBotExample.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bot.defaultPrefix = ["!", "$"] // can be a string or a list. i know it being a l
// a list of prefixes / a prefix
bot.addCommand new Command("hello"){
def run(Event e){
e.data.sendMessage("Hello there, ${e.data.author.mention}!")
e.data.sendMessage("Hello there, ${e.data.message.author.mention}!")
}
}

Expand All @@ -28,7 +28,7 @@ bot.addCommand new ClosureCommand({ Event e, Command c ->
// if you have two arguments, make sure to make the first one the event object,
// and the second one a command object.
// if you have one, make sure to make that one an event object.
e.data.sendMessage("Hello there, ${e.data.author.mention}!")
e.data.sendMessage("Hello there, ${e.data.message.author.mention}!")
}, "hello")

// here we login with the api and register our listeners.
Expand Down
4 changes: 4 additions & 0 deletions src/ml/hlaaftana/discordg/objects/API.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ class API{
return fields[key]
}

/**
* Triggers all listeners for an event.
* @param event - the event object to provide.
*/
void dispatchEvent(Event event){
this.listeners.each { Map.Entry<String, List<Closure>> entry ->
try{
Expand Down
3 changes: 3 additions & 0 deletions src/ml/hlaaftana/discordg/objects/Message.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Message extends Base{
*/
TextChannel getChannel() { return this.getTextChannel() }

/**
* @return a list of users who were mentioned in this message.
*/
List<User> getMentions(){ return this.object["mentions"].collect { new User(api, it) } }

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ml/hlaaftana/discordg/request/WSClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class WSClient{
@OnWebSocketMessage
void onMessage(Session session, String message) throws IOException{
def clos = {
if (api.ignorePresenceUpdate) return
Map content = JSONUtil.parse(message)
String type = content["t"]
if (api.ignorePresenceUpdate && type == "PRESENCE_UPDATE") return
Map data = content["d"]
int responseAmount = content["s"]
if (type.equals("READY") || type.equals("RESUMED")){
Expand Down
11 changes: 3 additions & 8 deletions src/ml/hlaaftana/discordg/util/RandomUtil.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,9 @@ class RandomUtil {
return [color.red, color.green, color.blue]
}

static boolean findStrings(String string, String...strings){
boolean returnThis = false
for (s in strings){
returnThis |= s in string
}
return returnThis
}

/**
* Registers a bunch of methods to help you with Discord formatting to the String meta class.
*/
static registerDiscordStringMethods(){
String.metaClass.removeFormatting = {
return delegate.replace("~", "\u200b~").replace("_", "\u200b_").replace("*", "\u200b*")
Expand Down
59 changes: 59 additions & 0 deletions src/ml/hlaaftana/discordg/util/bot/CommandBot.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import ml.hlaaftana.discordg.objects.API
import ml.hlaaftana.discordg.objects.Event
import ml.hlaaftana.discordg.util.Log

/**
* A simple bot implementation.
* @author Hlaaftana
*/
class CommandBot {
String name = "DiscordG|CommandBot"
API api
Expand All @@ -12,24 +16,46 @@ class CommandBot {
boolean acceptOwnCommands = false
private boolean loggedIn = false

/**
* @param api - The API object this bot should use.
* @param commands - A List of Commands you want to register right off the bat. Empty by default.
*/
CommandBot(API api, List commands=[]){
this.api = api
this.commands += commands
}

/**
* Adds a command.
* @param command - the command.
*/
def addCommand(Command command){
commands.add(command)
}

/**
* Adds a List of Commands.
* @param commands - the list of commands.
*/
def addCommands(List<Command> commands){
this.commands.addAll(commands)
}

/**
* Logs in with the API before initalizing. You don't have to type your email and password when calling #initalize.
* @param email - the email to log in with.
* @param password - the password to log in with.
*/
def login(String email, String password){
loggedIn = true
api.login(email, password)
}

/**
* Starts the bot. You don't have to enter any parameters if you ran #login already.
* @param email - the email to log in with.
* @param password - the password to log in with.
*/
def initialize(String email="", String password=""){
api.addListener("message create") { Event e ->
for (c in commands){
Expand Down Expand Up @@ -57,10 +83,18 @@ class CommandBot {
}
}

/**
* A command.
* @author Hlaaftana
*/
static abstract class Command{
List prefixes = []
List aliases = []

/**
* @param aliasOrAliases - A String or List of Strings of aliases this command will trigger with.
* @param prefixOrPrefixes - A String or List of Strings this command will be triggered by. Note that this is optional, and is CommandBot.defaultPrefix by default.
*/
Command(def aliasOrAliases, def prefixOrPrefixes=CommandBot.defaultPrefix){
if (aliasOrAliases instanceof List || aliasOrAliases instanceof Object[]){
aliases.addAll(aliasOrAliases)
Expand All @@ -74,6 +108,11 @@ class CommandBot {
}
}

/**
* Gets the text after the command trigger for this command.
* @param e - an event object.
* @return the arguments as a string.
*/
def args(Event e){
try{
for (p in prefixes){
Expand All @@ -88,12 +127,24 @@ class CommandBot {
}
}

/**
* Runs the command.
* @param e - an event object.
*/
abstract def run(Event e)
}

/**
* An implementation of Command with a string response.
* @author Hlaaftana
*/
static class ResponseCommand extends Command{
String response

/**
* @param response - a string to respond with to this command. <br>
* The rest of the parameters are Command's parameters.
*/
ResponseCommand(String response, def aliasOrAliases, def prefixOrPrefixes=CommandBot.defaultPrefix){
super(aliasOrAliases, prefixOrPrefixes)
this.response = response
Expand All @@ -104,9 +155,17 @@ class CommandBot {
}
}

/**
* An implementation of Command with a closure response.
* @author Hlaaftana
*/
static class ClosureCommand extends Command{
Closure response

/**
* @param response - a closure to respond with to this command. Can take one or two parameters. If it takes one, it has to be an Event object. If it takes two, the first one has to be an Event object, and the second one has to be a Command object. <br>
* The rest of the parameters are Command's parameters.
*/
ClosureCommand(Closure response, def aliasOrAliases, def prefixOrPrefixes=CommandBot.defaultPrefix){
super(aliasOrAliases, prefixOrPrefixes)
this.response = response
Expand Down

0 comments on commit f6ebab7

Please sign in to comment.