Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
fix: case insenstitive commands
Browse files Browse the repository at this point in the history
- remove random avatar images
  • Loading branch information
keshon committed Jul 15, 2024
1 parent 5fbc0cf commit 7c7bbb7
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 85 deletions.
Binary file removed assets/avatars/1.png
Binary file not shown.
Binary file removed assets/avatars/2.png
Binary file not shown.
Binary file removed assets/avatars/3.png
Binary file not shown.
23 changes: 16 additions & 7 deletions internal/manager/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,29 @@ func (gm *GuildManager) splitCommandFromParameter(content, commandPrefix string)
return "", "", fmt.Errorf("command prefix not found")
}

commandAndParams := content[len(commandPrefix):]
prefixLowercase := strings.ToLower(commandPrefix)
contentLowercase := strings.ToLower(content)

words := strings.Fields(commandAndParams)
if !strings.HasPrefix(contentLowercase, prefixLowercase) {
return "", "", nil // fmt.Errorf("pattern not found")
}

content = content[len(commandPrefix):]

words := strings.Fields(content)
if len(words) == 0 {
return "", "", fmt.Errorf("no command found")
}

command := strings.ToLower(words[0])
param := ""
command := words[0]
parameter := ""
if len(words) > 1 {
param = strings.Join(words[1:], " ")
param = strings.TrimSpace(param)
parameter = strings.Join(words[1:], " ")
parameter = strings.TrimSpace(parameter)
}
return command, param, nil

command = strings.ToLower(command)
return command, parameter, nil
}

func (gm *GuildManager) getEffectiveCommandPrefix() string {
Expand Down
1 change: 0 additions & 1 deletion mods/about/discord/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
func (d *Discord) handleAboutCommand() {
s := d.Session
m := d.Message
d.changeAvatar(s)

title := "ℹ️ About"
content := fmt.Sprintf("**%v** — %v", version.AppFullName, version.AppDescription)
Expand Down
28 changes: 10 additions & 18 deletions mods/about/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ func NewDiscord(session *discordgo.Session) *Discord {
config := loadConfig()

return &Discord{
Session: session,
IsInstanceActive: true,
CommandPrefix: config.DiscordCommandPrefix,
RateLimitDuration: time.Minute * 10,
Session: session,
IsInstanceActive: true,
CommandPrefix: config.DiscordCommandPrefix,
}
}

Expand All @@ -46,6 +45,7 @@ func (d *Discord) Start(guildID string, commandPrefix string) {
d.Session.AddHandler(d.Commands)
d.GuildID = guildID
d.CommandPrefix = commandPrefix
d.setupAvatar(d.Session)
}

func (d *Discord) Stop() {
Expand Down Expand Up @@ -99,30 +99,24 @@ func parseCommand(input, pattern string) (string, string, error) {
return command, parameter, nil
}

func getCanonicalCommand(alias string, commandAliases [][]string) string {
alias = strings.ToLower(alias)
func getCanonicalCommand(command string, commandAliases [][]string) string {
lowerCommand := strings.ToLower(command)
for _, aliases := range commandAliases {
for _, command := range aliases {
if strings.ToLower(command) == alias {
for _, alias := range aliases {
if strings.ToLower(alias) == lowerCommand {
return strings.ToLower(aliases[0])
}
}
}
return ""
}

func (d *Discord) changeAvatar(s *discordgo.Session) {
func (d *Discord) setupAvatar(s *discordgo.Session) {
if time.Since(d.LastChangeAvatarTime) < d.RateLimitDuration {
return
}

imgPath, err := utils.GetRandomImagePathFromPath("./assets/avatars")
if err != nil {
slog.Error("Error getting random image path:", err)
return
}

avatar, err := utils.ReadFileToBase64(imgPath)
avatar, err := utils.ReadFileToBase64("./assets/avatars/0.png")
if err != nil {
slog.Error("Error reading file to base64:", err)
return
Expand All @@ -133,8 +127,6 @@ func (d *Discord) changeAvatar(s *discordgo.Session) {
slog.Error("Error updating user avatar:", err)
return
}

d.LastChangeAvatarTime = time.Now()
}

func (d *Discord) sendMessageEmbed(embedStr string) *discordgo.Message {
Expand Down
1 change: 0 additions & 1 deletion mods/about/discord/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
func (d *Discord) handleHelpCommand(param string) {
s := d.Session
m := d.Message
d.changeAvatar(s)

cfg, err := config.NewConfig()
if err != nil {
Expand Down
67 changes: 17 additions & 50 deletions mods/music/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import (
"errors"
"fmt"
"strings"
"time"

embed "github.com/Clinet/discordgo-embed"
"github.com/bwmarrin/discordgo"
"github.com/gookit/slog"
"github.com/keshon/melodix-player/internal/config"
"github.com/keshon/melodix-player/mods/music/player"
"github.com/keshon/melodix-player/mods/music/utils"
)

type Discord struct {
Player player.IPlayer
Session *discordgo.Session
Message *discordgo.MessageCreate
GuildID string
IsInstanceActive bool
prefix string
lastChangeAvatarTime time.Time
rateLimitDuration time.Duration
Player player.IPlayer
Session *discordgo.Session
Message *discordgo.MessageCreate
GuildID string
IsInstanceActive bool
prefix string
}

func NewDiscord(session *discordgo.Session) *Discord {
Expand All @@ -32,11 +28,10 @@ func NewDiscord(session *discordgo.Session) *Discord {
}

return &Discord{
Session: session,
Message: nil,
IsInstanceActive: true,
prefix: config.DiscordCommandPrefix,
rateLimitDuration: time.Minute * 10,
Session: session,
Message: nil,
IsInstanceActive: true,
prefix: config.DiscordCommandPrefix,
}
}

Expand Down Expand Up @@ -129,9 +124,10 @@ func (d *Discord) splitCommandFromParameter(content, commandPrefix string) (stri
return "", "", fmt.Errorf("command prefix not found")
}

commandPrefix = strings.ToLower(commandPrefix)
prefixLowercase := strings.ToLower(commandPrefix)
contentLowercase := strings.ToLower(content)

if !strings.HasPrefix(content, commandPrefix) {
if !strings.HasPrefix(contentLowercase, prefixLowercase) {
return "", "", nil // fmt.Errorf("pattern not found")
}

Expand All @@ -153,47 +149,18 @@ func (d *Discord) splitCommandFromParameter(content, commandPrefix string) (stri
return command, parameter, nil
}

func getCanonicalCommand(alias string, commandAliases [][]string) string {
alias = strings.ToLower(alias)
func getCanonicalCommand(command string, commandAliases [][]string) string {
lowerCommand := strings.ToLower(command)
for _, aliases := range commandAliases {
for _, command := range aliases {
if strings.ToLower(command) == alias {
for _, alias := range aliases {
if strings.ToLower(alias) == lowerCommand {
return strings.ToLower(aliases[0])
}
}
}
return ""
}

func (d *Discord) changeAvatar() {
s := d.Session

if time.Since(d.lastChangeAvatarTime) < d.rateLimitDuration {
//slog.Info("Rate-limited. Skipping changeAvatar.")
return
}

imgPath, err := utils.GetWeightedRandomImagePath("./assets/avatars")
if err != nil {
slog.Errorf("Error getting avatar path: %v", err)
return
}

avatar, err := utils.ReadFileToBase64(imgPath)
if err != nil {
fmt.Printf("Error preparing avatar: %v\n", err)
return
}

_, err = s.UserUpdate("", avatar)
if err != nil {
slog.Errorf("Error setting the avatar: %v", err)
return
}

d.lastChangeAvatarTime = time.Now()
}

func (d *Discord) findUserVoiceState(userID string, voiceStates []*discordgo.VoiceState) (*discordgo.VoiceState, bool) {
for _, vs := range voiceStates {
if vs.UserID == userID {
Expand Down
1 change: 0 additions & 1 deletion mods/music/discord/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
func (d *Discord) handleHistoryCommand(param string) {
s := d.Session
m := d.Message
d.changeAvatar()

sortBy, title := "last_played", " — most recent"
switch param {
Expand Down
1 change: 0 additions & 1 deletion mods/music/discord/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package discord
func (d *Discord) handleShowQueueCommand() {
s := d.Session
m := d.Message
d.changeAvatar()

playlist := d.Player.GetSongQueue()
pleaseWaitMsg := d.sendMessageEmbed("Please wait...")
Expand Down
1 change: 0 additions & 1 deletion mods/music/discord/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

func (d *Discord) handlePauseCommand() {
d.changeAvatar()

if d.Player.GetCurrentStatus() != player.StatusPlaying {
slog.Info("Ignoring pause command because player is not playing", d.Player.GetCurrentStatus().String())
Expand Down
1 change: 0 additions & 1 deletion mods/music/discord/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
func (d *Discord) handlePlayCommand(param string, enqueueOnly bool) {
s := d.Session
m := d.Message
d.changeAvatar()

if param == "" {
return
Expand Down
2 changes: 0 additions & 2 deletions mods/music/discord/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
)

func (d *Discord) handleResumeCommand() {
d.changeAvatar()

if d.Player.GetCurrentStatus() != player.StatusPaused && d.Player.GetCurrentStatus() != player.StatusResting {
slog.Info("Ignoring resume command because player is not paused or resting", d.Player.GetCurrentStatus().String())
}
Expand Down
2 changes: 0 additions & 2 deletions mods/music/discord/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
)

func (d *Discord) handleSkipCommand() {
d.changeAvatar()

skipMsg := d.sendMessageEmbed("⏩ " + "Skipping")

err := d.Player.Skip()
Expand Down

0 comments on commit 7c7bbb7

Please sign in to comment.