136 lines
3.2 KiB
Go
Raw Normal View History

2017-06-20 16:06:41 +00:00
package main
import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/animenotifier/arn"
"github.com/bwmarrin/discordgo"
)
// Session provides access to the Discord session.
var discord *discordgo.Session
func main() {
var err error
discord, _ = discordgo.New()
2017-06-27 14:23:57 +00:00
discord.Token = "Bot " + arn.APIKeys.Discord.Token
2017-06-20 16:06:41 +00:00
// Verify a Token was provided
if discord.Token == "" {
log.Println("You must provide a Discord authentication token.")
return
}
// Verify the Token is valid and grab user information
discord.State.User, err = discord.User("@me")
if err != nil {
log.Printf("Error fetching user information: %s\n", err)
}
// Open a websocket connection to Discord
err = discord.Open()
if err != nil {
log.Printf("Error opening connection to Discord, %s\n", err)
}
defer discord.Close()
// Receive messages
discord.AddHandler(onMessage)
// Wait for a CTRL-C
log.Printf("Tsundere is ready. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}
// This function will be called every time a new message is created on any channel.
func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
if m.Content == "!commands" {
s.ChannelMessageSend(m.ChannelID, `
**!user** [username]
**!anime** [id]
**!animelist** [username]
**!tag** [forum tag]`)
}
2017-06-30 16:36:40 +00:00
// Has the bot been mentioned?
for _, user := range m.Mentions {
if user.ID == discord.State.User.ID {
s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" :heart:")
return
}
}
2017-06-20 16:06:41 +00:00
if strings.HasPrefix(m.Content, "!user ") {
s.ChannelMessageSend(m.ChannelID, "https://notify.moe/+"+strings.Split(m.Content, " ")[1])
return
}
if strings.HasPrefix(m.Content, "!anime ") {
s.ChannelMessageSend(m.ChannelID, "https://notify.moe/anime/"+strings.Split(m.Content, " ")[1])
return
}
if strings.HasPrefix(m.Content, "!animelist ") {
s.ChannelMessageSend(m.ChannelID, "https://notify.moe/+"+strings.Split(m.Content, " ")[1]+"/animelist")
return
}
if strings.HasPrefix(m.Content, "!tag ") {
s.ChannelMessageSend(m.ChannelID, "https://notify.moe/forum/"+strings.ToLower(strings.Split(m.Content, " ")[1]))
return
}
2017-06-20 21:58:20 +00:00
2017-10-21 18:49:14 +00:00
if strings.HasPrefix(m.Content, "!play ") {
s.UpdateStatus(0, strings.Split(m.Content, " ")[1])
return
}
2017-06-20 21:58:20 +00:00
if strings.HasPrefix(m.Content, "!s ") {
term := m.Content[len("!s "):]
2017-11-08 13:36:51 +00:00
users, animes, posts, threads, tracks := arn.Search(term, 3, 3, 3, 3, 3)
2017-06-20 21:58:20 +00:00
message := ""
2017-09-30 10:18:21 +00:00
for _, user := range users {
2017-06-20 22:02:26 +00:00
message += "https://notify.moe" + user.Link() + "\n"
2017-06-20 21:58:20 +00:00
}
2017-09-30 10:18:21 +00:00
for _, anime := range animes {
2017-06-20 22:02:26 +00:00
message += "https://notify.moe" + anime.Link() + "\n"
2017-06-20 21:58:20 +00:00
}
2017-09-30 10:18:21 +00:00
for _, post := range posts {
message += "https://notify.moe" + post.Link() + "\n"
}
for _, thread := range threads {
message += "https://notify.moe" + thread.Link() + "\n"
}
2017-11-08 13:36:51 +00:00
for _, track := range tracks {
message += "https://notify.moe" + track.Link() + "\n"
}
2017-09-30 10:18:21 +00:00
if len(users) == 0 && len(animes) == 0 && len(posts) == 0 && len(threads) == 0 {
message = "Sorry, I couldn't find anything using that term."
2017-06-20 21:58:20 +00:00
}
s.ChannelMessageSend(m.ChannelID, message)
return
}
2017-06-20 16:06:41 +00:00
}