Skip to content

Commit

Permalink
Fix initial guild create event launch
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Dec 9, 2017
1 parent 8f7a3ba commit 55bdab0
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 34 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'application'

version = "5.0.1"
version = "5.0.2"
group = "hlaaftana"
archivesBaseName = "DiscordG"
mainClassName = "hlaaftana.discordg.DSLMain"
Expand Down
5 changes: 4 additions & 1 deletion src/main/groovy/hlaaftana/discordg/Client.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Client extends User {
NEW_CHANNEL: 'CHANNEL_CREATE', CHANNEL_UPDATED: 'CHANNEL_UPDATE',
CHANNEL_DELETED: 'CHANNEL_DELETE', BAN: 'GUILD_BAN_ADD',
UNBAN: 'GUILD_BAN_REMOVE', GUILD: 'GUILD_CREATE',
GUILD_JOIN: 'GUILD_CREATE', GUILD_JOINED: 'GUILD_CREATE',
NEW_GUILD: 'GUILD_CREATE', JOIN_GUILD: 'GUILD_CREATE', JOINED_GUILD: 'GUILD_CREATE',
GUILD_CREATE: 'GUILD_CREATE', GUILD_CREATED: 'GUILD_CREATE',
GUILD_UPDATED: 'GUILD_UPDATE', GUILD_UPDATE: 'GUILD_UPDATE',
GUILD_DELETE: 'GUILD_DELETE', GUILD_DELETED: 'GUILD_DELETE',
Expand Down Expand Up @@ -190,7 +192,8 @@ class Client extends User {
]

List<DiscordRawWSListener> rawListeners = []
@Delegate(excludes = ['getClass', 'equals']) ListenerSystem listenerSystem = new ParentListenerSystem(this)
@Delegate(excludes = ['hashCode', 'equals', 'toString', 'listenerError', 'parseEvent'])
ListenerSystem<Map<String, Object>> listenerSystem = new ParentListenerSystem<>(this)
HTTPClient http
WSClient ws
Cache<String, Object> readyData = Cache.empty(this)
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/hlaaftana/discordg/DiscordG.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import groovy.transform.CompileStatic

@CompileStatic
class DiscordG {
static final String VERSION = '4.2.0'
static final String VERSION = '5.0.2'
static final String GITHUB = 'https://github.com/hlaaftana/DiscordG'
static final String USER_AGENT = "groovy/$GroovySystem.version DiscordBot ($GITHUB, $VERSION)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package hlaaftana.discordg.logic
import groovy.transform.CompileStatic

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

def listenerError(event, Throwable ex, Closure closure, data){
def listenerError(event, Throwable ex, Closure closure, T data){
throw new ListenerException(event, ex, closure, data)
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/main/groovy/hlaaftana/discordg/logic/ListenerSystem.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package hlaaftana.discordg.logic
import groovy.transform.CompileStatic

@CompileStatic
abstract class ListenerSystem {
abstract class ListenerSystem<T> {
Map<Object, List<Closure>> listeners = [:]

abstract parseEvent(param)

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

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

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

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

def dispatchEvent(type, data){
def dispatchEvent(type, T data){
def x = listeners[parseEvent(type)]
if (null != x) for (l in x){
def a = (Closure) l.clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package hlaaftana.discordg.logic
import groovy.transform.CompileStatic

@CompileStatic
class ParentListenerSystem extends ListenerSystem {
class ParentListenerSystem<T> extends ListenerSystem<T> {
def parent
ParentListenerSystem(parent){ this.parent = parent }

def parseEvent(param){
parent.invokeMethod('parseEvent', param)
}

def listenerError(event, Throwable ex, Closure closure, data){
def listenerError(event, Throwable ex, Closure closure, T data){
parent.invokeMethod('listenerError', [event, ex, closure, data])
}
}
18 changes: 11 additions & 7 deletions src/main/groovy/hlaaftana/discordg/net/WSClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ class WSClient extends WebSocketAdapter {
}
if (guildCreate) {
client.log.info 'Waiting for guilds.', client.log.name + 'WS'
guildCreatingState = LoadState.LOADING
long ass = System.currentTimeMillis()
while (client.anyUnavailableGuilds()) {
if (System.currentTimeMillis() - ass >= client.guildTimeout) {
Expand All @@ -151,8 +150,8 @@ class WSClient extends WebSocketAdapter {
return
}
}
guildCreatingState = LoadState.LOADED
}
guildCreatingState = LoadState.LOADED
loaded = true
while (!client.loaded) { Thread.sleep 10 }
dispatch = true
Expand All @@ -173,8 +172,12 @@ class WSClient extends WebSocketAdapter {
/"channel_id":$data.channel_id,"id":$i,"bulk":true}}/)
}
}
boolean guildCreateInitial = false
if (type == 'CHANNEL_CREATE') Channel.construct(client, data)
else if (type == 'GUILD_CREATE') Guild.construct(client, data)
else if (type == 'GUILD_CREATE') {
guildCreateInitial = client.guildCache.containsKey(data.id)
Guild.construct(client, data)
}
for (l in client.rawListeners) l.fire(type, data)
if (!client.listenerSystem.listeners[type]) return
data.rawType = type
Expand All @@ -200,10 +203,9 @@ class WSClient extends WebSocketAdapter {
data.sendFile = ((Channel) data.channel).&sendFile
}
if (type.startsWith('PRESENCE')) data.presence = ((Guild) data.guild).presence(data.user)
Map event = new HashMap(data)
Map<String, Object> event = new HashMap<>(data)
Thread.start("$type-${messageCounts[op][type]}"){
client.dispatchEvent(type == 'GUILD_CREATE' && guildCreatingState ==
LoadState.LOADING ? 'INITIAL_GUILD_CREATE' : type, event) }
client.dispatchEvent(guildCreateInitial ? 'INITIAL_GUILD_CREATE' : type, event) }
Thread.start("ALL-${((Map<String, Integer>) messageCounts[op]).values().sum()}"){
client.dispatchEvent('ALL', event) }
}
Expand All @@ -214,6 +216,7 @@ class WSClient extends WebSocketAdapter {
heartbeats = 0
identify()
} else if (op == 10) {
guildCreatingState = LoadState.LOADING
client.readyData.putAll((Map) content.d)
if (justReconnected){
dispatch = true
Expand Down Expand Up @@ -247,7 +250,8 @@ class WSClient extends WebSocketAdapter {

void onWebSocketClose(int code, String reason){
client.log.info "Connection closed. Reason: $reason, code: $code", client.log.name + 'WS'
Thread.start { client.dispatchEvent('CLOSE', [code: code, reason: reason, json: [code: code, reason: reason]]) }
Thread.start { client.dispatchEvent('CLOSE', new HashMap<String, Object>(
code: code, reason: reason, json: [code: code, reason: reason])) }
if (keepAliveThread){
keepAliveThread.interrupt()
keepAliveThread = null
Expand Down
56 changes: 47 additions & 9 deletions src/main/groovy/hlaaftana/discordg/util/bot/CommandBot.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CommandBot implements Triggerable {
Closure commandRunnerListener
Closure exceptionListener
Closure<String> formatter
ListenerSystem listenerSystem = new BasicListenerSystem()
ListenerSystem<BotEventData> listenerSystem = new BasicListenerSystem<>()
String token

CommandBot(Map<String, Object> config){
Expand Down Expand Up @@ -92,17 +92,16 @@ class CommandBot implements Triggerable {
* @param password - the password to log in with.
*/
void initialize() {
exceptionListener = listenerSystem.addListener(Events.EXCEPTION) { CommandEventData d ->
((Exception) d.getProperty('exception')).printStackTrace()
exceptionListener = listenerSystem.addListener(Events.EXCEPTION) { BotExceptionEventData d ->
d.exception.printStackTrace()
log.error 'Command threw exception'
}
commandRunnerListener = listenerSystem.addListener(Events.COMMAND) { CommandEventData d ->
commandRunnerListener = listenerSystem.addListener(Events.COMMAND) { BotCommandEventData d ->
try {
d.command.call(d)
d.command.call(d.commandData)
d.command.uses += 1
} catch (ex) {
CommandEventData c = d.clone()
c.setProperty('exception', ex)
def c = new BotExceptionEventData(this, d, ex)
listenerSystem.dispatchEvent(Events.EXCEPTION, c)
}
}
Expand All @@ -120,11 +119,11 @@ class CommandBot implements Triggerable {
ced.captures = c.captures(msg)
ced.allCaptures = c.allCaptures(msg)

listenerSystem.dispatchEvent(Events.COMMAND, ced)
listenerSystem.dispatchEvent(Events.COMMAND, new BotCommandEventData(this, ced))
}
}

if (!anyPassed) listenerSystem.dispatchEvent(Events.NO_COMMAND, data)
if (!anyPassed) listenerSystem.dispatchEvent(Events.NO_COMMAND, new BotMapEventData(this, data))
}
listenerSystem.dispatchEvent(Events.INITIALIZE, null)
if (null != token && !loggedIn) login(token)
Expand All @@ -148,6 +147,45 @@ class CommandBot implements Triggerable {
}
}

@CompileStatic
class BotEventData {
CommandBot bot

BotEventData(CommandBot bot) {
this.bot = bot
}
}

@CompileStatic
class BotMapEventData extends BotEventData {
Map data

BotMapEventData(CommandBot bot, Map data) {
super(bot)
this.data = data
}
}

class BotCommandEventData extends BotEventData {
@Delegate CommandEventData commandData

BotCommandEventData(CommandBot bot, CommandEventData commandData) {
super(bot)
this.commandData = commandData
}
}

class BotExceptionEventData extends BotEventData {
Exception exception
@Delegate BotCommandEventData commandEventData

BotExceptionEventData(CommandBot bot, BotCommandEventData commandEventData, Exception exception) {
super(bot)
this.commandEventData = commandEventData
this.exception = exception
}
}

@CompileStatic
class CommandType {
private static quote(CommandPattern aot){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class CommandEventData {
extra.put name, value
}

Message formatted(Channel chan = channel, String content) {
Message formatted(Channel chan = message.channel, String content) {
(Message) chan.invokeMethod('sendMessage', command.parent.formatter.call(content))
}

Message sendMessage(Channel chan = channel, ...arguments) {
Message sendMessage(Channel chan = message.channel, ...arguments) {
(Message) chan.invokeMethod('sendMessage', arguments)
}

Message respond(Channel chan = channel, ...arguments) {
Message respond(Channel chan = message.channel, ...arguments) {
(Message) chan.invokeMethod('sendMessage', arguments)
}

Expand Down

0 comments on commit 55bdab0

Please sign in to comment.