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)) endpoint := channel.CreateMessage(int(ticket.Channel))
err = endpoint.Request(store, &contentType, channel.CreateMessageBody{ err = endpoint.Request(store, &contentType, channel.CreateMessageBody{
Content: content, Content: content,
}, nil) }, nil, nil)
} }
} }

View File

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

View File

@ -75,7 +75,7 @@ func TicketViewHandler(ctx *gin.Context) {
var errorMessage string var errorMessage string
endpoint := channel.GetChannelMessages(int(ticket.Channel)) 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 isError = true
errorMessage = err.Error() errorMessage = err.Error()
} }

View File

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

View File

@ -4,6 +4,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/TicketsBot/GoPanel/cache"
"github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/database/table" "github.com/TicketsBot/GoPanel/database/table"
"github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils"
@ -64,7 +65,7 @@ func CallbackHandler(ctx *gin.Context) {
// Get ID + name // Get ID + name
var currentUser objects.User var currentUser objects.User
err = user.CurrentUser.Request(store, nil, nil, &currentUser) err = user.CurrentUser.Request(store, nil, nil, &currentUser, nil)
if err != nil { if err != nil {
ctx.String(500, err.Error()) ctx.String(500, err.Error())
return return
@ -84,12 +85,16 @@ func CallbackHandler(ctx *gin.Context) {
// Cache guilds because Discord takes like 2 whole seconds to return then // Cache guilds because Discord takes like 2 whole seconds to return then
go func() { go func() {
var guilds []objects.Guild var guilds []objects.Guild
err = user.CurrentUserGuilds.Request(store, nil, nil, &guilds) err = user.CurrentUserGuilds.Request(store, nil, nil, &guilds, nil)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
return return
} }
for _, guild := range guilds {
go cache.Client.StoreGuild(guild)
}
marshalled, err := json.Marshal(guilds) marshalled, err := json.Marshal(guilds)
if err != nil { if err != nil {
log.Error(err.Error()) 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 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 url := BASE_URL + e.Endpoint
// Create req // Create req
@ -117,5 +117,9 @@ func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, bod
return err return err
} }
if rawResponse != nil {
*rawResponse<-string(content)
}
return json.Unmarshal(content, response) return json.Unmarshal(content, response)
} }

View File

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

View File

@ -3,6 +3,7 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
redis "github.com/TicketsBot/GoPanel/cache"
"github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/database/table" "github.com/TicketsBot/GoPanel/database/table"
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/guild" "github.com/TicketsBot/GoPanel/utils/discord/endpoints/guild"
@ -45,12 +46,23 @@ func IsPremiumGuild(store sessions.Session, guildIdRaw string, ch chan bool) {
ch<-true ch<-true
} else { } else {
// Get guild object // Get guild object
var g objects.Guild guildChan := make(chan *objects.Guild)
endpoint := guild.GetGuild(int(guildId)) go redis.Client.GetGuildByID(guildIdRaw, guildChan)
go endpoint.Request(store, nil, nil, &g) 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 // 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()) fmt.Println(err.Error())
ch <- false ch <- false
return return
@ -73,7 +85,7 @@ func IsPremiumGuild(store sessions.Session, guildIdRaw string, ch chan bool) {
Timeout: time.Second * 3, 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) req, err := http.NewRequest("GET", url, nil)
res, err := client.Do(req); if err != nil { res, err := client.Do(req); if err != nil {