src/utils.go (view raw)
1package src
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/bwmarrin/discordgo"
8)
9
10func getVoiceChannelID(s *discordgo.Session, m *discordgo.MessageCreate) (response string, g *discordgo.Guild, voiceChannelID string) {
11 if m.Member == nil {
12 response = "Please, use this inside a server."
13 return
14 }
15
16 _, err := s.Guild(m.GuildID)
17 if err != nil {
18 logger.Errorf("could not update guild: %s", err)
19 response = msgError
20 return
21 }
22
23 g, err = s.State.Guild(m.GuildID)
24 if err != nil {
25 logger.Errorf("could not get guild: %s", err)
26 response = msgError
27 return
28 }
29
30 for _, vs := range g.VoiceStates {
31 if vs.UserID == m.Author.ID {
32 voiceChannelID = vs.ChannelID
33 break
34 }
35 }
36
37 if voiceChannelID == "" {
38 response = "You need to be in a voice channel to use this command."
39 }
40 return
41}
42
43func formatCommand(command, guildID string) string {
44 return fmt.Sprintf("`%s%s`", getPrefix(guildID), command)
45}
46
47func parseUserMessage(messageContent, guildID string) (command string, args []string, ok bool) {
48 after, found := strings.CutPrefix(messageContent, getPrefix(guildID))
49 if !found {
50 return
51 }
52
53 userInput := strings.Split(after, " ")
54 command = strings.ToLower(userInput[0])
55 return command, userInput[1:], len(command) > 0
56}
57
58func getPrefix(guildID string) string {
59 for _, prefix := range Config.Values.Prefixes {
60 if prefix.Name == guildID {
61 return prefix.Value
62 }
63 }
64
65 Config.Values.Prefixes = append(Config.Values.Prefixes, KeyValuePair{Name: guildID, Value: defaultPrefix})
66 err := Config.Save()
67 if err != nil {
68 logger.Errorf("could not save config: %s", err)
69 }
70 return defaultPrefix
71}
72
73func setPrefix(guildID, prefixValue string) string {
74 var found bool
75 for i, prefix := range Config.Values.Prefixes {
76 if prefix.Name == guildID {
77 Config.Values.Prefixes[i].Value = prefixValue
78 found = true
79 break
80 }
81 }
82
83 if !found {
84 Config.Values.Prefixes = append(Config.Values.Prefixes, KeyValuePair{Name: guildID, Value: prefixValue})
85 }
86
87 err := Config.Save()
88 if err != nil {
89 logger.Errorf("could not save config: %s", err)
90 }
91 return defaultPrefix
92}