140 lines
3.5 KiB
Go
Raw Normal View History

2018-03-06 16:34:02 +00:00
package main
import (
"math/rand"
2018-03-06 16:34:02 +00:00
"strings"
"github.com/animenotifier/arn"
2018-04-19 20:15:05 +00:00
"github.com/animenotifier/arn/search"
2018-03-06 16:34:02 +00:00
"github.com/bwmarrin/discordgo"
)
2018-07-10 07:51:34 +00:00
var regions = map[string]string{
"africa": "465876853236826112",
"america": "465876808311635979",
"asia": "465876834031108096",
"australia": "465876893036707840",
"europe": "465876773029019659",
}
2018-03-06 16:34:02 +00:00
// OnMessageCreate is called every time a new message is created on any channel.
func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if msg.Author.ID == s.State.User.ID {
return
}
if msg.Content == "!help" || msg.Content == "!commands" {
2018-03-06 16:34:02 +00:00
s.ChannelMessageSend(msg.ChannelID, `
**!a** [anime search term]
2018-03-06 16:34:02 +00:00
**!animelist** [username]
**!play** [status text]
**!randomquote**
2018-07-09 10:38:44 +00:00
**!source**
**!region** [region]`)
2018-03-06 16:34:02 +00:00
}
// Has the bot been mentioned?
for _, user := range msg.Mentions {
if user.ID == discord.State.User.ID {
s.ChannelMessageSend(msg.ChannelID, msg.Author.Mention()+" :heart:")
return
}
}
// Anime search
if strings.HasPrefix(msg.Content, "!a ") {
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)
2018-03-06 16:34:02 +00:00
return
}
// Anime list of user
2018-03-06 16:34:02 +00:00
if strings.HasPrefix(msg.Content, "!animelist ") {
2018-07-02 07:24:04 +00:00
s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/+"+strings.Split(msg.Content, " ")[1]+"/animelist/watching")
2018-03-06 16:34:02 +00:00
return
}
// Play status
2018-03-06 16:34:02 +00:00
if strings.HasPrefix(msg.Content, "!play ") {
s.UpdateStatus(0, msg.Content[len("!play "):])
return
}
// Random quote
if msg.Content == "!randomquote" {
2018-07-02 07:22:37 +00:00
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())
2018-03-15 17:01:17 +00:00
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")
2018-04-15 10:13:19 +00:00
return
}
2018-07-09 11:40:48 +00:00
// Set the specific region role for the user
if strings.HasPrefix(msg.Content, "!region ") {
2018-07-10 07:51:34 +00:00
region := strings.ToLower(msg.Content[len("!region "):])
2018-07-09 11:40:48 +00:00
2018-07-10 07:51:34 +00:00
// 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
2018-07-09 11:40:48 +00:00
}
2018-07-10 07:51:34 +00:00
// Get the channel, this is used to get the guild ID
c, _ := s.Channel(msg.ChannelID)
2018-07-09 11:40:48 +00:00
2018-07-10 07:51:34 +00:00
// Check to see if user already has a region role
user, _ := s.GuildMember(c.GuildID, msg.Author.ID)
2018-07-09 12:01:40 +00:00
2018-07-10 07:51:34 +00:00
for _, role := range user.Roles {
match := false
2018-07-09 12:01:40 +00:00
2018-07-10 07:51:34 +00:00
// 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
2018-07-09 12:01:40 +00:00
break
}
}
2018-07-10 07:51:34 +00:00
if match {
break
2018-07-09 11:40:48 +00:00
}
2018-07-10 07:51:34 +00:00
}
2018-07-09 11:40:48 +00:00
2018-07-10 07:51:34 +00:00
// Try to set the role.
err := s.GuildMemberRoleAdd(c.GuildID, msg.Author.ID, regions[region])
2018-07-09 11:40:48 +00:00
2018-07-10 07:51:34 +00:00
if err != nil {
s.ChannelMessageSend(msg.ChannelID, "The region role could not be set!")
return
2018-07-09 11:40:48 +00:00
}
2018-07-10 07:51:34 +00:00
s.ChannelMessageSend(msg.ChannelID, "The "+region+" role has been set on your user!")
2018-07-09 11:40:48 +00:00
}
2018-07-10 07:51:34 +00:00
return
2018-03-06 16:34:02 +00:00
}