all repos — disgord @ main

A simple Discord bot in Go.

src/globals/utils.go (view raw)

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