mirror of
https://github.com/Xevion/banner.git
synced 2025-12-09 06:06:31 -06:00
Improve command invocation logging details for guilds & direct messages
This commit is contained in:
21
main.go
21
main.go
@@ -158,6 +158,27 @@ func main() {
|
||||
options.Str(option.Name, fmt.Sprintf("%v", option.Value))
|
||||
}
|
||||
|
||||
event := log.Info().Str("name", name).Str("user", interaction.Member.User.Username).Dict("options", options)
|
||||
|
||||
// If the command was invoked in a guild, add guild & channel info to the log
|
||||
if interaction.Member != nil {
|
||||
guild := zerolog.Dict()
|
||||
guild.Str("id", interaction.GuildID)
|
||||
guild.Str("name", GetGuildName(interaction.GuildID))
|
||||
event.Dict("guild", guild)
|
||||
|
||||
channel := zerolog.Dict()
|
||||
channel.Str("id", interaction.ChannelID)
|
||||
guild.Str("name", GetChannelName(interaction.ChannelID))
|
||||
event.Dict("channel", channel)
|
||||
} else {
|
||||
// If the command was invoked in a DM, add the user info to the log
|
||||
user := zerolog.Dict()
|
||||
user.Str("id", interaction.User.ID)
|
||||
user.Str("name", interaction.User.Username)
|
||||
event.Dict("user", user)
|
||||
}
|
||||
|
||||
// Log command invocation
|
||||
log.Info().Str("name", name).Str("user", GetUsername(interaction)).Dict("options", options).Msg("Command Invoked")
|
||||
|
||||
|
||||
71
meta.go
Normal file
71
meta.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import log "github.com/rs/zerolog/log"
|
||||
|
||||
// GetGuildName returns the name of the guild with the given ID, utilizing Redis to cache the value
|
||||
func GetGuildName(guildID string) string {
|
||||
// Check Redis for the guild name
|
||||
guildName, err := kv.Get(ctx, "guild:"+guildID+":name").Result()
|
||||
if err != nil {
|
||||
log.Error().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().Err(err).Msg("Error getting guild name")
|
||||
|
||||
// Store an invalid value in Redis so we don't keep trying to get the guild name
|
||||
_, err := kv.Set(ctx, "guild:"+guildID+":name", "x", 60*5).Result()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error setting false guild name in Redis")
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Cache the guild name in Redis
|
||||
kv.Set(ctx, "guild:"+guildID+":name", guild.Name, 60*60*3)
|
||||
|
||||
return guild.Name
|
||||
}
|
||||
|
||||
// GetChannelName returns the name of the channel with the given ID, utilizing Redis to cache the value
|
||||
func GetChannelName(channelID string) string {
|
||||
// Check Redis for the channel name
|
||||
channelName, err := kv.Get(ctx, "channel:"+channelID+":name").Result()
|
||||
if err != nil {
|
||||
log.Error().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().Err(err).Msg("Error getting channel name")
|
||||
|
||||
// Store an invalid value in Redis so we don't keep trying to get the channel name
|
||||
_, err := kv.Set(ctx, "channel:"+channelID+":name", "x", 60*5).Result()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error setting false channel name in Redis")
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Cache the channel name in Redis
|
||||
kv.Set(ctx, "channel:"+channelID+":name", channel.Name, 60*60*3)
|
||||
|
||||
return channel.Name
|
||||
}
|
||||
Reference in New Issue
Block a user