Cleaned and relocated some code
This commit is contained in:
parent
3f874d6322
commit
bc43b183de
194
commands.go
194
commands.go
|
@ -35,96 +35,49 @@ func parseCommand(user *gumble.User, username, command string) {
|
||||||
// Add command
|
// Add command
|
||||||
case dj.conf.Aliases.AddAlias:
|
case dj.conf.Aliases.AddAlias:
|
||||||
if dj.HasPermission(username, dj.conf.Permissions.AdminAdd) {
|
if dj.HasPermission(username, dj.conf.Permissions.AdminAdd) {
|
||||||
if argument == "" {
|
add(user, username, argument)
|
||||||
user.Send(NO_ARGUMENT_MSG)
|
|
||||||
} else {
|
|
||||||
if songTitle, err := add(username, argument); err == nil {
|
|
||||||
dj.client.Self().Channel().Send(fmt.Sprintf(SONG_ADDED_HTML, username, songTitle), false)
|
|
||||||
if dj.queue.Len() == 1 && !dj.audioStream.IsPlaying() {
|
|
||||||
dj.currentSong = dj.queue.NextSong()
|
|
||||||
if err := dj.currentSong.Download(); err == nil {
|
|
||||||
dj.currentSong.Play()
|
|
||||||
} else {
|
|
||||||
user.Send(AUDIO_FAIL_MSG)
|
|
||||||
dj.currentSong.Delete()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
user.Send(INVALID_URL_MSG)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
user.Send(NO_PERMISSION_MSG)
|
user.Send(NO_PERMISSION_MSG)
|
||||||
}
|
}
|
||||||
// Skip command
|
// Skip command
|
||||||
case dj.conf.Aliases.SkipAlias:
|
case dj.conf.Aliases.SkipAlias:
|
||||||
if dj.HasPermission(username, dj.conf.Permissions.AdminSkip) {
|
if dj.HasPermission(username, dj.conf.Permissions.AdminSkip) {
|
||||||
if err := skip(username, false); err == nil {
|
skip(username, false)
|
||||||
dj.client.Self().Channel().Send(fmt.Sprintf(SKIP_ADDED_HTML, username), false)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
user.Send(NO_PERMISSION_MSG)
|
user.Send(NO_PERMISSION_MSG)
|
||||||
}
|
}
|
||||||
// Forceskip command
|
// Forceskip command
|
||||||
case dj.conf.Aliases.AdminSkipAlias:
|
case dj.conf.Aliases.AdminSkipAlias:
|
||||||
if dj.HasPermission(username, true) {
|
if dj.HasPermission(username, true) {
|
||||||
if err := skip(username, true); err == nil {
|
skip(username, true)
|
||||||
dj.client.Self().Channel().Send(ADMIN_SONG_SKIP_MSG, false)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
user.Send(NO_PERMISSION_MSG)
|
user.Send(NO_PERMISSION_MSG)
|
||||||
}
|
}
|
||||||
// Volume command
|
// Volume command
|
||||||
case dj.conf.Aliases.VolumeAlias:
|
case dj.conf.Aliases.VolumeAlias:
|
||||||
if dj.HasPermission(username, dj.conf.Permissions.AdminVolume) {
|
if dj.HasPermission(username, dj.conf.Permissions.AdminVolume) {
|
||||||
if argument == "" {
|
volume(user, username, argument)
|
||||||
dj.client.Self().Channel().Send(fmt.Sprintf(CUR_VOLUME_HTML, dj.audioStream.Volume()), false)
|
|
||||||
} else {
|
|
||||||
if err := volume(username, argument); err == nil {
|
|
||||||
dj.client.Self().Channel().Send(fmt.Sprintf(VOLUME_SUCCESS_HTML, username, argument), false)
|
|
||||||
} else {
|
|
||||||
user.Send(fmt.Sprintf(NOT_IN_VOLUME_RANGE_MSG, dj.conf.Volume.LowestVolume, dj.conf.Volume.HighestVolume))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
user.Send(NO_PERMISSION_MSG)
|
user.Send(NO_PERMISSION_MSG)
|
||||||
}
|
}
|
||||||
// Move command
|
// Move command
|
||||||
case dj.conf.Aliases.MoveAlias:
|
case dj.conf.Aliases.MoveAlias:
|
||||||
if dj.HasPermission(username, dj.conf.Permissions.AdminMove) {
|
if dj.HasPermission(username, dj.conf.Permissions.AdminMove) {
|
||||||
if argument == "" {
|
move(user, argument)
|
||||||
user.Send(NO_ARGUMENT_MSG)
|
|
||||||
} else {
|
|
||||||
if err := move(argument); err == nil {
|
|
||||||
fmt.Printf("%s has been moved to %s.", dj.client.Self().Name(), argument)
|
|
||||||
} else {
|
|
||||||
user.Send(CHANNEL_DOES_NOT_EXIST_MSG)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
user.Send(NO_PERMISSION_MSG)
|
user.Send(NO_PERMISSION_MSG)
|
||||||
}
|
}
|
||||||
// Reload command
|
// Reload command
|
||||||
case dj.conf.Aliases.ReloadAlias:
|
case dj.conf.Aliases.ReloadAlias:
|
||||||
if dj.HasPermission(username, dj.conf.Permissions.AdminReload) {
|
if dj.HasPermission(username, dj.conf.Permissions.AdminReload) {
|
||||||
err := loadConfiguration()
|
reload(user)
|
||||||
if err == nil {
|
|
||||||
user.Send(CONFIG_RELOAD_SUCCESS_MSG)
|
|
||||||
} else {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
user.Send(NO_PERMISSION_MSG)
|
user.Send(NO_PERMISSION_MSG)
|
||||||
}
|
}
|
||||||
// Kill command
|
// Kill command
|
||||||
case dj.conf.Aliases.KillAlias:
|
case dj.conf.Aliases.KillAlias:
|
||||||
if dj.HasPermission(username, dj.conf.Permissions.AdminKill) {
|
if dj.HasPermission(username, dj.conf.Permissions.AdminKill) {
|
||||||
if err := kill(); err == nil {
|
kill()
|
||||||
fmt.Println("Kill successful. Goodbye!")
|
|
||||||
os.Exit(0)
|
|
||||||
} else {
|
|
||||||
user.Send(KILL_ERROR_MSG)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
user.Send(NO_PERMISSION_MSG)
|
user.Send(NO_PERMISSION_MSG)
|
||||||
}
|
}
|
||||||
|
@ -135,100 +88,133 @@ func parseCommand(user *gumble.User, username, command string) {
|
||||||
|
|
||||||
// Performs add functionality. Checks input URL for YouTube format, and adds
|
// Performs add functionality. Checks input URL for YouTube format, and adds
|
||||||
// the URL to the queue if the format matches.
|
// the URL to the queue if the format matches.
|
||||||
func add(user, url string) (string, error) {
|
func add(user *gumble.User, username, url string) {
|
||||||
youtubePatterns := []string{
|
if url == "" {
|
||||||
`https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)`,
|
user.Send(NO_ARGUMENT_MSG)
|
||||||
`https?:\/\/youtube\.com\/watch\?v=([\w-]+)`,
|
} else {
|
||||||
`https?:\/\/youtu.be\/([\w-]+)`,
|
youtubePatterns := []string{
|
||||||
`https?:\/\/youtube.com\/v\/([\w-]+)`,
|
`https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)`,
|
||||||
`https?:\/\/www.youtube.com\/v\/([\w-]+)`,
|
`https?:\/\/youtube\.com\/watch\?v=([\w-]+)`,
|
||||||
}
|
`https?:\/\/youtu.be\/([\w-]+)`,
|
||||||
matchFound := false
|
`https?:\/\/youtube.com\/v\/([\w-]+)`,
|
||||||
shortUrl := ""
|
`https?:\/\/www.youtube.com\/v\/([\w-]+)`,
|
||||||
|
}
|
||||||
|
matchFound := false
|
||||||
|
shortUrl := ""
|
||||||
|
|
||||||
for _, pattern := range youtubePatterns {
|
for _, pattern := range youtubePatterns {
|
||||||
if re, err := regexp.Compile(pattern); err == nil {
|
if re, err := regexp.Compile(pattern); err == nil {
|
||||||
if re.MatchString(url) {
|
if re.MatchString(url) {
|
||||||
matchFound = true
|
matchFound = true
|
||||||
shortUrl = re.FindStringSubmatch(url)[1]
|
shortUrl = re.FindStringSubmatch(url)[1]
|
||||||
break
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if matchFound {
|
if matchFound {
|
||||||
newSong := NewSong(user, shortUrl)
|
newSong := NewSong(username, shortUrl)
|
||||||
if err := dj.queue.AddSong(newSong); err == nil {
|
if err := dj.queue.AddSong(newSong); err == nil {
|
||||||
return newSong.title, nil
|
dj.client.Self().Channel().Send(fmt.Sprintf(SONG_ADDED_HTML, username, newSong.title), false)
|
||||||
|
if dj.queue.Len() == 1 && !dj.audioStream.IsPlaying() {
|
||||||
|
dj.currentSong = dj.queue.NextSong()
|
||||||
|
if err := dj.currentSong.Download(); err == nil {
|
||||||
|
dj.currentSong.Play()
|
||||||
|
} else {
|
||||||
|
user.Send(AUDIO_FAIL_MSG)
|
||||||
|
dj.currentSong.Delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic(errors.New("Could not add the Song to the queue."))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return "", errors.New("Could not add the Song to the queue.")
|
user.Send(INVALID_URL_MSG)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return "", errors.New("The URL provided did not match a YouTube URL.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs skip functionality. Adds a skip to the skippers slice for the current song, and then
|
// Performs skip functionality. Adds a skip to the skippers slice for the current song, and then
|
||||||
// evaluates if a skip should be performed. Both skip and forceskip are implemented here.
|
// evaluates if a skip should be performed. Both skip and forceskip are implemented here.
|
||||||
func skip(user string, admin bool) error {
|
func skip(user string, admin bool) {
|
||||||
if err := dj.currentSong.AddSkip(user); err == nil {
|
if err := dj.currentSong.AddSkip(user); err == nil {
|
||||||
|
if admin {
|
||||||
|
dj.client.Self().Channel().Send(ADMIN_SONG_SKIP_MSG, false)
|
||||||
|
} else {
|
||||||
|
dj.client.Self().Channel().Send(fmt.Sprintf(SKIP_ADDED_HTML, user), false)
|
||||||
|
}
|
||||||
if dj.currentSong.SkipReached(len(dj.client.Self().Channel().Users())) || admin {
|
if dj.currentSong.SkipReached(len(dj.client.Self().Channel().Users())) || admin {
|
||||||
|
dj.client.Self().Channel().Send(SONG_SKIPPED_HTML, false)
|
||||||
if err := dj.audioStream.Stop(); err == nil {
|
if err := dj.audioStream.Stop(); err == nil {
|
||||||
dj.OnSongFinished()
|
dj.OnSongFinished()
|
||||||
return nil
|
|
||||||
} else {
|
} else {
|
||||||
return errors.New("An error occurred while stopping the current song.")
|
panic(errors.New("An error occurred while stopping the current song."))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return errors.New("An error occurred while adding a skip to the current song.")
|
panic(errors.New("An error occurred while adding a skip to the current song."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs volume functionality. Checks input value against LowestVolume and HighestVolume from
|
// Performs volume functionality. Checks input value against LowestVolume and HighestVolume from
|
||||||
// config to determine if the volume should be applied. If in the correct range, the new volume
|
// config to determine if the volume should be applied. If in the correct range, the new volume
|
||||||
// is applied and is immediately in effect.
|
// is applied and is immediately in effect.
|
||||||
func volume(user, value string) error {
|
func volume(user *gumble.User, username, value string) {
|
||||||
if parsedVolume, err := strconv.ParseFloat(value, 32); err == nil {
|
if value == "" {
|
||||||
newVolume := float32(parsedVolume)
|
dj.client.Self().Channel().Send(fmt.Sprintf(CUR_VOLUME_HTML, dj.audioStream.Volume()), false)
|
||||||
if newVolume >= dj.conf.Volume.LowestVolume && newVolume <= dj.conf.Volume.HighestVolume {
|
|
||||||
dj.audioStream.SetVolume(newVolume)
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return errors.New("The volume supplied was not in the allowed range.")
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return errors.New("An error occurred while parsing the volume string.")
|
if parsedVolume, err := strconv.ParseFloat(value, 32); err == nil {
|
||||||
|
newVolume := float32(parsedVolume)
|
||||||
|
if newVolume >= dj.conf.Volume.LowestVolume && newVolume <= dj.conf.Volume.HighestVolume {
|
||||||
|
dj.audioStream.SetVolume(newVolume)
|
||||||
|
dj.client.Self().Channel().Send(fmt.Sprintf(VOLUME_SUCCESS_HTML, username, dj.audioStream.Volume()), false)
|
||||||
|
} else {
|
||||||
|
user.Send(fmt.Sprintf(NOT_IN_VOLUME_RANGE_MSG, dj.conf.Volume.LowestVolume, dj.conf.Volume.HighestVolume))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
user.Send(fmt.Sprintf(NOT_IN_VOLUME_RANGE_MSG, dj.conf.Volume.LowestVolume, dj.conf.Volume.HighestVolume))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs move functionality. Determines if the supplied channel is valid and moves the bot
|
// Performs move functionality. Determines if the supplied channel is valid and moves the bot
|
||||||
// to the channel if it is.
|
// to the channel if it is.
|
||||||
func move(channel string) error {
|
func move(user *gumble.User, channel string) {
|
||||||
if dj.client.Channels().Find(channel) != nil {
|
if channel == "" {
|
||||||
dj.client.Self().Move(dj.client.Channels().Find(channel))
|
user.Send(NO_ARGUMENT_MSG)
|
||||||
return nil
|
|
||||||
} else {
|
} else {
|
||||||
return errors.New("The channel provided does not exist.")
|
if dj.client.Channels().Find(channel) != nil {
|
||||||
|
dj.client.Self().Move(dj.client.Channels().Find(channel))
|
||||||
|
} else {
|
||||||
|
user.Send(CHANNEL_DOES_NOT_EXIST_MSG)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Performs reload functionality. Tells command submitter if the reload completed successfully.
|
||||||
|
func reload(user *gumble.User) {
|
||||||
|
if err := loadConfiguration(); err == nil {
|
||||||
|
user.Send(CONFIG_RELOAD_SUCCESS_MSG)
|
||||||
|
} else {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs kill functionality. First cleans the ~/.mumbledj/songs directory to get rid of any
|
// Performs kill functionality. First cleans the ~/.mumbledj/songs directory to get rid of any
|
||||||
// excess m4a files. The bot then safely disconnects from the server.
|
// excess m4a files. The bot then safely disconnects from the server.
|
||||||
func kill() error {
|
func kill() {
|
||||||
songsDir := fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir)
|
songsDir := fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir)
|
||||||
if err := os.RemoveAll(songsDir); err != nil {
|
if err := os.RemoveAll(songsDir); err != nil {
|
||||||
return errors.New("An error occurred while deleting the audio files.")
|
panic(errors.New("An error occurred while deleting the audio files."))
|
||||||
} else {
|
} else {
|
||||||
if err := os.Mkdir(songsDir, 0777); err != nil {
|
if err := os.Mkdir(songsDir, 0777); err != nil {
|
||||||
return errors.New("An error occurred while recreating the songs directory.")
|
panic(errors.New("An error occurred while recreating the songs directory."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := dj.client.Disconnect(); err == nil {
|
if err := dj.client.Disconnect(); err == nil {
|
||||||
return nil
|
fmt.Println("Kill successful. Goodbye!")
|
||||||
|
os.Exit(0)
|
||||||
} else {
|
} else {
|
||||||
return errors.New("An error occurred while disconnecting from the server.")
|
panic(errors.New("An error occurred while disconnecting from the server."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue