feat: integrate music commands with Lavalink support
- Added new music commands: play, queue, and volume. - Implemented music management using disgolink for Lavalink integration. - Updated bot initialization to include Lavalink host and password. - Enhanced interaction handling for music commands, requiring DJ role for usage. - Introduced now playing message with interactive buttons for controlling playback. - Updated dependencies in go.mod for disgolink and snowflake.
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package music
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
// DJ role is required for all music controls.
|
||||
if !memberHasDJRoleForComponents(s, i.GuildID, i.Member) {
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "You need the **DJ** role to use music commands.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
customID := i.MessageComponentData().CustomID
|
||||
parts := strings.Split(customID, ":")
|
||||
if len(parts) < 2 {
|
||||
return
|
||||
}
|
||||
|
||||
action := parts[1]
|
||||
guildID := i.GuildID
|
||||
|
||||
// Ignore controls from old now-playing messages
|
||||
currentMsgID := CurrentNowPlayingMessageID(guildID)
|
||||
if currentMsgID != "" && i.Message != nil && i.Message.ID != currentMsgID {
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "These controls are outdated.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Acknowledge immediately to avoid "This interaction failed".
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseDeferredMessageUpdate,
|
||||
})
|
||||
|
||||
var err error
|
||||
content := ""
|
||||
|
||||
switch action {
|
||||
case "pause":
|
||||
err = Pause(guildID, true)
|
||||
content = "Paused."
|
||||
case "resume":
|
||||
err = Pause(guildID, false)
|
||||
content = "Resumed."
|
||||
case "toggle_pause":
|
||||
paused, _ := pausedAndVolume(guildID)
|
||||
err = Pause(guildID, !paused)
|
||||
if paused {
|
||||
content = "Resumed."
|
||||
} else {
|
||||
content = "Paused."
|
||||
}
|
||||
updateNowPlayingButtons(i.ChannelID, i.Message.ID, guildID)
|
||||
case "skip":
|
||||
err = Skip(guildID)
|
||||
content = "Skipped."
|
||||
case "stop":
|
||||
err = Stop(guildID)
|
||||
content = "Stopped."
|
||||
case "repeat_song":
|
||||
rs, rq := ToggleRepeatSong(guildID)
|
||||
_ = rs
|
||||
_ = rq
|
||||
updateNowPlayingButtons(i.ChannelID, i.Message.ID, guildID)
|
||||
if rs {
|
||||
content = "Repeat song enabled."
|
||||
} else {
|
||||
content = "Repeat song disabled."
|
||||
}
|
||||
case "repeat_queue":
|
||||
rs, rq := ToggleRepeatQueue(guildID)
|
||||
_ = rs
|
||||
_ = rq
|
||||
updateNowPlayingButtons(i.ChannelID, i.Message.ID, guildID)
|
||||
if rq {
|
||||
content = "Repeat queue enabled."
|
||||
} else {
|
||||
content = "Repeat queue disabled."
|
||||
}
|
||||
}
|
||||
|
||||
if content == "" {
|
||||
content = "Done."
|
||||
}
|
||||
if err != nil {
|
||||
content = "Error: " + err.Error()
|
||||
}
|
||||
|
||||
// send ephemeral confirmation
|
||||
_, _ = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
||||
Content: content,
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user