Skip to content

Commit

Permalink
added commandPrefix configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Jun 23, 2015
1 parent cc098af commit 300e205
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ In the directory `grails-app/conf`, create a file named `duga.groovy`
// API configuration
stackAPI = 'xxxxxxxxx'
githubAPI = 'xxxxxxxxx'
commandPrefix = '@Duga ' // chat messages that begins with this will be considered as commands

// Database configuration
adminDefaultPass = 'xxxxxxxxx' // default password for username 'admin'
Expand Down
2 changes: 2 additions & 0 deletions grails-app/services/net/zomis/duga/DugaChatListener.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.zomis.duga
import net.zomis.duga.tasks.ListenTask
import org.springframework.beans.factory.InitializingBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.env.Environment
import org.springframework.scheduling.TaskScheduler

/**
Expand All @@ -13,6 +14,7 @@ class DugaChatListener implements InitializingBean {
@Autowired TaskScheduler scheduler
@Autowired DugaBot chatBot
@Autowired DugaTasks tasks
@Autowired Environment environment

private ChatCommands commands

Expand Down
7 changes: 4 additions & 3 deletions src/main/groovy/net/zomis/duga/tasks/ListenTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import static org.codehaus.groovy.syntax.Types.*
class ListenTask implements Runnable {

private static final int NUM_MESSAGES = 10
private static final String DUGA_COMMAND = '@Duga do '

private final String commandPrefix
private final DugaBot bot
private final String room
private final WebhookParameters params
Expand All @@ -39,6 +39,7 @@ class ListenTask implements Runnable {
this.params = WebhookParameters.toRoom(room)
this.handler = commandHandler
this.agent = new MechanizeAgent()
this.commandPrefix = bean.environment.getProperty('commandPrefix', '@Duga ')

Binding binding = new Binding()
CompilerConfiguration cc = new CompilerConfiguration()
Expand Down Expand Up @@ -133,7 +134,7 @@ class ListenTask implements Runnable {
continue
}
def content = message.content
if (!content.startsWith(DUGA_COMMAND)) {
if (!content.startsWith(commandPrefix)) {
continue
}
if (!authorizedCommander(message)) {
Expand All @@ -159,7 +160,7 @@ class ListenTask implements Runnable {

def botCommand(ChatMessageIncoming chatMessageIncoming) {
try {
ChatCommandDelegate script = (ChatCommandDelegate) groovyShell.parse(chatMessageIncoming.content.substring(DUGA_COMMAND.length()))
ChatCommandDelegate script = (ChatCommandDelegate) groovyShell.parse(chatMessageIncoming.content.substring(commandPrefix.length()))
script.init(chatMessageIncoming, bean)
def result = script.run()
println 'Script ' + chatMessageIncoming + ' returned ' + result
Expand Down

0 comments on commit 300e205

Please sign in to comment.