From 64f009a3bf6fe800cb970a978b9f68db893289f8 Mon Sep 17 00:00:00 2001 From: Vegard Berg Date: Tue, 27 Aug 2024 22:44:19 +0200 Subject: [PATCH] feat(quote): show channel name of quoted message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will display the channel name of the quoted message's channel, if Heimdallr is able to retrieve it. Otherwise, it will display "unknown channel". It will also add a prefix (#,๐Ÿ”Š, etc.) depending on the type of the channel. --- commands/quote.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/commands/quote.go b/commands/quote.go index f6cf460..e7ad080 100644 --- a/commands/quote.go +++ b/commands/quote.go @@ -3,6 +3,7 @@ package commands import ( "errors" "fmt" + "log/slog" "math" "regexp" "slices" @@ -90,6 +91,17 @@ func QuoteHandler(e *handler.CommandEvent) error { return err } + channelName := "unknown channel" + channel, err := e.Client().Rest().GetChannel(parts.ChannelId) + if err == nil { + prefix := getChannelTypePrefix(channel) + channelName = prefix + channel.Name() + } else { + slog.Warn("Failed to fetch channel", + "guild_id", parts.GuildId, + "channel_id", parts.ChannelId) + } + if canRead, _ := userCanReadChannelMessages(e.User().ID, message.ChannelID, e.Client()); !canRead { _ = respondWithContentEph(e, "You don't have permission to read messages in that channel.") return nil @@ -98,7 +110,8 @@ func QuoteHandler(e *handler.CommandEvent) error { embed := discord.NewEmbedBuilder(). SetDescription(message.Content). SetAuthor(message.Author.EffectiveName(), "", message.Author.EffectiveAvatarURL()). - SetTimestamp(message.CreatedAt) + SetTimestamp(message.CreatedAt). + SetFooter(channelName, "") if len(message.Attachments) == 1 { att := message.Attachments[0] @@ -134,6 +147,24 @@ func QuoteHandler(e *handler.CommandEvent) error { return e.CreateMessage(resp.Build()) } +func getChannelTypePrefix(channel discord.Channel) string { + switch channel.Type() { + case discord.ChannelTypeGuildVoice: + return "๐Ÿ”Š" + case discord.ChannelTypeGuildStageVoice: + return "๐Ÿ—ฃ๏ธ" + case discord.ChannelTypeGuildNews: + return "๐Ÿ“ข" + case discord.ChannelTypeGuildForum: + return "๐Ÿ—ช" + case discord.ChannelTypeGuildPublicThread, + discord.ChannelTypeGuildPrivateThread: + return "๐Ÿงต" + default: + return "#" + } +} + func userCanReadChannelMessages(userID, channelID snowflake.ID, client bot.Client) (bool, error) { channel, err := client.Rest().GetChannel(channelID) if err != nil {