From afd62a192a409e93d48d6680e7e46d2fd2e06e25 Mon Sep 17 00:00:00 2001 From: Paul Lhussiez Date: Fri, 13 Nov 2020 20:22:35 +0100 Subject: [PATCH] play ambient and volume fix --- acl/acl.go | 28 ++++++++++++++++++++++++++++ bot/handler.go | 4 +++- commands/command.go | 28 ++++++++-------------------- commands/player.go | 22 ++++++++++++++++++---- commands/volume.go | 2 +- 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/acl/acl.go b/acl/acl.go index 2588be5..761b0ad 100644 --- a/acl/acl.go +++ b/acl/acl.go @@ -59,6 +59,34 @@ func (a ACL) Check(r RoleRestriction, c ChannelRestriction, u *discordgo.Member, return true } +func RoleRestrictionString(r RoleRestriction) string { + var rr string + + switch r { + case Admin: + rr = "🔐 Admin" + case Privileged: + rr = "🔒 Admin or DJ" + case Anyone: + rr = "🔓 No restriction" + } + + return rr +} + +func ChannelRestrictionString(c ChannelRestriction) string { + var cr string + + switch c { + case Music: + cr = "🎶 Music text channel only" + case Anywhere: + cr = "🌍 No restriction" + } + + return cr +} + // IsMusic will check if the provided message was sent to the music channel. func (a ACL) IsMusic(m *discordgo.Message) bool { return m.ChannelID == a.MusicChannelID diff --git a/bot/handler.go b/bot/handler.go index 3719b47..2925974 100644 --- a/bot/handler.go +++ b/bot/handler.go @@ -5,6 +5,7 @@ import ( "strings" "time" + "github.com/Depado/fox/acl" "github.com/Depado/fox/message" "github.com/bwmarrin/discordgo" ) @@ -71,7 +72,8 @@ func (b *Bot) MessageCreatedHandler(s *discordgo.Session, m *discordgo.MessageCr // Check permissions cr, rr := c.ACL() if !b.acl.Check(rr, cr, member, m.Message) { - err := message.SendTimedReply(s, m.Message, "", "You do not have the permission to do that", "", 5*time.Second) + msg := fmt.Sprintf("You do not have permission to do that.\n%s\n%s", acl.RoleRestrictionString(rr), acl.ChannelRestrictionString(cr)) + err := message.SendTimedReply(s, m.Message, "", msg, "", 5*time.Second) if err != nil { b.log.Err(err).Msg("unable to send timed reply") } diff --git a/commands/command.go b/commands/command.go index c2823e6..55fc542 100644 --- a/commands/command.go +++ b/commands/command.go @@ -90,26 +90,14 @@ func (c BaseCommand) GetHelp() Help { func (c BaseCommand) DisplayHelp(s *discordgo.Session, m *discordgo.Message, prefix string) { desc := c.Help.Description - - var cr string - switch c.ChannelRestriction { - case acl.Music: - cr = "🎶 Music text channel only" - case acl.Anywhere: - cr = "🌍 No restriction" - } - desc += fmt.Sprintf("\n\nChannel Restriction\n**%s**", cr) - - var rr string - switch c.RoleRestriction { - case acl.Admin: - rr = "🔐 Admin" - case acl.Privileged: - rr = "🔒 Admin or DJ" - case acl.Anyone: - rr = "🔓 No restriction" - } - desc += fmt.Sprintf("\n\nRole Restriction\n**%s**", rr) + desc += fmt.Sprintf( + "\n\nChannel Restriction\n**%s**", + acl.ChannelRestrictionString(c.ChannelRestriction), + ) + desc += fmt.Sprintf( + "\n\nRole Restriction\n**%s**", + acl.RoleRestrictionString(c.RoleRestriction), + ) var aliases string if len(c.Aliases) > 0 { diff --git a/commands/player.go b/commands/player.go index 22dc948..486aadc 100644 --- a/commands/player.go +++ b/commands/player.go @@ -30,10 +30,18 @@ func (c *play) Handler(s *discordgo.Session, m *discordgo.Message, args []string } return } - c.Player.Play() + msg := fmt.Sprintf("▶️ Started playing for <@%s>", m.Author.ID) + if len(args) > 0 && args[0] == "ambient" { + if err := c.Player.SetVolumePercent(50); err != nil { + c.log.Err(err).Msg("unable to set volume") + } else { + msg += " in ambient mode" + } + } + c.Player.Play() if err := message.SendReply(s, m, "", msg, ""); err != nil { - c.log.Err(err).Msg("unable to ") + c.log.Err(err).Msg("unable to send reply") } } @@ -52,8 +60,14 @@ func NewPlayCommand(p *player.Player, log *zerolog.Logger) Command { Usage: cmd, ShortDesc: "Start playing the queue", Description: "This command will start playing the queue. " + - "It has no effect if the queue is the player is already " + - "active.\nThe bot will join the vocal channel when playing starts", + "It has no effect if the player is already " + + "active.\nThe bot will join the vocal channel when playing " + + "starts.\n\n`ambient` can be passed as an extra argument " + + "to play in ambient mode with a lower volume.", + Examples: []Example{ + {Command: "play", Explanation: "Start playing"}, + {Command: "play ambient", Explanation: "Start playing in ambient mode"}, + }, }, Player: p, log: log.With().Str("command", cmd).Logger(), diff --git a/commands/volume.go b/commands/volume.go index 6d681fa..b75ab8f 100644 --- a/commands/volume.go +++ b/commands/volume.go @@ -23,7 +23,7 @@ func (c *volume) Handler(s *discordgo.Session, m *discordgo.Message, args []stri var emoji = "🔉" if len(args) < 1 { - v = c.Player.State.Volume / 256 * 100 + v = c.Player.State.Volume * 100 / 256 if v > 100 { emoji = "🔊" } else if v < 100 {