From 3afa5e1dd12850d399d3d642e216bebc6efe6157 Mon Sep 17 00:00:00 2001 From: Zane Helton Date: Sun, 8 Sep 2024 15:51:22 -0400 Subject: [PATCH] chore: switch plain text responses to embeds --- uncord-bot-go/handlers/colors.go | 8 +++ uncord-bot-go/handlers/music_handler.go | 67 +++++++++++++++--------- uncord-bot-go/handlers/slash_commands.go | 10 ++-- 3 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 uncord-bot-go/handlers/colors.go diff --git a/uncord-bot-go/handlers/colors.go b/uncord-bot-go/handlers/colors.go new file mode 100644 index 0000000..29dc547 --- /dev/null +++ b/uncord-bot-go/handlers/colors.go @@ -0,0 +1,8 @@ +package handlers + +const ( + ColorSuccess = 0x57F287 // Green + ColorInfo = 0x3498DB // Blue + ColorWarning = 0xFEE75C // Yellow + ColorError = 0xED4245 // Red +) diff --git a/uncord-bot-go/handlers/music_handler.go b/uncord-bot-go/handlers/music_handler.go index 5588f85..e900407 100644 --- a/uncord-bot-go/handlers/music_handler.go +++ b/uncord-bot-go/handlers/music_handler.go @@ -100,20 +100,26 @@ func (h *Handler) play(guildID, commandChannelID, voiceChannelID snowflake.ID, u // Send appropriate message based on queue position queuePosition := len(queue.Tracks) - 1 - var content string + var embed *discord.EmbedBuilder if queuePosition == 0 { - content = fmt.Sprintf("Now playing: %s by %s", addedTrack.Info.Title, addedTrack.Info.Author) + embed = discord.NewEmbedBuilder(). + SetTitle("Now Playing"). + SetDescription(fmt.Sprintf("**%s**\nby %s", addedTrack.Info.Title, addedTrack.Info.Author)). + SetColor(ColorSuccess). + SetThumbnail(*addedTrack.Info.ArtworkURL) } else { - content = fmt.Sprintf("Added to queue: %s by %s (%d %s in front)", - addedTrack.Info.Title, - addedTrack.Info.Author, - queuePosition, - pluralize("song", queuePosition)) + embed = discord.NewEmbedBuilder(). + SetTitle("Added to Queue"). + SetDescription(fmt.Sprintf("**%s**\nby %s\n\nPosition in queue: %d", + addedTrack.Info.Title, + addedTrack.Info.Author, + queuePosition+1)). + SetColor(ColorInfo). + SetThumbnail(*addedTrack.Info.ArtworkURL) } _, err = h.Client.Rest().CreateMessage(commandChannelID, discord.NewMessageCreateBuilder(). - SetContent(content). - SetEphemeral(true). + SetEmbeds(embed.Build()). Build()) if err != nil { slog.Error("Failed to send queue message", slog.Any("err", err)) @@ -162,6 +168,7 @@ func (h *Handler) createControlPanel(channelID, guildID snowflake.ID) { SetEmbeds(discord.NewEmbedBuilder(). SetTitle(currentTrack.Info.Title). SetDescription(currentTrack.Info.Author). + SetColor(ColorInfo). SetImage(*currentTrack.Info.ArtworkURL). Build(), ). @@ -258,55 +265,67 @@ func (h *Handler) playNextTrack(guildID snowflake.ID) { } func (h *Handler) handleSkipButton(event *events.ComponentInteractionCreate) { - message, err := h.skipTracks(*event.GuildID(), 1) + embed, err := h.skipTracks(*event.GuildID(), 1) if err != nil { event.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent(fmt.Sprintf("Error: %s", err)). + SetEmbeds(discord.NewEmbedBuilder(). + SetDescription(fmt.Sprintf("Error: %s", err)). + SetColor(ColorError). + Build()). SetEphemeral(true). Build()) return } event.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent(message). - SetEphemeral(true). + SetEmbeds(embed.Build()). Build()) } -func (h *Handler) skipTracks(guildID snowflake.ID, amount int) (string, error) { +func (h *Handler) skipTracks(guildID snowflake.ID, amount int) (*discord.EmbedBuilder, error) { player := h.Lavalink.ExistingPlayer(guildID) queue := h.Queues.Get(guildID) if player == nil { - return "", fmt.Errorf("no player found") + return nil, fmt.Errorf("no player found") } if len(queue.Tracks) == 0 { // If queue is empty, stop the current track if err := player.Update(context.TODO(), lavalink.WithNullTrack()); err != nil { - return "", fmt.Errorf("error while stopping track: %w", err) + return nil, fmt.Errorf("error while stopping track: %w", err) } - return "No more tracks in the queue. Stopped playing.", nil + return discord.NewEmbedBuilder(). + SetDescription("No more tracks in the queue. Stopped playing."). + SetColor(ColorInfo), nil } skippedTracks := min(amount, len(queue.Tracks)) queue.Tracks = queue.Tracks[skippedTracks:] if len(queue.Tracks) == 0 { + // If we've skipped all tracks, stop the player if err := player.Update(context.TODO(), lavalink.WithNullTrack()); err != nil { - return "", fmt.Errorf("error while stopping track: %w", err) + return nil, fmt.Errorf("error while stopping track: %w", err) } - return "No more tracks in the queue. Stopped playing.", nil + return discord.NewEmbedBuilder(). + SetDescription("Skipped all tracks. No more tracks in the queue. Stopped playing."). + SetColor(ColorInfo), nil } nextTrack := queue.Tracks[0] if err := player.Update(context.TODO(), lavalink.WithTrack(nextTrack)); err != nil { - return "", fmt.Errorf("error while skipping to next track: %w", err) + return nil, fmt.Errorf("error while skipping to next track: %w", err) } - return fmt.Sprintf("Skipped %d %s. Now playing: %s", - skippedTracks, - pluralize("track", skippedTracks), - nextTrack.Info.Title), nil + return discord.NewEmbedBuilder(). + SetTitle("Skipped Track(s)"). + SetDescription(fmt.Sprintf("Skipped %d %s.\n\nNow playing: **%s**\nby %s", + skippedTracks, + pluralize("track", skippedTracks), + nextTrack.Info.Title, + nextTrack.Info.Author)). + SetColor(ColorSuccess). + SetThumbnail(*nextTrack.Info.ArtworkURL), nil } func pluralize(word string, count int) string { diff --git a/uncord-bot-go/handlers/slash_commands.go b/uncord-bot-go/handlers/slash_commands.go index 04a3c6e..6a73a34 100644 --- a/uncord-bot-go/handlers/slash_commands.go +++ b/uncord-bot-go/handlers/slash_commands.go @@ -111,17 +111,19 @@ func (h *Handler) handleSkip(event *events.ApplicationCommandInteractionCreate) amount = data } - message, err := h.skipTracks(*event.GuildID(), amount) + embed, err := h.skipTracks(*event.GuildID(), amount) if err != nil { event.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent(fmt.Sprintf("Error: %s", err)). + SetEmbeds(discord.NewEmbedBuilder(). + SetDescription(fmt.Sprintf("Error: %s", err)). + SetColor(ColorError). + Build()). SetEphemeral(true). Build()) return } event.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent(message). - SetEphemeral(true). + SetEmbeds(embed.Build()). Build()) }