fix: Keep calm and report panics

This prevents random crashes within Milen's code.
master
Luther Wen Xu 2020-05-31 11:09:58 +07:00
parent 2ec00273c1
commit ce43bd5864
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
3 changed files with 50 additions and 0 deletions

@ -1,6 +1,7 @@
package commands package commands
import ( import (
"fmt"
"strings" "strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -26,6 +27,18 @@ func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot { if m.Author.Bot {
return return
} }
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
if val, ok := r.(error); ok {
util.ReportError(dg, val)
} else {
util.ReportError(dg, fmt.Errorf("%T: %v", r, r))
}
}
}()
split := strings.Split(m.Content, " ") split := strings.Split(m.Content, " ")
prefix := strings.ToLower(split[0]) prefix := strings.ToLower(split[0])

@ -1,6 +1,8 @@
package level package level
import ( import (
"fmt"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db" "gitea.teamortix.com/chanbakjsd/Milen/db"
@ -14,6 +16,17 @@ func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {
return return
} }
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
if val, ok := r.(error); ok {
util.ReportError(dg, val)
} else {
util.ReportError(dg, fmt.Errorf("%T: %v", r, r))
}
}
}()
prevTime := db.GetLastActive(m.Author.ID) prevTime := db.GetLastActive(m.Author.ID)
nextTime, err := discordgo.SnowflakeTimestamp(m.ID) nextTime, err := discordgo.SnowflakeTimestamp(m.ID)
if err != nil { if err != nil {

@ -1,6 +1,8 @@
package reactrole package reactrole
import ( import (
"fmt"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db" "gitea.teamortix.com/chanbakjsd/Milen/db"
@ -8,6 +10,17 @@ import (
) )
func EventAdd(dg *discordgo.Session, r *discordgo.MessageReactionAdd) { func EventAdd(dg *discordgo.Session, r *discordgo.MessageReactionAdd) {
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
if val, ok := r.(error); ok {
util.ReportError(dg, val)
} else {
util.ReportError(dg, fmt.Errorf("%T: %v", r, r))
}
}
}()
member, err := util.GetMember(dg, r.GuildID, r.UserID) member, err := util.GetMember(dg, r.GuildID, r.UserID)
if err != nil { if err != nil {
util.ReportError(dg, err) util.ReportError(dg, err)
@ -27,6 +40,17 @@ func EventAdd(dg *discordgo.Session, r *discordgo.MessageReactionAdd) {
} }
func EventRemove(dg *discordgo.Session, r *discordgo.MessageReactionRemove) { func EventRemove(dg *discordgo.Session, r *discordgo.MessageReactionRemove) {
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
if val, ok := r.(error); ok {
util.ReportError(dg, val)
} else {
util.ReportError(dg, fmt.Errorf("%T: %v", r, r))
}
}
}()
member, err := util.GetMember(dg, r.GuildID, r.UserID) member, err := util.GetMember(dg, r.GuildID, r.UserID)
if err != nil { if err != nil {
util.ReportError(dg, err) util.ReportError(dg, err)