summaryrefslogtreecommitdiff
path: root/commands/disconnect.go
blob: 94249c88ec6e5ab047869e4a17b0be7157829050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package commands

import (
	"ai/utils/music"
	"fmt"

	"github.com/bwmarrin/discordgo"
)

func Disconnect(s *discordgo.Session, i *discordgo.InteractionCreate) {
	guildID := i.GuildID
	userID := i.Member.User.ID

	isSameVC, userChannelID := music.IsUserInSameVC(s, guildID, userID)

	if userChannelID == "" {
		respondWithError(s, i, "You must be in a voice channel to use this command.")
		return
	}

	voice, exists := music.VoiceConnection[guildID]
	if !exists {
		respondWithError(s, i, "I'm not in a voice channel.")
		return
	}

	if !isSameVC {
		channel, err := s.Channel(voice.ChannelID)
		if err == nil {
			respondWithError(s, i, fmt.Sprintf("You must be in the same voice channel as me (**%s**) to use this command.", channel.Name))
		} else {
			respondWithError(s, i, "You must be in the same voice channel as me to use this command.")
		}
		return
	}

	channel, err := s.Channel(voice.ChannelID)
	channelName := "voice channel"
	if err == nil {
		channelName = channel.Name
	}

	err = music.LeaveVoiceChannel(guildID)
	if err != nil {
		respondWithError(s, i, fmt.Sprintf("Error disconnecting from voice channel: %v", err))
		return
	}

	s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
		Type: discordgo.InteractionResponseChannelMessageWithSource,
		Data: &discordgo.InteractionResponseData{
			Content: fmt.Sprintf("✅ Disconnected from **%s**.", channelName),
		},
	})
}