Added new bot commands

This commit is contained in:
Eduard Urbach 2019-11-23 16:49:53 +09:00
parent 3a3867cc21
commit 173a3c996d
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 82 additions and 0 deletions

View File

@ -13,6 +13,7 @@ type Command func(*discordgo.Session, *discordgo.MessageCreate) bool
var allCommands = []Command{
commands.AnimeList,
commands.AnimeSearch,
commands.Did,
commands.Play,
commands.RandomQuote,
commands.Roles,

View File

@ -0,0 +1,81 @@
package commands
import (
"fmt"
"regexp"
"strings"
"github.com/akyoto/color"
"github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/arn/search"
"github.com/bwmarrin/discordgo"
)
var (
watchedAnimeRegex = regexp.MustCompile(`did (.*?) watch (.*?)\?`)
)
// Did answers some questions with the pattern "Did ...?".
func Did(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
if !strings.HasPrefix(msg.Content, "!did ") {
return false
}
matches := watchedAnimeRegex.FindStringSubmatch(msg.Content)
if len(matches) < 3 {
_, err := s.ChannelMessageSend(msg.ChannelID, "I don't understand that question")
if err != nil {
color.Red(err.Error())
}
return true
}
userName := matches[1]
animeName := matches[2]
user, err := arn.GetUser(userName)
if err != nil {
_, err := s.ChannelMessageSend(msg.ChannelID, "User not found")
if err != nil {
color.Red(err.Error())
}
return true
}
results := search.Anime(animeName, 1)
if len(results) == 0 {
_, err := s.ChannelMessageSend(msg.ChannelID, "Anime not found")
if err != nil {
color.Red(err.Error())
}
return true
}
anime := results[0]
animeList := user.AnimeList()
if animeList.Contains(anime.ID) {
_, err := s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Yes, %s has watched %s.", user.Nick, anime.Title.Canonical))
if err != nil {
color.Red(err.Error())
}
} else {
_, err := s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("No, %s hasn't watched %s.", user.Nick, anime.Title.Canonical))
if err != nil {
color.Red(err.Error())
}
}
return true
}