Improved error handling for the discord bot
This commit is contained in:
parent
dd36c852d3
commit
190a85fe08
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/akyoto/color"
|
||||
"github.com/animenotifier/notify.moe/bots/discord/commands"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
@ -27,13 +28,17 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
|
||||
}
|
||||
|
||||
if msg.Content == "!help" || msg.Content == "!commands" {
|
||||
s.ChannelMessageSend(msg.ChannelID, `
|
||||
_, err := s.ChannelMessageSend(msg.ChannelID, `
|
||||
**!a** [anime search term]
|
||||
**!animelist** [username]
|
||||
**!play** [status text]
|
||||
**!randomquote**
|
||||
**!source**
|
||||
**!verify** [username]`)
|
||||
|
||||
if err != nil {
|
||||
color.Red(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Has the bot been mentioned?
|
||||
|
@ -3,6 +3,7 @@ package commands
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/akyoto/color"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
@ -12,6 +13,11 @@ func AnimeList(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package commands
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/akyoto/color"
|
||||
"github.com/animenotifier/notify.moe/arn/search"
|
||||
"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."
|
||||
}
|
||||
|
||||
s.ChannelMessageSend(msg.ChannelID, message)
|
||||
_, err := s.ChannelMessageSend(msg.ChannelID, message)
|
||||
|
||||
if err != nil {
|
||||
color.Red(err.Error())
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package commands
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/akyoto/color"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
@ -12,6 +13,11 @@ func Play(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package commands
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/akyoto/color"
|
||||
"github.com/animenotifier/notify.moe/arn"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
@ -18,6 +19,11 @@ func RandomQuote(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package commands
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/akyoto/color"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
@ -21,7 +22,11 @@ func Roles(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
roles, _ := s.GuildRoles(guildID)
|
||||
|
||||
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
|
||||
|
@ -1,6 +1,7 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/akyoto/color"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
@ -10,6 +11,11 @@ func Source(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
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
|
||||
}
|
||||
|
@ -33,7 +33,12 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
discordTag := msg.Author.Username + "#" + msg.Author.Discriminator
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@ -45,17 +50,32 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
user, err := arn.GetUserByNick(arnUserName)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@ -63,14 +83,28 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
err = s.GuildMemberRoleAdd(guildID, msg.Author.ID, verifiedRole)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Give editor role
|
||||
if user.Role == "editor" {
|
||||
s.GuildMemberRoleAdd(guildID, msg.Author.ID, editorRole)
|
||||
s.GuildMemberRoleAdd(guildID, msg.Author.ID, staffRole)
|
||||
err := s.GuildMemberRoleAdd(guildID, msg.Author.ID, editorRole)
|
||||
|
||||
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
|
||||
@ -87,20 +121,36 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
} else {
|
||||
// Remove old region role
|
||||
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
|
||||
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
|
||||
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 {
|
||||
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
|
||||
@ -117,6 +167,11 @@ func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/akyoto/color"
|
||||
"github.com/animenotifier/notify.moe/arn"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
@ -41,7 +42,13 @@ func main() {
|
||||
|
||||
defer discord.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
|
||||
discord.AddHandler(OnMessageCreate)
|
||||
@ -49,7 +56,12 @@ func main() {
|
||||
discord.AddHandler(OnGuildMemberRemove)
|
||||
|
||||
// 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.")
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||
|
@ -21,6 +21,6 @@ func Get(ctx aero.Context) error {
|
||||
}
|
||||
|
||||
customCtx := ctx.(*middleware.OpenGraphContext)
|
||||
customCtx.OpenGraph = getOpenGraph(ctx, amv)
|
||||
customCtx.OpenGraph = getOpenGraph(amv)
|
||||
return ctx.HTML(components.AMVPage(amv, user))
|
||||
}
|
||||
|
@ -3,12 +3,11 @@ package amv
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/notify.moe/arn"
|
||||
"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{
|
||||
Tags: map[string]string{
|
||||
"og:title": amv.Title.ByUser(nil) + " (AMV)",
|
||||
|
Loading…
Reference in New Issue
Block a user