src/globals/utils.go (view raw)
1package globals
2
3import (
4 "errors"
5 "fmt"
6 "io"
7 "net/http"
8 "net/url"
9 "regexp"
10 "strings"
11
12 "github.com/bwmarrin/discordgo"
13 "github.com/kkdai/youtube/v2"
14)
15
16var searchPattern = regexp.MustCompile(`watch\?v\x3d([a-zA-Z0-9_-]{11})`)
17
18func GetVoiceChannelID(s *discordgo.Session, m *discordgo.MessageCreate) (response string, g *discordgo.Guild, voiceChannelID string) {
19 if m.Member == nil {
20 response = "Please, use this inside a server."
21 return
22 }
23
24 g, err := s.State.Guild(m.GuildID)
25 if err != nil {
26 logger.Errorf("could not get guild: %s", err)
27 response = MsgError
28 return
29 }
30
31 for _, vs := range g.VoiceStates {
32 if vs.UserID == m.Author.ID {
33 voiceChannelID = vs.ChannelID
34 break
35 }
36 }
37
38 if voiceChannelID == "" {
39 response = "You need to be in a voice channel to use this command."
40 }
41 return
42}
43
44func (bc BotCommand) FormatHelp(command, guildID string) string {
45 var shortCodeStr string
46 if bc.ShortCode != "" {
47 shortCodeStr = fmt.Sprintf(" (%s)", FormatCommand(bc.ShortCode, guildID))
48 }
49 return fmt.Sprintf(MsgHelpFmt, FormatCommand(command, guildID)+shortCodeStr, bc.Help)
50}
51
52func FormatCommand(command, guildID string) string {
53 return fmt.Sprintf("`%s%s`", GetPrefix(guildID), command)
54}
55
56func FormatVideo(v *youtube.Video) string {
57 return fmt.Sprintf("**%s** (`%s`)", v.Title, v.Duration.String())
58}
59
60func ParseUserMessage(messageContent, guildID string) (command string, args []string, ok bool) {
61 after, found := strings.CutPrefix(messageContent, GetPrefix(guildID))
62 if !found {
63 return
64 }
65
66 userInput := strings.Split(after, " ")
67 command = strings.ToLower(userInput[0])
68 return command, userInput[1:], len(command) > 0
69}
70
71func GetPrefix(guildID string) string {
72 for _, prefix := range Config.Values.Prefixes {
73 if prefix.Name == guildID {
74 return prefix.Value
75 }
76 }
77
78 Config.Values.Prefixes = append(Config.Values.Prefixes, KeyValuePair{Name: guildID, Value: defaultPrefix})
79 err := Config.Save()
80 if err != nil {
81 logger.Errorf("could not save config: %s", err)
82 }
83 return defaultPrefix
84}
85
86func SetPrefix(guildID, prefixValue string) string {
87 var found bool
88 for i, prefix := range Config.Values.Prefixes {
89 if prefix.Name == guildID {
90 Config.Values.Prefixes[i].Value = prefixValue
91 found = true
92 break
93 }
94 }
95
96 if !found {
97 Config.Values.Prefixes = append(Config.Values.Prefixes, KeyValuePair{Name: guildID, Value: prefixValue})
98 }
99
100 err := Config.Save()
101 if err != nil {
102 logger.Errorf("could not save config: %s", err)
103 }
104 return defaultPrefix
105}
106
107func Search(query string) (videoID string, err error) {
108 resp, err := http.Get("https://www.youtube.com/results?search_query=" + url.QueryEscape(query))
109 if err != nil {
110 return
111 }
112 defer resp.Body.Close()
113
114 pageContent, err := io.ReadAll(resp.Body)
115 if err != nil {
116 return
117 }
118
119 matches := searchPattern.FindAllStringSubmatch(string(pageContent), -1)
120 if len(matches) == 0 {
121 err = errors.New("no video found")
122 return
123 }
124
125 return matches[0][1], nil
126}