Improved error handling for the discord bot

This commit is contained in:
Eduard Urbach 2019-06-05 14:56:12 +09:00
parent dd36c852d3
commit 190a85fe08
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
11 changed files with 130 additions and 24 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"github.com/akyoto/color"
"github.com/animenotifier/notify.moe/bots/discord/commands" "github.com/animenotifier/notify.moe/bots/discord/commands"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -27,13 +28,17 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
} }
if msg.Content == "!help" || msg.Content == "!commands" { if msg.Content == "!help" || msg.Content == "!commands" {
s.ChannelMessageSend(msg.ChannelID, ` _, err := s.ChannelMessageSend(msg.ChannelID, `
**!a** [anime search term] **!a** [anime search term]
**!animelist** [username] **!animelist** [username]
**!play** [status text] **!play** [status text]
**!randomquote** **!randomquote**
**!source** **!source**
**!verify** [username]`) **!verify** [username]`)
if err != nil {
color.Red(err.Error())
}
} }
// Has the bot been mentioned? // Has the bot been mentioned?

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"strings" "strings"
"github.com/akyoto/color"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -12,6 +13,11 @@ func AnimeList(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
return false return false
} }
s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/+"+strings.Split(msg.Content, " ")[1]+"/animelist/watching") _, err := s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/+"+strings.Split(msg.Content, " ")[1]+"/animelist/watching")
if err != nil {
color.Red(err.Error())
}
return true return true
} }

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"strings" "strings"
"github.com/akyoto/color"
"github.com/animenotifier/notify.moe/arn/search" "github.com/animenotifier/notify.moe/arn/search"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -25,6 +26,11 @@ func AnimeSearch(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
message = "Sorry, I couldn't find anything using that term." message = "Sorry, I couldn't find anything using that term."
} }
s.ChannelMessageSend(msg.ChannelID, message) _, err := s.ChannelMessageSend(msg.ChannelID, message)
if err != nil {
color.Red(err.Error())
}
return true return true
} }

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"strings" "strings"
"github.com/akyoto/color"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -12,6 +13,11 @@ func Play(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
return false return false
} }
s.UpdateStatus(0, msg.Content[len("!play "):]) err := s.UpdateStatus(0, msg.Content[len("!play "):])
if err != nil {
color.Red(err.Error())
}
return true return true
} }

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"math/rand" "math/rand"
"github.com/akyoto/color"
"github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/arn"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -18,6 +19,11 @@ func RandomQuote(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
}) })
quote := allQuotes[rand.Intn(len(allQuotes))] quote := allQuotes[rand.Intn(len(allQuotes))]
s.ChannelMessageSend(msg.ChannelID, "https://notify.moe"+quote.Link()) _, err := s.ChannelMessageSend(msg.ChannelID, "https://notify.moe"+quote.Link())
if err != nil {
color.Red(err.Error())
}
return true return true
} }

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/akyoto/color"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -21,7 +22,11 @@ func Roles(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
roles, _ := s.GuildRoles(guildID) roles, _ := s.GuildRoles(guildID)
for _, role := range roles { for _, role := range roles {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("%s: %s", role.ID, role.Name)) _, err := s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("%s: %s", role.ID, role.Name))
if err != nil {
color.Red(err.Error())
}
} }
return true return true

View File

@ -1,6 +1,7 @@
package commands package commands
import ( import (
"github.com/akyoto/color"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -10,6 +11,11 @@ func Source(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
return false 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") _, err := 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")
if err != nil {
color.Red(err.Error())
}
return true return true
} }

View File

@ -33,7 +33,12 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
discordTag := msg.Author.Username + "#" + msg.Author.Discriminator discordTag := msg.Author.Username + "#" + msg.Author.Discriminator
if msg.Content == "!verify" { if msg.Content == "!verify" {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("To verify your account, first add `%s` as your Discord account on https://notify.moe/settings/accounts, then type `!verify` followed by your username on notify.moe, e.g. `!verify MyName`", discordTag)) _, err := s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("To verify your account, first add `%s` as your Discord account on https://notify.moe/settings/accounts, then type `!verify` followed by your username on notify.moe, e.g. `!verify MyName`", discordTag))
if err != nil {
color.Red(err.Error())
}
return true return true
} }
@ -45,17 +50,32 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
user, err := arn.GetUserByNick(arnUserName) user, err := arn.GetUserByNick(arnUserName)
if err != nil { if err != nil {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("User `%s` doesn't seem to exist on notify.moe", arnUserName)) _, err := s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("User `%s` doesn't seem to exist on notify.moe", arnUserName))
if err != nil {
color.Red(err.Error())
}
return true return true
} }
if user.Accounts.Discord.Nick == "" { if user.Accounts.Discord.Nick == "" {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("You haven't set up your Discord account `%s` on https://notify.moe/settings/accounts yet", discordTag)) _, err := s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("You haven't set up your Discord account `%s` on https://notify.moe/settings/accounts yet", discordTag))
if err != nil {
color.Red(err.Error())
}
return true return true
} }
if user.Accounts.Discord.Nick != discordTag { if user.Accounts.Discord.Nick != discordTag {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Mismatching Discord accounts: `%s` and `%s`", user.Accounts.Discord.Nick, discordTag)) _, err := s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Mismatching Discord accounts: `%s` and `%s`", user.Accounts.Discord.Nick, discordTag))
if err != nil {
color.Red(err.Error())
}
return true return true
} }
@ -63,14 +83,28 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
err = s.GuildMemberRoleAdd(guildID, msg.Author.ID, verifiedRole) err = s.GuildMemberRoleAdd(guildID, msg.Author.ID, verifiedRole)
if err != nil { if err != nil {
s.ChannelMessageSend(msg.ChannelID, "There was an error adding the Verified role to your account!") _, err := s.ChannelMessageSend(msg.ChannelID, "There was an error adding the Verified role to your account!")
if err != nil {
color.Red(err.Error())
}
return true return true
} }
// Give editor role // Give editor role
if user.Role == "editor" { if user.Role == "editor" {
s.GuildMemberRoleAdd(guildID, msg.Author.ID, editorRole) err := s.GuildMemberRoleAdd(guildID, msg.Author.ID, editorRole)
s.GuildMemberRoleAdd(guildID, msg.Author.ID, staffRole)
if err != nil {
color.Red(err.Error())
}
err = s.GuildMemberRoleAdd(guildID, msg.Author.ID, staffRole)
if err != nil {
color.Red(err.Error())
}
} }
// Give region role // Give region role
@ -87,20 +121,36 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
} else { } else {
// Remove old region role // Remove old region role
for _, roleID := range regions { for _, roleID := range regions {
s.GuildMemberRoleRemove(guildID, msg.Author.ID, roleID) err := s.GuildMemberRoleRemove(guildID, msg.Author.ID, roleID)
if err != nil {
color.Red(err.Error())
}
} }
// Add new region role // Add new region role
s.GuildMemberRoleAdd(guildID, msg.Author.ID, regionRole) err := s.GuildMemberRoleAdd(guildID, msg.Author.ID, regionRole)
if err != nil {
color.Red(err.Error())
}
} }
} }
} }
// Give or remove supporter role // Give or remove supporter role
if user.IsPro() { if user.IsPro() {
s.GuildMemberRoleAdd(guildID, msg.Author.ID, supporterRole) err := s.GuildMemberRoleAdd(guildID, msg.Author.ID, supporterRole)
if err != nil {
color.Red(err.Error())
}
} else { } else {
s.GuildMemberRoleRemove(guildID, msg.Author.ID, supporterRole) err := s.GuildMemberRoleRemove(guildID, msg.Author.ID, supporterRole)
if err != nil {
color.Red(err.Error())
}
} }
// Update nickname to notify.moe nick // Update nickname to notify.moe nick
@ -117,6 +167,11 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
} }
// Send success message // Send success message
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("%s Thank you, you are now a verified member of the notify.moe community!", msg.Author.Mention())) _, err = s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("%s Thank you, you are now a verified member of the notify.moe community!", msg.Author.Mention()))
if err != nil {
color.Red(err.Error())
}
return true return true
} }

View File

@ -6,6 +6,7 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/akyoto/color"
"github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/arn"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -41,7 +42,13 @@ func main() {
defer discord.Close() defer discord.Close()
defer arn.Node.Close() defer arn.Node.Close()
defer discord.ChannelMessageSend(logChannel, "I'm feeling like shit today so I'm shutting down. B-baka!") defer func() {
_, err := discord.ChannelMessageSend(logChannel, "I'm feeling like shit today so I'm shutting down. B-baka!")
if err != nil {
color.Red(err.Error())
}
}()
// Receive events // Receive events
discord.AddHandler(OnMessageCreate) discord.AddHandler(OnMessageCreate)
@ -49,7 +56,12 @@ func main() {
discord.AddHandler(OnGuildMemberRemove) discord.AddHandler(OnGuildMemberRemove)
// Wait for a CTRL-C // Wait for a CTRL-C
discord.ChannelMessageSend(logChannel, "Hooray, I'm up again! Did you miss me?") _, err = discord.ChannelMessageSend(logChannel, "Hooray, I'm up again! Did you miss me?")
if err != nil {
color.Red(err.Error())
}
log.Printf("Tsundere is ready. Press CTRL-C to exit.") log.Printf("Tsundere is ready. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)

View File

@ -21,6 +21,6 @@ func Get(ctx aero.Context) error {
} }
customCtx := ctx.(*middleware.OpenGraphContext) customCtx := ctx.(*middleware.OpenGraphContext)
customCtx.OpenGraph = getOpenGraph(ctx, amv) customCtx.OpenGraph = getOpenGraph(amv)
return ctx.HTML(components.AMVPage(amv, user)) return ctx.HTML(components.AMVPage(amv, user))
} }

View File

@ -3,12 +3,11 @@ package amv
import ( import (
"strings" "strings"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/assets" "github.com/animenotifier/notify.moe/assets"
) )
func getOpenGraph(ctx aero.Context, amv *arn.AMV) *arn.OpenGraph { func getOpenGraph(amv *arn.AMV) *arn.OpenGraph {
openGraph := &arn.OpenGraph{ openGraph := &arn.OpenGraph{
Tags: map[string]string{ Tags: map[string]string{
"og:title": amv.Title.ByUser(nil) + " (AMV)", "og:title": amv.Title.ByUser(nil) + " (AMV)",