Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set listeners in a loop #7

Open
kiliankoe opened this issue Sep 23, 2016 · 2 comments
Open

Set listeners in a loop #7

kiliankoe opened this issue Sep 23, 2016 · 2 comments

Comments

@kiliankoe
Copy link
Contributor

I have a bot that has a few very simple responses. I tried doing the following to register multiple listeners on the bot calling initSimple from within a InitPlugin func and passing it the reference to the bot.

var simpleResponses = map[string]string {
    "listenfor1": "response1",
    "listenfor2": "response2",
}

func initSimple(bot *slick.Bot) {
    for listen, response := range simpleResponses {
        bot.Listen(&slick.Listener{
            Contains:           listen,
            MessageHandlerFunc: func(l *slick.Listener, m *slick.Message) {
                m.Reply(response)
            },
        })
    }
}

The problem is that every "query" into simpleResponses returns the response from the last response, e.g. "response2" in this case. I've also tried generating a listener for every case and handing them to the bot one for one, but that leads to the same result.

Any help on how to achieve this would be much appreciated 😊

@kiliankoe kiliankoe mentioned this issue Sep 23, 2016
24 tasks
@kiliankoe
Copy link
Contributor Author

I currently implemented this in my bot by using the same handler and checking msg.Match[0] inside the handler. This requires a regex match though, which is only a minor tradeoff.

I'm still unsure why the loop with closures didn't work and would love to figure that out.

@abourget
Copy link
Owner

abourget commented Feb 1, 2017

That is weird indeed. Do you have a more complete example? This code seems fine, you're not even passing pointers, only copies of strings, and you create a new Listener on each iteration. Hmm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants