Skip to content

Set listeners in a loop #7

@kiliankoe

Description

@kiliankoe

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 😊

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions