Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "strings"
- )
- // Command is a chat command for the bot.
- type Command struct {
- Name string `json:"name"`
- Response string `json:"response"`
- }
- func main() {
- commands := make(map[string]*Command)
- // add a command to test against
- commands["music"] = &Command{
- Name: "music",
- Response: "play that funky music!",
- }
- // faking what the message would be from twitch chat.
- message := "!music"
- // check the message
- if response, ok := Check(commands, message); ok {
- // send the response
- fmt.Printf("%s\n\r", response)
- } else {
- // no command was found in the message.
- fmt.Println("No command found.")
- }
- }
- // Check validates a message for a command and returns a response.
- func Check(commands map[string]*Command, message string) (string, bool) {
- // take the first word from the message
- firstWord := strings.Split(message, " ")[0]
- // check if the word starts with an exclamation mark
- // and has at least one other character
- if strings.HasPrefix(firstWord, "!") && len(message) > 1 {
- // remove the exclamation mark from the word
- word := firstWord[1:]
- // perform an ok check on the map to see if it exists, and return
- // the response for it
- if cmd, ok := commands[word]; ok {
- return cmd.Response, true
- }
- }
- // we didn't find a command, so return no response and false.
- return "", false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement