Skip to content

Commit

Permalink
passing DugaChatListener along to ChatCommands, to give bean access
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Jun 20, 2015
1 parent d65e018 commit 51d0491
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion grails-app/services/net/zomis/duga/DugaChatListener.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DugaChatListener implements InitializingBean {
void afterPropertiesSet() throws Exception {
assert !commands
commands = new ChatCommands(this)
scheduler.scheduleWithFixedDelay(new ListenTask(chatBot, '20298', commands), 3000)
scheduler.scheduleWithFixedDelay(new ListenTask(chatBot, '20298', commands, this), 3000)
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package net.zomis.duga.tasks

import net.zomis.duga.ChatCommands
import net.zomis.duga.DugaChatListener
import net.zomis.duga.chat.WebhookParameters

/**
* Delegate for running chat commands
*/
class ChatCommandDelegate {

private final ChatMessageIncoming message
private final ChatCommands handler
private final DugaChatListener bean

ChatCommandDelegate(ChatMessageIncoming chatMessageIncoming, ChatCommands chatCommands) {
ChatCommandDelegate(ChatMessageIncoming chatMessageIncoming, DugaChatListener bean) {
this.message = chatMessageIncoming
this.handler = chatCommands
this.bean = bean
}

Closure ping = {
Expand Down
8 changes: 5 additions & 3 deletions src/main/groovy/net/zomis/duga/tasks/ListenTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import com.gistlabs.mechanize.impl.MechanizeAgent
import groovy.json.JsonSlurper
import net.zomis.duga.ChatCommands
import net.zomis.duga.DugaBot
import net.zomis.duga.DugaChatListener
import net.zomis.duga.chat.WebhookParameters
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.SecureASTCustomizer
import org.springframework.beans.factory.annotation.Autowired

import static org.codehaus.groovy.syntax.Types.*

Expand All @@ -21,13 +21,15 @@ class ListenTask implements Runnable {
private final DugaBot bot
private final String room
private final WebhookParameters params
private final DugaChatListener bean
private final ChatCommands handler
private final GroovyShell groovyShell
private long lastHandledId
private long lastMessageTime
private MechanizeAgent agent

public ListenTask(DugaBot bot, String room, ChatCommands commandHandler) {
public ListenTask(DugaBot bot, String room, ChatCommands commandHandler, DugaChatListener bean) {
this.bean = bean
this.bot = bot
this.room = room
this.params = WebhookParameters.toRoom(room)
Expand Down Expand Up @@ -141,7 +143,7 @@ class ListenTask implements Runnable {
}

def botCommand(ChatMessageIncoming chatMessageIncoming) {
def delegate = new ChatCommandDelegate(chatMessageIncoming, handler)
def delegate = new ChatCommandDelegate(chatMessageIncoming, bean)

try {
DelegatingScript script = (DelegatingScript) groovyShell.parse(chatMessageIncoming.content.substring('@Duga '.length()))
Expand Down

0 comments on commit 51d0491

Please sign in to comment.