fix panic

This commit is contained in:
Dot-Rar 2020-03-24 17:31:23 +00:00
parent 2d501fed3e
commit 61c0fdd8df
4 changed files with 58 additions and 6 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}