premium update

This commit is contained in:
Dot-Rar 2020-01-17 21:28:20 +00:00
parent 85f2730fb0
commit 05b31e02ae
9 changed files with 89 additions and 29 deletions

View File

@ -68,7 +68,7 @@ func SendMessage(ctx *gin.Context) {
endpoint := channel.CreateMessage(int(ticket.Channel))
err = endpoint.Request(store, &contentType, channel.CreateMessageBody{
Content: content,
}, nil)
}, nil, nil)
}
}

View File

@ -105,7 +105,7 @@ func SettingsHandler(ctx *gin.Context) {
if len(guild.Channels) == 0 {
var channels []objects.Channel
endpoint := guildendpoint.GetGuildChannels(int(guildId))
err = endpoint.Request(store, nil, nil, &channels)
err = endpoint.Request(store, nil, nil, &channels, nil)
if err != nil {
// Not in guild

View File

@ -75,7 +75,7 @@ func TicketViewHandler(ctx *gin.Context) {
var errorMessage string
endpoint := channel.GetChannelMessages(int(ticket.Channel))
if err = endpoint.Request(store, nil, nil, &messages); err != nil {
if err = endpoint.Request(store, nil, nil, &messages, nil); err != nil {
isError = true
errorMessage = err.Error()
}

View File

@ -169,7 +169,7 @@ func WebChatWs(ctx *gin.Context) {
endpoint := channel.CreateMessage(int(ticket.Channel))
err = endpoint.Request(store, &contentType, channel.CreateMessageBody{
Content: content,
}, nil)
}, nil, nil)
}
}
}

View File

@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/TicketsBot/GoPanel/cache"
"github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/database/table"
"github.com/TicketsBot/GoPanel/utils"
@ -64,7 +65,7 @@ func CallbackHandler(ctx *gin.Context) {
// Get ID + name
var currentUser objects.User
err = user.CurrentUser.Request(store, nil, nil, &currentUser)
err = user.CurrentUser.Request(store, nil, nil, &currentUser, nil)
if err != nil {
ctx.String(500, err.Error())
return
@ -84,12 +85,16 @@ func CallbackHandler(ctx *gin.Context) {
// Cache guilds because Discord takes like 2 whole seconds to return then
go func() {
var guilds []objects.Guild
err = user.CurrentUserGuilds.Request(store, nil, nil, &guilds)
err = user.CurrentUserGuilds.Request(store, nil, nil, &guilds, nil)
if err != nil {
log.Error(err.Error())
return
}
for _, guild := range guilds {
go cache.Client.StoreGuild(guild)
}
marshalled, err := json.Marshal(guilds)
if err != nil {
log.Error(err.Error())

36
cache/guildobjectcache.go vendored Normal file
View File

@ -0,0 +1,36 @@
package cache
import (
"fmt"
"github.com/TicketsBot/GoPanel/utils/discord/objects"
"github.com/apex/log"
"github.com/vmihailenco/msgpack"
"time"
)
func (c *RedisClient) StoreGuild(guild objects.Guild) {
packed, err := msgpack.Marshal(guild); if err != nil {
log.Error(err.Error())
return
}
key := fmt.Sprintf("ticketspanel:guilds:%s", string(packed))
c.Set(key, string(packed), time.Hour * 48)
}
func (c *RedisClient) GetGuildByID(guildId string, res chan *objects.Guild) {
key := fmt.Sprintf("ticketspanel:guilds:%s", guildId)
packed, err := c.Get(key).Result()
if err != nil {
res<-nil
} else {
var unpacked objects.Guild
if err = msgpack.Unmarshal([]byte(packed), &unpacked); err != nil {
log.Error(err.Error())
res<-nil
} else {
res<-&unpacked
}
}
}

View File

@ -36,7 +36,7 @@ type Endpoint struct {
Endpoint string
}
func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, body interface{}, response interface{}) error {
func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, body interface{}, response interface{}, rawResponse *chan string) error {
url := BASE_URL + e.Endpoint
// Create req
@ -117,5 +117,9 @@ func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, bod
return err
}
if rawResponse != nil {
*rawResponse<-string(content)
}
return json.Unmarshal(content, response)
}

View File

@ -6,37 +6,40 @@ type Guild struct {
Icon string
Splash string
Owner bool
OwnerId string
OwnerId string `json:"owner_id"`
Permissions int
Region string
AfkChannelid string
AfkChannelid string `json:"afk_channel_id"`
AfkTimeout int
EmbedEnabled bool
EmbedChannelId string
VerificationLevel int
DefaultMessageNotifications int
ExplicitContentFilter int
EmbedEnabled bool `json:"embed_enabled"`
EmbedChannelId string `json:"embed_channel_id"`
VerificationLevel int `json:"verification_level"`
DefaultMessageNotifications int `json:"default_message_notifications"`
ExplicitContentFilter int `json:"explicit_content_filter"`
Roles []Role
Emojis []Emoji
Features []string
MfaLevel int
ApplicationId string
WidgetEnabled bool
WidgetChannelId string
SystemChannelId string
JoinedAt string
MfaLevel int `json:"mfa_level"`
ApplicationId string `json:"application_id"`
WidgetEnabled bool `json:"widget_enabled"`
WidgetChannelId string `json:"widget_channel_id"`
SystemChannelId string `json:"system_channel_id"`
JoinedAt string `json:"joined_at"`
Large bool
Unavailable bool
MemberCount int
MemberCount int `json:"member_count"`
VoiceStates []VoiceState
Members []Member
Channels []Channel
Presences []Presence
MaxPresences int
Maxmembers int
VanityUrlCode string
MaxPresences int `json:"max_presences"`
MaxMembers int `json:"max_members"`
VanityUrlCode string `json:"vanity_url_code"`
Description string
Banner string
PremiumTier int `json:"premium_tier"`
PremiumSubscriptionCount int `json:"premium_subscription_count"`
PreferredLocale string `json:"preferred_locale"`
}
func (g *Guild) GetCategories() []Channel {

View File

@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"fmt"
redis "github.com/TicketsBot/GoPanel/cache"
"github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/database/table"
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/guild"
@ -45,12 +46,23 @@ func IsPremiumGuild(store sessions.Session, guildIdRaw string, ch chan bool) {
ch<-true
} else {
// Get guild object
var g objects.Guild
endpoint := guild.GetGuild(int(guildId))
go endpoint.Request(store, nil, nil, &g)
guildChan := make(chan *objects.Guild)
go redis.Client.GetGuildByID(guildIdRaw, guildChan)
g := <-guildChan
ownerIdRaw := ""
if g == nil {
var g objects.Guild
endpoint := guild.GetGuild(int(guildId))
endpoint.Request(store, nil, nil, &g, nil)
ownerIdRaw = g.OwnerId
go redis.Client.StoreGuild(g)
}
// Lookup votes
ownerId, err := strconv.ParseInt(g.OwnerId, 10, 64); if err != nil {
ownerId, err := strconv.ParseInt(ownerIdRaw, 10, 64); if err != nil {
fmt.Println(err.Error())
ch <- false
return
@ -73,7 +85,7 @@ func IsPremiumGuild(store sessions.Session, guildIdRaw string, ch chan bool) {
Timeout: time.Second * 3,
}
url := fmt.Sprintf("%s/ispremium?key=%s&id=%s", config.Conf.Bot.PremiumLookupProxyUrl, config.Conf.Bot.PremiumLookupProxyKey, g.OwnerId)
url := fmt.Sprintf("%s/ispremium?key=%s&id=%s", config.Conf.Bot.PremiumLookupProxyUrl, config.Conf.Bot.PremiumLookupProxyKey, ownerIdRaw)
req, err := http.NewRequest("GET", url, nil)
res, err := client.Do(req); if err != nil {