From 61c0fdd8df01ce8f94ab31393225e25c045b49db Mon Sep 17 00:00:00 2001 From: Dot-Rar Date: Tue, 24 Mar 2020 17:31:23 +0000 Subject: [PATCH] fix panic --- database/table/rolescache.go | 11 ++++++--- utils/discord/objects/member.go | 4 ++- utils/permissionutils.go | 5 +++- utils/types/int64stringslice.go | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 utils/types/int64stringslice.go diff --git a/database/table/rolescache.go b/database/table/rolescache.go index b0807cc..6f4fca2 100644 --- a/database/table/rolescache.go +++ b/database/table/rolescache.go @@ -1,6 +1,8 @@ package table -import "github.com/TicketsBot/TicketsGo/database" +import ( + "github.com/TicketsBot/GoPanel/database" +) type CachedRole struct { AssociationId int `gorm:"column:ASSOCIATIONID;primary_key;auto_increment"` @@ -14,7 +16,7 @@ func (CachedRole) TableName() string { } func DeleteRoles(guildId, userId int64) { - database.Db.Where(CachedRole{ + database.Database.Where(CachedRole{ GuildId: guildId, UserId: userId, }).Delete(CachedRole{}) @@ -22,7 +24,7 @@ func DeleteRoles(guildId, userId int64) { // TODO: Cache invalidation func CacheRole(guildId, userId, roleId int64) { - database.Db.Create(&CachedRole{ + database.Database.Create(&CachedRole{ GuildId: guildId, UserId: userId, RoleId: roleId, @@ -31,7 +33,7 @@ func CacheRole(guildId, userId, roleId int64) { func GetCachedRoles(guildId, userId int64, res chan []int64) { var rows []CachedRole - database.Db.Where(&CachedRole{ + database.Database.Where(&CachedRole{ GuildId: guildId, UserId: userId, }).Find(&rows) @@ -40,5 +42,6 @@ func GetCachedRoles(guildId, userId int64, res chan []int64) { for _, row := range rows { roles = append(roles, row.RoleId) } + res <- roles } diff --git a/utils/discord/objects/member.go b/utils/discord/objects/member.go index 8f303a7..99d8079 100644 --- a/utils/discord/objects/member.go +++ b/utils/discord/objects/member.go @@ -1,9 +1,11 @@ package objects +import "github.com/TicketsBot/GoPanel/utils/types" + type Member struct { User User Nick string - Roles []int64 `json:"roles,string"` + Roles types.Int64StringSlice `json:"roles,string"` JoinedAt string Deaf bool Mute bool diff --git a/utils/permissionutils.go b/utils/permissionutils.go index 540367e..c9cf55c 100644 --- a/utils/permissionutils.go +++ b/utils/permissionutils.go @@ -5,6 +5,7 @@ import ( "github.com/TicketsBot/GoPanel/database/table" "github.com/TicketsBot/GoPanel/utils/discord/endpoints/guild" "github.com/TicketsBot/GoPanel/utils/discord/objects" + "github.com/apex/log" "github.com/gin-gonic/contrib/sessions" "github.com/robfig/go-cache" "strconv" @@ -60,8 +61,10 @@ func GetRolesRest(store sessions.Session, guildId, userId int64) *[]int64 { endpoint := guild.GetGuildMember(int(guildId), int(userId)) if err, _ := endpoint.Request(store, nil, nil, &member); err != nil { + log.Error(err.Error()) return nil } - return &member.Roles + roles := []int64(member.Roles) + return &roles } diff --git a/utils/types/int64stringslice.go b/utils/types/int64stringslice.go new file mode 100644 index 0000000..3f88708 --- /dev/null +++ b/utils/types/int64stringslice.go @@ -0,0 +1,44 @@ +package types + +import ( + "encoding/json" + "fmt" + "strconv" + "strings" +) + +type Int64StringSlice []int64 + +func (slice Int64StringSlice) MarshalJSON() ([]byte, error) { + values := make([]string, len(slice)) + for i, value := range []int64(slice) { + values[i] = fmt.Sprintf(`"%v"`, value) + } + + return []byte(fmt.Sprintf("[%v]", strings.Join(values, ","))), nil +} + +func (slice *Int64StringSlice) UnmarshalJSON(b []byte) error { + // Try array of strings first. + var values []string + err := json.Unmarshal(b, &values) + if err != nil { + // Fall back to array of integers: + var values []int64 + if err := json.Unmarshal(b, &values); err != nil { + return err + } + *slice = values + return nil + } + *slice = make([]int64, len(values)) + for i, value := range values { + value, err := strconv.ParseInt(value, 10, 64) + if err != nil { + return err + } + (*slice)[i] = value + } + return nil +} +