Skip to content

Commit

Permalink
5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Dec 5, 2017
1 parent 29f3394 commit 68c949e
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 339 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ dependencies {
compile 'org.abstractj.kalium:kalium:0.4.0'*/
}

version = "4.2.0"
version = "5.0.0"
group = "com.github.hlaaftana"
archivesBaseName = "DiscordG"
15 changes: 6 additions & 9 deletions examples/ChannelListener.groovy
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import hlaaftana.discordg.objects.*
import hlaaftana.discordg.*

Client client = DiscordG.withToken("token")
client = DiscordG.withToken args[0]

client.listener(Events.CHANNEL){
client.listener('channel') {
if (channel.text && !channel.private)
channel.sendMessage("Hello there, new channel!")
}

client.listener(Events.CHANNEL_DELETE){
if (server)
server.sendMessage("Looks like $channel.type channel \"$channel.name\" was deleted.")
client.listener('channel deleted') {
if (guild) guild.sendMessage("Looks like $channel.type channel \"$channel.name\" was deleted.")
}

client.listener(Events.CHANNEL_UPDATE){
client.listener('channel changed') {
if (channel.text)
channel.sendMessage("Seems this channel changed. I like it.")
if (channel.voice)
server.sendMessage("Seems $channel.mention changed. I like it.")
else guild.sendMessage("Seems $channel.mention changed. I like it.")
}
15 changes: 7 additions & 8 deletions examples/MemberListener.groovy
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import hlaaftana.discordg.objects.*
import hlaaftana.discordg.*

Client client = DiscordG.withToken("token")
Client client = DiscordG.withToken args[0]

client.listener(Events.MEMBER){
server.sendMessage("Welcome to the server, $member.mention!")
client.listener('member') {
guild.sendMessage("Welcome to the guild, $member.mention!")
}

client.listener(Events.MEMBER_LEFT){
server.sendMessage("Aww, $member.mention left the server.")
client.listener('member left') {
guild.sendMessage("Aww, $member.mention left the guild.")
}

client.listener(Events.MEMBER_UPDATE){
server.sendMessage("I see your roles changed, $member.mention.")
client.listener('member changed') {
guild.sendMessage("I see you changed, $member.mention.")
}
10 changes: 3 additions & 7 deletions examples/PingPong.groovy
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import hlaaftana.discordg.objects.*
import hlaaftana.discordg.*


Client client = DiscordG.withToken("token")
Client client = DiscordG.withToken args[0]

/*
* Names like "message" and "sendMessage" directly correspond to values in the ".listener" method,
* but need to be gathered from the first argument of the closure in the ".addListener" method.
*/
client.listener(Events.MESSAGE){
if (message.content.startsWith("!ping")){
sendMessage("Pong!")
}
client.listener('message') {
if (content.startsWith("!ping")) respond "Pong!"
}
19 changes: 9 additions & 10 deletions examples/PingPongPlus.groovy
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hlaaftana.discordg.objects.*
import hlaaftana.discordg.*

// This example doesn't really teach you anything about the API, just wanted to help beginners.
Expand All @@ -7,17 +6,17 @@ Client client
// No type is defined here, so methods can use this variable. (Scripts only)
prefix = "!"

boolean isMessageCommand(String message, String command){
static boolean isMessageCommand(String message, String command){
// We're checking if the lowercase message plus a space starts with the prefix
// for the commands, the lowercase command plus another space.
// The reason we do the toLowerCase()s are because we want the command to not be
// case-sensitive. Remove those if you want it to be case sensitive.
// The reason we are adding a space is because if we didn't and the command string
// was "ping", "!pingpong" would trigger as well.
(message + " ").toLowerCase().startsWith(prefix + command.toLowerCase() + " ")
(message + " ").toLowerCase().startsWith(prefix.toString() + command.toLowerCase() + " ")
}

String getCommandArgs(String message, String command){
static String getCommandArgs(String message, String command){
// If our message doesn't contain a space after the command, we would be returning
// a string with -1 length which would throw an exception. If we catch an exception
// after this, its reason is because of no arguments, so we return an empty string.
Expand All @@ -26,17 +25,17 @@ String getCommandArgs(String message, String command){
// command plus a space until the end of the string.
// Therefore the arguments of "!ping already" (considering our command
// is "ping") would be "already".
message.substring((prefix + command + " ").length())
}catch (ex){
message.substring("$prefix$command ".length())
}catch (ignored){
""
}
}

client = DiscordG.withToken("token")
client.listener(Events.MESSAGE){
client = DiscordG.withToken args[0]
client.listener('message'){
// Refer to isMessageCommand above to understand how it works.
if (isMessageCommand(message.content, "ping")){
if (isMessageCommand(content, "ping")){
// Refer to getCommandArgs above to understand how it works.
sendMessage("Pong! In addition to ${prefix}ping, you said " + getCommandArgs(message.content, "ping"))
sendMessage("Pong! In addition to ${prefix}ping, you said " + getCommandArgs(content, "ping"))
}
}
15 changes: 7 additions & 8 deletions examples/ServerListener.groovy
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import hlaaftana.discordg.objects.*
import hlaaftana.discordg.*

Client client = DiscordG.withToken("token")
Client client = DiscordG.withToken args[0]

client.listener(Events.SERVER){
server.sendMessage("Hello there, new server!")
client.listener 'guild', {
guild.sendMessage("Hello there, new guild!")
}

client.listener(Events.SERVER_DELETE){
println "It seems I left or was banned in/kicked out of $server.name."
client.listener 'guild deleted', {
println "It seems I left or was banned in/kicked out of $guild.name."
}

client.listener(Events.SERVER_UPDATE){
server.sendMessage("Seems this server updated.")
client.listener 'guild changed', {
guild.sendMessage("Seems this guild updated.")
}
35 changes: 21 additions & 14 deletions examples/bot/CommandBotExample.groovy
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import hlaaftana.discordg.util.bot.CommandBot
import hlaaftana.discordg.util.bot.CommandEventData

// triggers here mean the same thing as prefixes, buty ou can change it to suffixes
CommandBot bot = new CommandBot(triggers: ["!", "\$"]) // notice how we don't log in yet
CommandBot bot = new CommandBot(triggers: ["!", "\$"])

// the command's only alias is "hello", and can also be a list.
// do note that after "hello" i can add a list or string as
// a list of custom triggers / a trigger
// the variables are the variables in the event data and
// methods inside Command which take
// a Message or the event data as arguments
bot.command("hello"){
sendMessage("Hello there, $author.mention!")
bot.command("hello") {
respond "Hello there, $author.mention!"
}

// here we login with the api and register our listeners.
// note that you can login before initializing and then call bot.initalize
// without any arguments.
bot.initialize("token")
bot.formatter = { "| $it |" }

bot.command(["say", "repeat", ~/echo+/]) {
respond arguments
}

bot.command "mfw", category: "meme", {
assert command.info.category == "meme"
respond ">mfw $arguments"
delete()
}

bot.command("whatsmyname") {
respond username
}.extraArgs.username = { CommandEventData it -> it.private ? it.member.nick : it.author.name }

bot.initialize args[0] // token
30 changes: 0 additions & 30 deletions src/main/build.gradle

This file was deleted.

1 change: 1 addition & 0 deletions src/main/groovy/hlaaftana/discordg/Client.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Client extends User {
String getEmail(){ userObject.getOrDefault('email', this.@email) }
String password
boolean confirmedBot
void setBot(boolean x) { if (x && !confirmedBot) confirmedBot = true }

// if the key is a string, it calls .replace
// if the key is a pattern, it calls .replaceAll
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/hlaaftana/discordg/DiscordG.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package hlaaftana.discordg

import groovy.transform.CompileStatic

@CompileStatic
class DiscordG {
static final String VERSION = '4.2.0'
static final String GITHUB = 'https://github.com/hlaaftana/DiscordG'
Expand Down
10 changes: 5 additions & 5 deletions src/main/groovy/hlaaftana/discordg/Permissions.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Permissions {
static final Permissions VOICE_ALL_TRUE = new Permissions(0b0000011111100000000000000011001)
static final Permissions ROLE_ALL_TRUE = new Permissions(0b1111111111101111111110001111111)
static final Permissions PRIVATE_CHANNEL = new Permissions(0b0000000000000011101110000000000)
long value
int value

Permissions(Map<String, Boolean> defaults) { for (e in defaults) each { set(e.key, e.value) } }
Permissions(v = 0){ value = v as long }
Permissions(v = 0){ value = v as int }

Map<BitOffsets, Boolean> getOffsetMap(){
Map<BitOffsets, Boolean> r = new HashMap<>()
Expand Down Expand Up @@ -94,17 +94,17 @@ class Permissions {
switch (target){
case int:
case Integer:
return value as int
return value
case long:
case Long:
return value
return (long) value
default:
super.asType(target)
}
}

String toString(){ map.toString() }
int toInteger(){ value as int }
int toInteger() { value }

static enum BitOffsets {
CREATE_INSTANT_INVITE('createInstantInvite', 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DiscordListCache<T extends DiscordObject> extends Cache<String, Map<String
}

T at(id) {
class_.newInstance(client, DiscordObject.id(id))
class_.newInstance(client, get(DiscordObject.id(id)))
}

List<Map<String, Object>> rawList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import groovy.transform.CompileStatic

@CompileStatic
class BasicListenerSystem extends ListenerSystem {
def parseEvent(param){ param }
def parseEvent(param) { param }

def listenerError(event, Throwable ex, Closure closure, data){
throw new ListenerException(event, ex, closure, data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class ListenerSystem {

abstract listenerError(event, Throwable ex, Closure closure, data)

Closure submit(event, boolean temporary = false, Closure closure){
Closure submit(event, boolean temporary = false, @DelegatesTo(Map) Closure closure){
addListener(event, temporary, closure)
}

Expand All @@ -22,7 +22,7 @@ abstract class ListenerSystem {
closure
}

Closure listen(event, boolean temporary = false, Closure closure){
Closure listen(event, boolean temporary = false, @DelegatesTo(Map) Closure closure){
Closure ass
ass = { Map d, Closure internal ->
//d['rawClosure'] = closure
Expand All @@ -31,6 +31,7 @@ abstract class ListenerSystem {
copy.delegate = d
copy.parameterTypes.size() == 2 ? copy(copy.delegate, internal) : copy(copy.delegate)
}
ass.resolveStrategy = Closure.DELEGATE_FIRST
addListener(event, temporary, ass)
}

Expand All @@ -47,7 +48,8 @@ abstract class ListenerSystem {
}

def dispatchEvent(type, data){
for (l in listeners[parseEvent(type)]){
def x = listeners[parseEvent(type)]
if (null != x) for (l in x){
def a = (Closure) l.clone()
try{
if (a.parameterTypes.length > 1) a.call(data, l)
Expand Down
Loading

0 comments on commit 68c949e

Please sign in to comment.