diff --git a/app/http/endpoints/manage/settings.go b/app/http/endpoints/manage/settings.go index 0c4a675..fef63c6 100644 --- a/app/http/endpoints/manage/settings.go +++ b/app/http/endpoints/manage/settings.go @@ -1,6 +1,8 @@ package manage import ( + "fmt" + "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" "github.com/TicketsBot/GoPanel/utils" @@ -33,6 +35,14 @@ func SettingsHandler(ctx *gin.Context) { return } + // Check the bot is in the guild + isInGuild := make(chan bool) + go cache.Client.GuildExists(guildIdStr, isInGuild) + if !<-isInGuild { + ctx.Redirect(302, fmt.Sprintf("https://invite.ticketsbot.net/?guild_id=%s&disable_guild_select=true&response_type=code&scope=bot%%20identify&redirect_uri=%s", guildIdStr, config.Conf.Server.BaseUrl)) + return + } + // Get object for selected guild var guild objects.Guild for _, g := range table.GetGuilds(userIdStr) { diff --git a/app/http/endpoints/root/index.go b/app/http/endpoints/root/index.go index cbdfb41..fd7c535 100644 --- a/app/http/endpoints/root/index.go +++ b/app/http/endpoints/root/index.go @@ -25,8 +25,9 @@ func IndexHandler(ctx *gin.Context) { return } + userGuilds := table.GetGuilds(userIdStr) adminGuilds := make([]objects.Guild, 0) - for _, guild := range table.GetGuilds(userIdStr) { + for _, guild := range userGuilds { guildId, err := strconv.ParseInt(guild.Id, 10, 64) if err != nil { // I think this happens when a server was deleted? We should just skip though. continue diff --git a/cache/guildobjectcache.go b/cache/guildobjectcache.go index 15b6680..680c127 100644 --- a/cache/guildobjectcache.go +++ b/cache/guildobjectcache.go @@ -9,13 +9,14 @@ import ( ) func (c *RedisClient) StoreGuild(guild objects.Guild) { - packed, err := msgpack.Marshal(guild); if err != nil { + 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) + c.Set(key, string(packed), time.Hour*48) } func (c *RedisClient) GetGuildByID(guildId string, res chan *objects.Guild) { @@ -23,14 +24,26 @@ func (c *RedisClient) GetGuildByID(guildId string, res chan *objects.Guild) { packed, err := c.Get(key).Result() if err != nil { - res<-nil + res <- nil } else { var unpacked objects.Guild if err = msgpack.Unmarshal([]byte(packed), &unpacked); err != nil { log.Error(err.Error()) - res<-nil + res <- nil } else { - res<-&unpacked + res <- &unpacked } } } + +func (c *RedisClient) GuildExists(guildId string, res chan bool) { + key := fmt.Sprintf("tickets:guilds:%s", guildId) + + intResult, err := c.Exists(key).Result() + if err != nil { + res <- false + return + } + + res <- intResult == 1 +} diff --git a/utils/stringutils.go b/utils/stringutils.go index 89e0e51..9226e80 100644 --- a/utils/stringutils.go +++ b/utils/stringutils.go @@ -7,7 +7,7 @@ import ( ) var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") -// + func RandStringRunes(length int) string { b := make([]rune, length) for i := range b {