diff --git a/app/http/endpoints/manage/sendmessage.go b/app/http/endpoints/manage/sendmessage.go index e49ba7f..f9ca2be 100644 --- a/app/http/endpoints/manage/sendmessage.go +++ b/app/http/endpoints/manage/sendmessage.go @@ -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) } } diff --git a/app/http/endpoints/manage/settings.go b/app/http/endpoints/manage/settings.go index 4abaf06..8694f8c 100644 --- a/app/http/endpoints/manage/settings.go +++ b/app/http/endpoints/manage/settings.go @@ -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 diff --git a/app/http/endpoints/manage/ticketview.go b/app/http/endpoints/manage/ticketview.go index 461433c..5e714f3 100644 --- a/app/http/endpoints/manage/ticketview.go +++ b/app/http/endpoints/manage/ticketview.go @@ -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() } diff --git a/app/http/endpoints/manage/webchatws.go b/app/http/endpoints/manage/webchatws.go index 2b00d30..d7c4e65 100644 --- a/app/http/endpoints/manage/webchatws.go +++ b/app/http/endpoints/manage/webchatws.go @@ -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) } } } diff --git a/app/http/endpoints/root/callback.go b/app/http/endpoints/root/callback.go index 47f5fca..1903377 100644 --- a/app/http/endpoints/root/callback.go +++ b/app/http/endpoints/root/callback.go @@ -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, ¤tUser) + err = user.CurrentUser.Request(store, nil, nil, ¤tUser, nil) if err != nil { ctx.String(500, err.Error()) return @@ -84,11 +85,15 @@ 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 { diff --git a/cache/guildobjectcache.go b/cache/guildobjectcache.go new file mode 100644 index 0000000..15b6680 --- /dev/null +++ b/cache/guildobjectcache.go @@ -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 + } + } +} diff --git a/utils/discord/endpoints.go b/utils/discord/endpoints.go index d345897..7df507c 100644 --- a/utils/discord/endpoints.go +++ b/utils/discord/endpoints.go @@ -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) } diff --git a/utils/discord/objects/guild.go b/utils/discord/objects/guild.go index adc9d9b..e8fd329 100644 --- a/utils/discord/objects/guild.go +++ b/utils/discord/objects/guild.go @@ -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 { diff --git a/utils/premiumutils.go b/utils/premiumutils.go index 277da44..115a277 100644 --- a/utils/premiumutils.go +++ b/utils/premiumutils.go @@ -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 {