canopeas/discord/insult.go

59 lines
1.1 KiB
Go

package discord
import (
"fmt"
r "math/rand"
"strings"
"time"
)
func init() {
commands["insult"] = command{
validate: func(cmd commandInit) bool {
return true
},
run: func(cmd commandInit) (string, error) {
if !memeRateLimit.Try() {
return "", nil
}
fields := strings.Fields(cmd.message.Content)
var target string
if len(fields) > 1 {
target = strings.Join(fields[1:], " ")
} else if cmd.message.Member.Nick != "" {
target = cmd.message.Member.Nick
} else {
target = cmd.message.Author.Username
}
insult := fmt.Sprintf("%s, your %s looks like %s, you %s %s.",
target,
random(cmd.config.Insult.Targets),
random(cmd.config.Insult.Comparisons),
random(cmd.config.Insult.Adjectives),
random(cmd.config.Insult.Nouns),
)
sendMessage(cmd.session, cmd.message.ChannelID, insult, true)
return "", nil
},
help: "Insult someone!",
}
}
var rand = r.New(r.NewSource(time.Now().Unix()))
func random(list []string) string {
size := len(list)
if size == 0 {
return ""
} else if size == 1 {
return list[0]
}
idx := rand.Intn(size)
return list[idx]
}