all repos — disgord @ 4594fc1f9738dd7c5e7d9ec50660b347f0d3a6d2

A simple Discord bot in Go.

minor tweaks
Marco Andronaco andronacomarco@gmail.com
Fri, 04 Oct 2024 17:00:57 +0200
commit

4594fc1f9738dd7c5e7d9ec50660b347f0d3a6d2

parent

788b481ce887db1dccfc707bc18e508405fdd809

M main.gomain.go

@@ -15,11 +15,11 @@ var logger = mylog.NewLogger(os.Stdout, "init", mylog.DEBUG)

func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { - logger.Debugf("Ignoring own message: %s", m.Content) + // logger.Debugf("Ignoring own message: %s", m.Content) return - } else { - logger.Debug("Got a message: " + m.Content) } + + logger.Debug("Got a message: " + m.Content) response, ok, err := src.HandleCommand(s, m) if err != nil {
M src/commands.gosrc/commands.go

@@ -1,12 +1,22 @@

package src import ( + "fmt" "strings" gl "github.com/BiRabittoh/disgord/src/globals" "github.com/BiRabittoh/disgord/src/music" "github.com/BiRabittoh/disgord/src/shoot" "github.com/bwmarrin/discordgo" +) + +const ( + MsgUnknownCommand = "Unknown command: %s." + MsgPrefixSet = "Prefix set to `%s`." + MsgPrefixTooLong = "Prefix is too long." + MsgUsagePrefix = "Usage: %s <new prefix>." + MsgHelp = "**Bot commands:**\n%s" + MsgHelpCommandFmt = "* %s\n" ) var (

@@ -50,7 +60,7 @@ }

botCommand, found := handlersMap[command] if !found { - response = "Unknown command: " + gl.FormatCommand(command, m.GuildID) + response = fmt.Sprintf(MsgUnknownCommand, gl.FormatCommand(command, m.GuildID)) return }

@@ -64,23 +74,24 @@ }

func handlePrefix(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { if len(args) == 0 { - return "Usage: " + gl.FormatCommand("prefix <new prefix>", m.GuildID) + "." + return fmt.Sprintf(MsgUsagePrefix, gl.FormatCommand("prefix", m.GuildID)) } newPrefix := args[0] if len(newPrefix) > 10 { - return "Prefix is too long." + return MsgPrefixTooLong } gl.SetPrefix(m.GuildID, newPrefix) - return "Prefix set to " + newPrefix + "." + return fmt.Sprintf(MsgPrefixSet, newPrefix) } func handleHelp(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { - helpText := "**Bot commands:**\n" + helpText := MsgHelp for command, botCommand := range handlersMap { - helpText += "* " + botCommand.FormatHelp(command, m.GuildID) + "\n" + // helpText += fmt.Sprintf() + helpText += fmt.Sprintf(MsgHelpCommandFmt, botCommand.FormatHelp(command, m.GuildID)) } return helpText }
M src/globals/globals.gosrc/globals/globals.go

@@ -6,12 +6,11 @@

"github.com/BiRabittoh/disgord/src/myconfig" "github.com/BiRabittoh/disgord/src/mylog" "github.com/bwmarrin/discordgo" - "github.com/kkdai/youtube/v2" ) const ( MsgError = "Something went wrong." - helpFmt = "%s - _%s_" + MsgHelpFmt = "%s - _%s_" defaultPrefix = "$" )

@@ -20,7 +19,6 @@ CommitID string

Config *myconfig.Config[MyConfig] logger = mylog.NewLogger(os.Stdout, "main", mylog.DEBUG) - YT = youtube.Client{} ) type KeyValuePair struct {
M src/globals/utils.gosrc/globals/utils.go

@@ -39,7 +39,7 @@ var shortCodeStr string

if bc.ShortCode != "" { shortCodeStr = fmt.Sprintf(" (%s)", FormatCommand(bc.ShortCode, guildID)) } - return fmt.Sprintf(helpFmt, FormatCommand(command, guildID)+shortCodeStr, bc.Help) + return fmt.Sprintf(MsgHelpFmt, FormatCommand(command, guildID)+shortCodeStr, bc.Help) } func FormatCommand(command, guildID string) string {
M src/music/commands.gosrc/music/commands.go

@@ -1,10 +1,28 @@

package music import ( + "fmt" + gl "github.com/BiRabittoh/disgord/src/globals" "github.com/bwmarrin/discordgo" + "github.com/kkdai/youtube/v2" +) + +const ( + MsgNoURL = "Please, provide a YouTube URL." + MsgAddedToQueue = "Added to queue: %s." + MsgNothingIsPlaying = "Nothing is playing." + MsgSameVoiceChannel = "You need to be in the same voice channel to use this command." + MsgPaused = "Paused." + MsgResumed = "Resumed." + MsgSkipped = "Skipped." + MsgCleared = "Cleared." + MsgLeft = "Left." + MsgQueueLine = "%d. %s\n" ) +var yt = youtube.Client{} + func HandlePlay(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { r, _, vc := gl.GetVoiceChannelID(s, m) if r != "" {

@@ -12,7 +30,7 @@ return r

} if len(args) == 0 { - return "Please, provide a YouTube URL." + return MsgNoURL } voice, err := s.ChannelVoiceJoin(m.GuildID, vc, false, true)

@@ -22,7 +40,7 @@ return gl.MsgError

} // Get the video information - video, err := gl.YT.GetVideo(args[0]) + video, err := yt.GetVideo(args[0]) if err != nil { logger.Errorf("could not get video: %v", err) return gl.MsgError

@@ -34,7 +52,7 @@

// Add video to the queue q.AddVideo(video) - return "Added to queue: " + gl.FormatVideo(video) + return fmt.Sprintf(MsgAddedToQueue, gl.FormatVideo(video)) } func HandlePause(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {

@@ -45,16 +63,16 @@ }

q := GetQueue(g.ID) if q == nil { - return "Nothing is playing." + return MsgNothingIsPlaying } if vc != q.VoiceChannelID() { - return "You need to be in the same voice channel to use this command." + return MsgSameVoiceChannel } q.Pause() - return "Paused." + return MsgPaused } func HandleResume(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {

@@ -65,16 +83,16 @@ }

q := GetQueue(g.ID) if q == nil { - return "Nothing is playing." + return MsgNothingIsPlaying } if vc != q.VoiceChannelID() { - return "You need to be in the same voice channel to use this command." + return MsgSameVoiceChannel } q.Resume() - return "Resumed." + return MsgResumed } func HandleSkip(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {

@@ -85,31 +103,31 @@ }

q := GetQueue(g.ID) if q == nil { - return "Nothing is playing." + return MsgNothingIsPlaying } if vc != q.VoiceChannelID() { - return "You need to be in the same voice channel to use this command." + return MsgSameVoiceChannel } err := q.PlayNext() if err != nil { - return "Nothing is playing." + return MsgNothingIsPlaying } - return "Skipped." + return MsgSkipped } func HandleQueue(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { q := GetQueue(m.GuildID) if q == nil { - return "Nothing is playing." + return MsgNothingIsPlaying } var out string videos := q.Videos() - for _, v := range videos { - out += gl.FormatVideo(v) + "\n" + for i, v := range videos { + out += fmt.Sprintf(MsgQueueLine, i, gl.FormatVideo(v)) } return out }

@@ -122,16 +140,16 @@ }

q := GetQueue(g.ID) if q == nil { - return "Nothing is playing." + return MsgNothingIsPlaying } if vc != q.VoiceChannelID() { - return "You need to be in the same voice channel to use this command." + return MsgSameVoiceChannel } q.Clear() - return "Cleared." + return MsgCleared } func HandleLeave(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {

@@ -142,11 +160,11 @@ }

q := GetQueue(g.ID) if q == nil { - return "Nothing is playing." + return MsgNothingIsPlaying } if vc != q.VoiceChannelID() { - return "You need to be in the same voice channel to use this command." + return MsgSameVoiceChannel } err := q.Stop()

@@ -154,5 +172,5 @@ if err != nil {

return gl.MsgError } - return "Cleared." + return MsgLeft }
M src/music/queue.gosrc/music/queue.go

@@ -21,14 +21,18 @@ // queues stores all guild queues

var queues = map[string]*Queue{} // GetOrCreateQueue fetches or creates a new queue for the guild -func GetOrCreateQueue(vc *discordgo.VoiceConnection) *Queue { +func GetOrCreateQueue(vc *discordgo.VoiceConnection) (q *Queue) { q, ok := queues[vc.GuildID] if !ok { q = &Queue{vc: vc} queues[vc.GuildID] = q + return } - return q + if q.vc.Ready == false { + q.vc = vc + } + return } // GetQueue returns either nil or the queue for the requested guild

@@ -70,7 +74,7 @@

q.nowPlaying = q.items[0] q.items = q.items[1:] - formats := q.nowPlaying.Formats.WithAudioChannels().Type("audio/webm") + formats := q.nowPlaying.Formats.WithAudioChannels() if len(formats) == 0 { logger.Debug("no formats with audio channels available for video " + q.nowPlaying.ID) return q.PlayNext()