Skip to content

Commit

Permalink
feat: add pause/resume/leave slash commands
Browse files Browse the repository at this point in the history
chore: improve embed messages
  • Loading branch information
ZaneH committed Sep 8, 2024
1 parent 3afa5e1 commit c83d988
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 32 deletions.
3 changes: 1 addition & 2 deletions uncord-bot-go/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgolink/v3/disgolink"
"github.com/disgoorg/snowflake/v2"
)

func main() {
Expand Down Expand Up @@ -60,7 +59,7 @@ func main() {
return
}

b.RegisterGuildCommands(client, snowflake.ID(1112943203755233350))
// b.RegisterGuildCommands(client, snowflake.ID(1112943203755233350))

node, err := b.Lavalink.AddNode(ctx, disgolink.NodeConfig{
Name: "local",
Expand Down
34 changes: 6 additions & 28 deletions uncord-bot-go/handlers/music_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ func (h *Handler) play(guildID, commandChannelID, voiceChannelID snowflake.ID, u
if queuePosition == 0 {
embed = discord.NewEmbedBuilder().
SetTitle("Now Playing").
SetDescription(fmt.Sprintf("**%s**\nby %s", addedTrack.Info.Title, addedTrack.Info.Author)).
SetDescription(addedTrack.Info.Title).
SetColor(ColorSuccess).
SetThumbnail(*addedTrack.Info.ArtworkURL)
} else {
embed = discord.NewEmbedBuilder().
SetTitle("Added to Queue").
SetDescription(fmt.Sprintf("**%s**\nby %s\n\nPosition in queue: %d",
SetDescription(fmt.Sprintf("**%s**\n\nPosition in queue: %d",
addedTrack.Info.Title,
addedTrack.Info.Author,
queuePosition+1)).
SetColor(ColorInfo).
SetThumbnail(*addedTrack.Info.ArtworkURL)
Expand All @@ -128,26 +127,6 @@ func (h *Handler) play(guildID, commandChannelID, voiceChannelID snowflake.ID, u
return nil
}

func (h *Handler) sendNowPlayingMessage(channelID snowflake.ID, track lavalink.Track) {
_, err := h.Client.Rest().CreateMessage(channelID, discord.NewMessageCreateBuilder().
SetContent(fmt.Sprintf("Now playing: %s by %s", track.Info.Title, track.Info.Author)).
SetEphemeral(true).
Build())
if err != nil {
slog.Error("Failed to send now playing message", slog.Any("err", err))
}
}

func (h *Handler) sendAddedToQueueMessage(channelID snowflake.ID, track lavalink.Track, position int) {
_, err := h.Client.Rest().CreateMessage(channelID, discord.NewMessageCreateBuilder().
SetContent(fmt.Sprintf("Added to queue: %s by %s (Position: %d)", track.Info.Title, track.Info.Author, position+1)).
SetEphemeral(true).
Build())
if err != nil {
slog.Error("Failed to send added to queue message", slog.Any("err", err))
}
}

func (h *Handler) createControlPanel(channelID, guildID snowflake.ID) {
queue := h.Queues.Get(guildID)
if len(queue.Tracks) == 0 {
Expand All @@ -166,8 +145,8 @@ func (h *Handler) createControlPanel(channelID, guildID snowflake.ID) {
_, err := h.Client.Rest().CreateMessage(channelID, discord.NewMessageCreateBuilder().
SetContent("").
SetEmbeds(discord.NewEmbedBuilder().
SetTitle(currentTrack.Info.Title).
SetDescription(currentTrack.Info.Author).
SetTitle("Media Controls").
SetDescription(currentTrack.Info.Title).
SetColor(ColorInfo).
SetImage(*currentTrack.Info.ArtworkURL).
Build(),
Expand Down Expand Up @@ -319,11 +298,10 @@ func (h *Handler) skipTracks(guildID snowflake.ID, amount int) (*discord.EmbedBu

return discord.NewEmbedBuilder().
SetTitle("Skipped Track(s)").
SetDescription(fmt.Sprintf("Skipped %d %s.\n\nNow playing: **%s**\nby %s",
SetDescription(fmt.Sprintf("Skipped %d %s.\n\nNow playing: **%s**",
skippedTracks,
pluralize("track", skippedTracks),
nextTrack.Info.Title,
nextTrack.Info.Author)).
nextTrack.Info.Title)).
SetColor(ColorSuccess).
SetThumbnail(*nextTrack.Info.ArtworkURL), nil
}
Expand Down
146 changes: 144 additions & 2 deletions uncord-bot-go/handlers/slash_commands.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package handlers

import (
"context"
"fmt"
"log/slog"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgolink/v3/lavalink"
"github.com/disgoorg/snowflake/v2"
)

Expand All @@ -27,6 +29,18 @@ var commands = []discord.ApplicationCommandCreate{
Name: "skip",
Description: "Skip the currently playing song",
},
discord.SlashCommandCreate{
Name: "pause",
Description: "Pause the music player",
},
discord.SlashCommandCreate{
Name: "resume",
Description: "Resume the music player",
},
discord.SlashCommandCreate{
Name: "leave",
Description: "Leave the voice channel",
},
}

func (h *Handler) HandleSlashCommand(event *events.ApplicationCommandInteractionCreate) {
Expand All @@ -39,6 +53,12 @@ func (h *Handler) HandleSlashCommand(event *events.ApplicationCommandInteraction
h.handlePlayer(event)
case "skip":
h.handleSkip(event)
case "pause":
h.handlePause(event)
case "resume":
h.handleResume(event)
case "leave":
h.handleLeave(event)
}
}

Expand Down Expand Up @@ -71,7 +91,7 @@ func (h *Handler) handleNowPlaying(event *events.ApplicationCommandInteractionCr

currentTrack := queue.Tracks[0]
event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(fmt.Sprintf("Now playing: %s by %s", currentTrack.Info.Title, currentTrack.Info.Author)).
SetContent(fmt.Sprintf("Now playing: **%s**", currentTrack.Info.Title)).
SetEphemeral(true).
Build())
}
Expand Down Expand Up @@ -118,7 +138,6 @@ func (h *Handler) handleSkip(event *events.ApplicationCommandInteractionCreate)
SetDescription(fmt.Sprintf("Error: %s", err)).
SetColor(ColorError).
Build()).
SetEphemeral(true).
Build())
return
}
Expand All @@ -127,3 +146,126 @@ func (h *Handler) handleSkip(event *events.ApplicationCommandInteractionCreate)
SetEmbeds(embed.Build()).
Build())
}

func (h *Handler) handlePause(event *events.ApplicationCommandInteractionCreate) {
player := h.Lavalink.Player(*event.GuildID())
if player == nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription("No music player found for this guild.").
SetColor(ColorWarning).
Build()).
Build())
return
}

if err := player.Update(context.TODO(), lavalink.WithPaused(true)); err != nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription(fmt.Sprintf("Error: %s", err)).
SetColor(ColorError).
Build()).
Build())
return
}

event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription("Music paused.").
SetColor(ColorSuccess).
Build()).
Build())
}

func (h *Handler) handleResume(event *events.ApplicationCommandInteractionCreate) {
player := h.Lavalink.Player(*event.GuildID())
if player == nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription("No music player found for this guild.").
SetColor(ColorWarning).
Build()).
Build())
return
}

if err := player.Update(context.TODO(), lavalink.WithPaused(false)); err != nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription(fmt.Sprintf("Error: %s", err)).
SetColor(ColorError).
Build()).
Build())
return
}

event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription("Music resumed.").
SetColor(ColorSuccess).
Build()).
Build())
}

// func (b *Bot) disconnect(event *events.ApplicationCommandInteractionCreate, data discord.SlashCommandInteractionData) error {
// player := b.Lavalink.ExistingPlayer(*event.GuildID())
// if player == nil {
// return event.CreateMessage(discord.MessageCreate{
// Content: "No player found",
// })
// }

// if err := b.Client.UpdateVoiceState(context.TODO(), *event.GuildID(), nil, false, false); err != nil {
// return event.CreateMessage(discord.MessageCreate{
// Content: fmt.Sprintf("Error while disconnecting: `%s`", err),
// })
// }

// return event.CreateMessage(discord.MessageCreate{
// Content: "Player disconnected",
// })
// }

func (h *Handler) handleLeave(event *events.ApplicationCommandInteractionCreate) {
player := h.Lavalink.Player(*event.GuildID())
if player == nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription("No music player found for this guild.").
SetColor(ColorWarning).
Build()).
Build())
return
}

if err := player.Destroy(context.TODO()); err != nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription(fmt.Sprintf("Error: %s", err)).
SetColor(ColorError).
Build()).
Build())
return
}

// Clear the queue
h.Queues.Get(*event.GuildID()).Tracks = nil

// Disconnect bot from voice channel
if err := h.Client.UpdateVoiceState(context.TODO(), *event.GuildID(), nil, false, false); err != nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription(fmt.Sprintf("Error while disconnecting: `%s`", err)).
SetColor(ColorError).
Build()).
Build())
return
}

event.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetDescription("Left the voice channel and cleared the queue.").
SetColor(ColorSuccess).
Build()).
Build())
}

0 comments on commit c83d988

Please sign in to comment.