// Package internal provides shared functionality for the banner application. package internal import ( "banner/internal/config" "context" "time" "github.com/bwmarrin/discordgo" "github.com/redis/go-redis/v9" log "github.com/rs/zerolog/log" ) // GetGuildName returns the name of a guild by its ID, using Redis for caching. func GetGuildName(cfg *config.Config, session *discordgo.Session, guildID string) string { // Create a timeout context for Redis operations ctx, cancel := context.WithTimeout(cfg.Ctx, 5*time.Second) defer cancel() // Check Redis for the guild name guildName, err := cfg.KV.Get(ctx, "guild:"+guildID+":name").Result() if err != nil && err != redis.Nil { log.Error().Stack().Err(err).Msg("Error getting guild name from Redis") return "err" } // If the guild name is invalid (1 character long), then return "unknown" if len(guildName) == 1 { return "unknown" } // If the guild name isn't in Redis, get it from Discord and cache it guild, err := session.Guild(guildID) if err != nil { log.Error().Stack().Err(err).Msg("Error getting guild name") // Store an invalid value in Redis so we don't keep trying to get the guild name ctx2, cancel2 := context.WithTimeout(cfg.Ctx, 5*time.Second) defer cancel2() _, err := cfg.KV.Set(ctx2, "guild:"+guildID+":name", "x", time.Minute*5).Result() if err != nil { log.Error().Stack().Err(err).Msg("Error setting false guild name in Redis") } return "unknown" } // Cache the guild name in Redis ctx3, cancel3 := context.WithTimeout(cfg.Ctx, 5*time.Second) defer cancel3() cfg.KV.Set(ctx3, "guild:"+guildID+":name", guild.Name, time.Hour*3) return guild.Name } // GetChannelName returns the name of a channel by its ID, using Redis for caching. func GetChannelName(cfg *config.Config, session *discordgo.Session, channelID string) string { // Create a timeout context for Redis operations ctx, cancel := context.WithTimeout(cfg.Ctx, 5*time.Second) defer cancel() // Check Redis for the channel name channelName, err := cfg.KV.Get(ctx, "channel:"+channelID+":name").Result() if err != nil && err != redis.Nil { log.Error().Stack().Err(err).Msg("Error getting channel name from Redis") return "err" } // If the channel name is invalid (1 character long), then return "unknown" if len(channelName) == 1 { return "unknown" } // If the channel name isn't in Redis, get it from Discord and cache it channel, err := session.Channel(channelID) if err != nil { log.Error().Stack().Err(err).Msg("Error getting channel name") // Store an invalid value in Redis so we don't keep trying to get the channel name ctx2, cancel2 := context.WithTimeout(cfg.Ctx, 5*time.Second) defer cancel2() _, err := cfg.KV.Set(ctx2, "channel:"+channelID+":name", "x", time.Minute*5).Result() if err != nil { log.Error().Stack().Err(err).Msg("Error setting false channel name in Redis") } return "unknown" } // Cache the channel name in Redis ctx3, cancel3 := context.WithTimeout(cfg.Ctx, 5*time.Second) defer cancel3() cfg.KV.Set(ctx3, "channel:"+channelID+":name", channel.Name, time.Hour*3) return channel.Name }