Refactor bot command invocation
This commit is contained in:
parent
957de3856b
commit
009449316e
@ -1,21 +1,21 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"github.com/animenotifier/notify.moe/bots/discord/commands"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/animenotifier/arn"
|
|
||||||
|
|
||||||
"github.com/animenotifier/arn/search"
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var regions = map[string]string{
|
// Command represents a single bot command function signature.
|
||||||
"africa": "465876853236826112",
|
type Command func(*discordgo.Session, *discordgo.MessageCreate) bool
|
||||||
"america": "465876808311635979",
|
|
||||||
"asia": "465876834031108096",
|
var allCommands = []Command{
|
||||||
"australia": "465876893036707840",
|
commands.AnimeList,
|
||||||
"europe": "465876773029019659",
|
commands.AnimeSearch,
|
||||||
|
commands.Play,
|
||||||
|
commands.RandomQuote,
|
||||||
|
commands.Region,
|
||||||
|
commands.Source,
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnMessageCreate is called every time a new message is created on any channel.
|
// OnMessageCreate is called every time a new message is created on any channel.
|
||||||
@ -43,97 +43,12 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Anime search
|
// Has the user invoked a command?
|
||||||
if strings.HasPrefix(msg.Content, "!a ") {
|
for _, cmd := range allCommands {
|
||||||
term := msg.Content[len("!a "):]
|
if cmd(s, msg) {
|
||||||
animes := search.Anime(term, 3)
|
|
||||||
message := ""
|
|
||||||
|
|
||||||
for _, anime := range animes {
|
|
||||||
message += "https://notify.moe" + anime.Link() + "\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(animes) == 0 {
|
|
||||||
message = "Sorry, I couldn't find anything using that term."
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(msg.ChannelID, message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Anime list of user
|
|
||||||
if strings.HasPrefix(msg.Content, "!animelist ") {
|
|
||||||
s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/+"+strings.Split(msg.Content, " ")[1]+"/animelist/watching")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Play status
|
|
||||||
if strings.HasPrefix(msg.Content, "!play ") {
|
|
||||||
s.UpdateStatus(0, msg.Content[len("!play "):])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Random quote
|
|
||||||
if msg.Content == "!randomquote" {
|
|
||||||
allQuotes := arn.FilterQuotes(func(quote *arn.Quote) bool {
|
|
||||||
return !quote.IsDraft && quote.IsValid()
|
|
||||||
})
|
|
||||||
|
|
||||||
quote := allQuotes[rand.Intn(len(allQuotes))]
|
|
||||||
s.ChannelMessageSend(msg.ChannelID, "https://notify.moe"+quote.Link())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GitHub source of the bot
|
|
||||||
if msg.Content == "!source" {
|
|
||||||
s.ChannelMessageSend(msg.ChannelID, msg.Author.Mention()+" B-baaaaaaaka! Y..you...you want to...TOUCH MY CODE?!\n\nhttps://github.com/animenotifier/notify.moe/tree/go/bots/discord")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the specific region role for the user
|
|
||||||
if strings.HasPrefix(msg.Content, "!region ") {
|
|
||||||
region := strings.ToLower(msg.Content[len("!region "):])
|
|
||||||
|
|
||||||
// check to make sure the region is in the region map
|
|
||||||
if _, ok := regions[region]; !ok {
|
|
||||||
s.ChannelMessageSend(msg.ChannelID, "This is not a region!")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the channel, this is used to get the guild ID
|
|
||||||
c, _ := s.Channel(msg.ChannelID)
|
|
||||||
|
|
||||||
// Check to see if user already has a region role
|
|
||||||
user, _ := s.GuildMember(c.GuildID, msg.Author.ID)
|
|
||||||
|
|
||||||
for _, role := range user.Roles {
|
|
||||||
match := false
|
|
||||||
|
|
||||||
// We also need to loop through our map because discord doesn't return roles as names
|
|
||||||
// but rather IDs.
|
|
||||||
for _, id := range regions {
|
|
||||||
if role == id {
|
|
||||||
// Remove the role and set match to true.
|
|
||||||
s.GuildMemberRoleRemove(c.GuildID, msg.Author.ID, id)
|
|
||||||
match = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if match {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to set the role.
|
|
||||||
err := s.GuildMemberRoleAdd(c.GuildID, msg.Author.ID, regions[region])
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
s.ChannelMessageSend(msg.ChannelID, "The region role could not be set!")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(msg.ChannelID, "The "+region+" role has been set on your user!")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
17
bots/discord/commands/AnimeList.go
Normal file
17
bots/discord/commands/AnimeList.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AnimeList shows the link for the anime list of a user.
|
||||||
|
func AnimeList(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||||
|
if strings.HasPrefix(msg.Content, "!animelist ") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/+"+strings.Split(msg.Content, " ")[1]+"/animelist/watching")
|
||||||
|
return true
|
||||||
|
}
|
30
bots/discord/commands/AnimeSearch.go
Normal file
30
bots/discord/commands/AnimeSearch.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/animenotifier/arn/search"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AnimeSearch shows the link for the anime list of a user.
|
||||||
|
func AnimeSearch(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||||
|
if strings.HasPrefix(msg.Content, "!a ") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
term := msg.Content[len("!a "):]
|
||||||
|
animes := search.Anime(term, 3)
|
||||||
|
message := ""
|
||||||
|
|
||||||
|
for _, anime := range animes {
|
||||||
|
message += "https://notify.moe" + anime.Link() + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(animes) == 0 {
|
||||||
|
message = "Sorry, I couldn't find anything using that term."
|
||||||
|
}
|
||||||
|
|
||||||
|
s.ChannelMessageSend(msg.ChannelID, message)
|
||||||
|
return true
|
||||||
|
}
|
17
bots/discord/commands/Play.go
Normal file
17
bots/discord/commands/Play.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Play changes the status of the bot.
|
||||||
|
func Play(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||||
|
if strings.HasPrefix(msg.Content, "!play ") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
s.UpdateStatus(0, msg.Content[len("!play "):])
|
||||||
|
return true
|
||||||
|
}
|
23
bots/discord/commands/RandomQuote.go
Normal file
23
bots/discord/commands/RandomQuote.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/animenotifier/arn"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RandomQuote shows a random quote.
|
||||||
|
func RandomQuote(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||||
|
if msg.Content != "!randomquote" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
allQuotes := arn.FilterQuotes(func(quote *arn.Quote) bool {
|
||||||
|
return !quote.IsDraft && quote.IsValid()
|
||||||
|
})
|
||||||
|
|
||||||
|
quote := allQuotes[rand.Intn(len(allQuotes))]
|
||||||
|
s.ChannelMessageSend(msg.ChannelID, "https://notify.moe"+quote.Link())
|
||||||
|
return true
|
||||||
|
}
|
66
bots/discord/commands/Region.go
Normal file
66
bots/discord/commands/Region.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var regions = map[string]string{
|
||||||
|
"africa": "465876853236826112",
|
||||||
|
"america": "465876808311635979",
|
||||||
|
"asia": "465876834031108096",
|
||||||
|
"australia": "465876893036707840",
|
||||||
|
"europe": "465876773029019659",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Region sets the specific region role for the user.
|
||||||
|
func Region(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||||
|
if strings.HasPrefix(msg.Content, "!region ") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
region := strings.ToLower(msg.Content[len("!region "):])
|
||||||
|
|
||||||
|
// Check to make sure the region is in the region map
|
||||||
|
if _, ok := regions[region]; !ok {
|
||||||
|
s.ChannelMessageSend(msg.ChannelID, "This is not a region!")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the channel, this is used to get the guild ID
|
||||||
|
c, _ := s.Channel(msg.ChannelID)
|
||||||
|
|
||||||
|
// Check to see if user already has a region role
|
||||||
|
user, _ := s.GuildMember(c.GuildID, msg.Author.ID)
|
||||||
|
|
||||||
|
for _, role := range user.Roles {
|
||||||
|
match := false
|
||||||
|
|
||||||
|
// We also need to loop through our map because Discord doesn't
|
||||||
|
// return roles as names but rather IDs.
|
||||||
|
for _, id := range regions {
|
||||||
|
if role == id {
|
||||||
|
// Remove the role and set match to true
|
||||||
|
s.GuildMemberRoleRemove(c.GuildID, msg.Author.ID, id)
|
||||||
|
match = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if match {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to set the role
|
||||||
|
err := s.GuildMemberRoleAdd(c.GuildID, msg.Author.ID, regions[region])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
s.ChannelMessageSend(msg.ChannelID, "The region role could not be set!")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
s.ChannelMessageSend(msg.ChannelID, "Set region "+region+" for your account!")
|
||||||
|
return true
|
||||||
|
}
|
15
bots/discord/commands/Source.go
Normal file
15
bots/discord/commands/Source.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Source shows the link for the Discord bot's source code.
|
||||||
|
func Source(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||||
|
if msg.Content != "!source" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
s.ChannelMessageSend(msg.ChannelID, msg.Author.Mention()+" B-baaaaaaaka! Y..you...you want to...TOUCH MY CODE?!\n\nhttps://github.com/animenotifier/notify.moe/tree/go/bots/discord")
|
||||||
|
return true
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user