Skip to content

Commit

Permalink
Now using maps for custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTipo01 committed Sep 13, 2020
1 parent cce1984 commit 6236fbb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
43 changes: 21 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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, ", ")
Expand Down Expand Up @@ -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
}

}
Expand Down
8 changes: 0 additions & 8 deletions structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
28 changes: 13 additions & 15 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}
}

Expand Down

0 comments on commit 6236fbb

Please sign in to comment.