Skip to content

Commit

Permalink
feat(quote): show channel name of quoted message (#22)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
myrkvi committed Aug 27, 2024
2 parents c985d56 + 64f009a commit b5c9f57
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion commands/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"errors"
"fmt"
"log/slog"
"math"
"regexp"
"slices"
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b5c9f57

Please sign in to comment.