summaryrefslogtreecommitdiff
path: root/commands/play_autocomplete.go
blob: 85613b35448d86f566fe0200c544d23b5cfd0e53 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package commands

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

	"github.com/bwmarrin/discordgo"
)

func PlayAutocomplete(s *discordgo.Session, i *discordgo.InteractionCreate) {
	var focusedOption *discordgo.ApplicationCommandInteractionDataOption

	for _, option := range i.ApplicationCommandData().Options {
		if option.Focused {
			focusedOption = option
			break
		}
	}

	if focusedOption == nil {
		return
	}

	query := focusedOption.StringValue()

	if len(query) < 3 {
		s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
			Type: discordgo.InteractionApplicationCommandAutocompleteResult,
			Data: &discordgo.InteractionResponseData{
				Choices: []*discordgo.ApplicationCommandOptionChoice{
					{
						Name:  "Please enter at least 3 characters",
						Value: "min_chars",
					},
				},
			},
		})
		return
	}

	results, err := music.Search(query, 10)
	if err != nil {
		logger.Log(fmt.Sprintf("Search error: %v", err), types.LogOptions{
			Prefix: "Play Autocomplete",
			Level:  types.Error,
		})

		s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
			Type: discordgo.InteractionApplicationCommandAutocompleteResult,
			Data: &discordgo.InteractionResponseData{
				Choices: []*discordgo.ApplicationCommandOptionChoice{
					{
						Name:  "Error searching. Try again later.",
						Value: "search_error",
					},
				},
			},
		})
		return
	}

	choices := make([]*discordgo.ApplicationCommandOptionChoice, 0, 25)

	for i, result := range results {
		if i >= 25 {
			break
		}

		var displayName string
		if result.SourceType == types.YouTube {
			displayName = fmt.Sprintf("▶️ %s - %s", result.Title, result.Artist)
		} else {
			displayName = fmt.Sprintf("🎵 %s - %s", result.Title, result.Artist)
		}

		if len(displayName) > 100 {
			displayName = displayName[:97] + "..."
		}

		valueStr := fmt.Sprintf("%s|%s|%s", result.SourceType, result.ID, result.URL)
		if len(valueStr) > 100 {
			valueStr = fmt.Sprintf("%s|%s", result.SourceType, result.ID)
		}

		choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
			Name:  displayName,
			Value: valueStr,
		})
	}

	if len(choices) == 0 {
		choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
			Name:  "No results found",
			Value: "no_results",
		})
	}

	err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
		Type: discordgo.InteractionApplicationCommandAutocompleteResult,
		Data: &discordgo.InteractionResponseData{
			Choices: choices,
		},
	})

	if err != nil {
		logger.Log(fmt.Sprintf("Failed to send autocomplete response: %v", err), types.LogOptions{
			Prefix: "Play Autocomplete",
			Level:  types.Error,
		})
	}
}