From 6236fbbbf2e725fb1ffa325e6c9f5c3a232c942e Mon Sep 17 00:00:00 2001 From: Manuel <10187614+TheTipo01@users.noreply.github.com> Date: Sun, 13 Sep 2020 18:03:36 +0200 Subject: [PATCH] Now using maps for custom commands --- main.go | 43 +++++++++++++++++++++---------------------- structures.go | 8 -------- utilities.go | 28 +++++++++++++--------------- 3 files changed, 34 insertions(+), 45 deletions(-) diff --git a/main.go b/main.go index 688393d..7c97098 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,12 @@ import ( "context" "database/sql" "fmt" + "github.com/bwmarrin/discordgo" + _ "github.com/go-sql-driver/mysql" + _ "github.com/mattn/go-sqlite3" + "github.com/spf13/viper" + "github.com/zmb3/spotify" + "golang.org/x/oauth2/clientcredentials" "log" "math/rand" "os" @@ -13,12 +19,6 @@ import ( "sync" "syscall" "time" - "github.com/bwmarrin/discordgo" - _ "github.com/go-sql-driver/mysql" - _ "github.com/mattn/go-sqlite3" - "github.com/spf13/viper" - "github.com/zmb3/spotify" - "golang.org/x/oauth2/clientcredentials" ) var ( @@ -36,8 +36,8 @@ var ( queue = make(map[string][]Queue) //Voice connection vc = make(map[string]*discordgo.VoiceConnection) - //Custom commands - custom = make(map[string][]CustomCommand) + //Custom commands, first map is for the guild id, second one is for the command, and the final string for the song + custom = make(map[string]map[string]string) //Spotify client client spotify.Client //Genius key @@ -303,8 +303,8 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if len(custom[m.GuildID]) > 0 { message += "\nCustom commands:\n```" - for _, c := range custom[m.GuildID] { - message += c.command + ", " + for k := range custom[m.GuildID] { + message += k + ", " } message = strings.TrimSuffix(message, ", ") @@ -406,27 +406,26 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //Makes the bot exit case prefix + "restart", prefix + "r": + go deleteMessage(s, m) os.Exit(0) //We search for possible custom commands default: - lowerMessage := strings.ToLower(m.Content) + lower := strings.TrimPrefix(strings.ToLower(m.Content), prefix) - for _, command := range custom[m.GuildID] { - if lowerMessage == prefix+command.command { - go deleteMessage(s, m) + if custom[m.GuildID][lower] != "" { + go deleteMessage(s, m) - if isValidUrl(command.song) { - downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), command.song, m.Author.Username, m.ChannelID, false) + if isValidUrl(custom[m.GuildID][lower]) { + downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), custom[m.GuildID][lower], m.Author.Username, m.ChannelID, false) + } else { + if strings.HasPrefix(custom[m.GuildID][lower], "spotify:playlist:") { + spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID, false) } else { - if strings.HasPrefix(command.song, "spotify:playlist:") { - spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID, false) - } else { - searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), command.song, m.Author.Username, m.ChannelID, false) - } + searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), custom[m.GuildID][lower], m.Author.Username, m.ChannelID, false) } - break } + break } } diff --git a/structures.go b/structures.go index bd2cdb0..071e22f 100644 --- a/structures.go +++ b/structures.go @@ -154,14 +154,6 @@ type Queue struct { messageID []discordgo.Message } -//Structure for holding infos about a custom command -type CustomCommand struct { - //Name of the command - command string - //Song link - song string -} - //Structure for getting lyrics of a song type Lyrics struct { Type string `json:"_type"` diff --git a/utilities.go b/utilities.go index 27063ea..44c5431 100644 --- a/utilities.go +++ b/utilities.go @@ -136,14 +136,15 @@ func checkInDb(link string) Queue { //Adds a custom command to db and to the command map func addCommand(command string, song string, guild string) { - for _, c := range custom[guild] { - if c.song == song && c.command == command { - //If the song is already in the map, we ignore it - return - } + //If the song is already in the map, we ignore it + if custom[guild][command] == song { + return } - custom[guild] = append(custom[guild], CustomCommand{command, song}) + //Else, we add it to the map + custom[guild][command] = song + + //And to the database statement, _ := db.Prepare("INSERT INTO customCommands (guild, command, song) VALUES(?, ?, ?)") _, err := statement.Exec(guild, command, song) @@ -163,14 +164,7 @@ func removeCustom(command string, guild string) { } //Remove from the map - for i := range custom[guild] { - if custom[guild][i].command == command { - custom[guild][i] = custom[guild][len(custom[guild])-1] - custom[guild][len(custom[guild])-1] = CustomCommand{"", ""} - custom[guild] = custom[guild][:len(custom[guild])-1] - break - } - } + delete(custom[guild], command) } //Loads custom command from the database @@ -189,7 +183,11 @@ func loadCustomCommands(db *sql.DB) { continue } - custom[guild] = append(custom[guild], CustomCommand{command, song}) + if custom[guild] == nil { + custom[guild] = make(map[string]string) + } + + custom[guild][command] = song } }