-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vote, handlers permissions, temp messages, attempt to fix voice disco…
…nnect properly
- Loading branch information
Showing
10 changed files
with
447 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package bot | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
func (b *BotInstance) PlayHandler(m *discordgo.MessageCreate) { | ||
b.PlayQueue() | ||
b.SendNamedNotice(m, "Requested by", "⏯️ Play", "", "") | ||
b.DeleteUserMessage(m) | ||
} | ||
|
||
func (b *BotInstance) PauseHandler(m *discordgo.MessageCreate) { | ||
defer b.DeleteUserMessage(m) | ||
if !b.Player.playing { | ||
b.SendNamedNotice(m, "Requested by", "⏯️ Pause", "Nothing to do", "") | ||
return | ||
} | ||
|
||
if !b.Player.pause { | ||
b.Player.stream.SetPaused(true) | ||
b.Player.pause = true | ||
b.SendNamedNotice(m, "Requested by", "⏯️ Pause", "", "") | ||
} else { | ||
b.SendNamedNotice(m, "Requested by", "⏯️ Pause", "Nothing to do", "") | ||
} | ||
} | ||
|
||
func (b *BotInstance) ResumeHandler(m *discordgo.MessageCreate) { | ||
defer b.DeleteUserMessage(m) | ||
if !b.Player.playing { | ||
b.SendNamedNotice(m, "Requested by", "⏯️ Resume", "Nothing to do", "") | ||
return | ||
} | ||
if b.Player.pause { | ||
b.Player.stream.SetPaused(false) | ||
b.Player.pause = false | ||
b.SendNamedNotice(m, "Requested by", "⏯️ Resume", "", "") | ||
} else { | ||
b.SendNamedNotice(m, "Requested by", "⏯️ Resume", "Nothing to do", "") | ||
} | ||
} | ||
|
||
func (b *BotInstance) StopHandler(m *discordgo.MessageCreate) { | ||
defer b.DeleteUserMessage(m) | ||
if !b.restricted(m) { | ||
b.DisplayTemporaryMessage(m, "", "You do not have permission to stop the player", "") | ||
return | ||
} | ||
if !b.Player.playing { | ||
b.SendNamedNotice(m, "Requested by", "⏹️ Stop", "Nothing to do", "") | ||
return | ||
} | ||
b.Player.session.Stop() // nolint:errcheck | ||
b.Player.stop = true | ||
b.SendNamedNotice(m, "Requested by", "⏹️ Stop", "", "") | ||
} | ||
|
||
func (b *BotInstance) SkipHandler(m *discordgo.MessageCreate) { | ||
defer b.DeleteUserMessage(m) | ||
if !b.restricted(m) { | ||
b.DisplayTemporaryMessage(m, "", "You do not have permission to arbitrarily skip a track", "Tip: Start a vote using '!fox vote'") | ||
return | ||
} | ||
|
||
if b.Player.playing { | ||
b.Player.session.Stop() // nolint:errcheck | ||
b.SendNamedNotice(m, "Requested by", "⏭️ Skip", "The currently playing track has been skipped", "Note: This can take a few seconds") | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
} else { | ||
b.Player.Pop() | ||
b.SendNamedNotice(m, "Requested by", "⏭️ Skip", "The next track in queue has been skipped", "") | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
} | ||
} | ||
|
||
func (b *BotInstance) JoinHandler(m *discordgo.MessageCreate) { | ||
defer b.DeleteUserMessage(m) | ||
if !b.restricted(m) { | ||
b.DisplayTemporaryMessage(m, "", "Permission denied", "Tip: Only admins and DJs can do that") | ||
return | ||
} | ||
|
||
b.log.Debug().Str("user", m.Author.Username).Str("method", "join").Msg("called") | ||
if b.Voice == nil { | ||
voice, err := b.Session.ChannelVoiceJoin(b.conf.Bot.Guild, b.conf.Bot.Channels.Voice, false, true) | ||
if err != nil { | ||
b.SendNamedNotice(m, "Requested by", "Unable to join voice channel", "", "Error was: "+err.Error()) | ||
b.log.Error().Err(err).Msg("unable to connect to voice channel") | ||
return | ||
} | ||
b.Voice = voice | ||
b.log.Debug().Str("user", m.Author.Username).Str("method", "join").Msg("bot joined vocal channel") | ||
} | ||
} | ||
|
||
func (b *BotInstance) LeaveHandler(m *discordgo.MessageCreate) { | ||
defer b.DeleteUserMessage(m) | ||
if !b.restricted(m) { | ||
b.DisplayTemporaryMessage(m, "", "Permission denied", "Tip: Only admins and DJs can do that") | ||
return | ||
} | ||
|
||
b.log.Debug().Str("user", m.Author.Username).Str("method", "leave").Msg("called") | ||
if b.Voice != nil { | ||
if b.Player.playing { | ||
b.Player.session.Stop() // nolint:errcheck | ||
b.Player.stop = true | ||
} | ||
if err := b.Voice.Disconnect(); err != nil { | ||
b.SendNamedNotice(m, "Requested by", "Unable to leave voice channel", "", "Error was: "+err.Error()) | ||
b.log.Error().Err(err).Msg("unable to disconnect from voice channel") | ||
return | ||
} | ||
b.Voice = nil | ||
b.log.Debug().Str("user", m.Author.Username).Str("method", "leave").Msg("bot left vocal channel") | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package bot | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// AddHandler is in charge of pushing a track or playlist to the end of the | ||
// current queue | ||
func (b *BotInstance) AddHandler(m *discordgo.MessageCreate, args []string) { | ||
if len(args) < 1 { | ||
b.SendNotice("", fmt.Sprintf("Usage: `%s <add|a> <soundcloud URL>`", b.conf.Bot.Prefix), "", m.ChannelID) | ||
return | ||
} | ||
|
||
url := args[0] | ||
url = strings.Trim(url, "<>") | ||
if !strings.HasPrefix(url, "https://soundcloud.com") { | ||
b.SendNotice("", "This doesn't look like a SoundCloud URL", "", m.ChannelID) | ||
return | ||
} | ||
|
||
b.AddToQueue(m, url, false) | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
} | ||
|
||
// NextHandler is in charge of pushing a track or playlist in front of the rest | ||
// of the queue | ||
func (b *BotInstance) NextHandler(m *discordgo.MessageCreate, args []string) { | ||
if len(args) < 1 { | ||
b.SendNotice("", fmt.Sprintf("Usage: `%s <next|n> <soundcloud URL>`", b.conf.Bot.Prefix), "", m.ChannelID) | ||
return | ||
} | ||
|
||
url := args[0] | ||
url = strings.Trim(url, "<>") | ||
if !strings.HasPrefix(url, "https://soundcloud.com") { | ||
b.SendNotice("", "This doesn't look like a SoundCloud URL", "", m.ChannelID) | ||
return | ||
} | ||
|
||
b.AddToQueue(m, url, true) | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
} | ||
|
||
// HelpHandler will handle incoming requests for help | ||
func (b *BotInstance) HelpHandler(m *discordgo.MessageCreate) { | ||
var doc = &discordgo.MessageEmbed{ | ||
Title: "Fox Help", | ||
Color: 0xff5500, | ||
Fields: []*discordgo.MessageEmbedField{ | ||
{Name: "Help", Value: fmt.Sprintf("%s <help|h>", b.conf.Bot.Prefix), Inline: true}, | ||
{Name: "Add tracks", Value: fmt.Sprintf("%s <add|a> <soundcloud url>", b.conf.Bot.Prefix), Inline: true}, | ||
{Name: "Add next tracks", Value: fmt.Sprintf("%s <next|n> <soundcloud url>", b.conf.Bot.Prefix)}, | ||
{Name: "Channel", Value: fmt.Sprintf("%s <join/leave>", b.conf.Bot.Prefix)}, | ||
{Name: "Display Queue", Value: fmt.Sprintf("%s <queue|q>", b.conf.Bot.Prefix), Inline: true}, | ||
{Name: "Shuffle Queue", Value: fmt.Sprintf("%s <queue|q> shuffle", b.conf.Bot.Prefix), Inline: true}, | ||
{Name: "Clear Queue", Value: fmt.Sprintf("%s <queue|q> clear", b.conf.Bot.Prefix), Inline: true}, | ||
{Name: "Control", Value: fmt.Sprintf("%s <play/pause/resume/stop>", b.conf.Bot.Prefix)}, | ||
}, | ||
} | ||
_, err := b.Session.ChannelMessageSendEmbed(m.ChannelID, doc) | ||
if err != nil { | ||
log.Err(err).Msg("unable to send embed") | ||
} | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
} | ||
|
||
// QueueHandler is in charge of dealing with queue commands such as displaying | ||
// the current queue, shuffling the queue or in the control channel, clearing it | ||
func (b *BotInstance) QueueHandler(m *discordgo.MessageCreate, args []string) { | ||
if len(args) == 0 { | ||
b.DisplayQueue(m) | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
return | ||
} | ||
switch args[0] { | ||
case "shuffle": | ||
b.Player.Shuffle() | ||
b.SendNamedNotice(m, "Requested by", "🎲 Shuffle!", fmt.Sprintf("I shuffled %d tracks for you.", len(b.Player.tracks)), "") | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
case "clear": // The clear command is not public and shouldn't be used | ||
if b.restricted(m) { | ||
b.Player.Clear() | ||
b.SendNamedNotice(m, "Requested by", "🚮 Clear", "The queue has been reset", "") | ||
if err := b.Session.ChannelMessageDelete(m.ChannelID, m.ID); err != nil { | ||
b.log.Err(err).Msg("unable to delete user message") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.