From 173a3c996d1b2b218e2adb6af940b02d9130f002 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 23 Nov 2019 16:49:53 +0900 Subject: [PATCH] Added new bot commands --- bots/discord/OnMessageCreate.go | 1 + bots/discord/commands/Did.go | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 bots/discord/commands/Did.go diff --git a/bots/discord/OnMessageCreate.go b/bots/discord/OnMessageCreate.go index 462b7c9d..93026372 100644 --- a/bots/discord/OnMessageCreate.go +++ b/bots/discord/OnMessageCreate.go @@ -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, diff --git a/bots/discord/commands/Did.go b/bots/discord/commands/Did.go new file mode 100644 index 00000000..69522fa0 --- /dev/null +++ b/bots/discord/commands/Did.go @@ -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 +}