diff --git a/chatcommands.go b/chatcommands.go index 40fa62a..f18bbed 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -438,26 +438,7 @@ var commands = &CommandControl{ common.CNReloadEmotes.String(): Command{ HelpText: "Reload the emotes on the server.", Function: func(cl *Client, args []string) (string, error) { - go func() { - cl.SendServerMessage("Reloading emotes") - err := loadEmotes() - if err != nil { - common.LogErrorf("Unbale to reload emotes: %s\n", err) - //return "", err - - cl.SendChatData(common.NewChatMessage("", "", - err.Error(), - common.CmdlUser, common.MsgCommandResponse)) - return - } - - cl.belongsTo.AddChatMsg(common.NewChatHiddenMessage(common.CdEmote, common.Emotes)) - cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes") - - num := len(common.Emotes) - common.LogInfof("Loaded %d emotes\n", num) - cl.belongsTo.AddModNotice(fmt.Sprintf("%s reloaded %d emotes.", cl.name, num)) - }() + go commandReloadEmotes(cl) return "Reloading emotes...", nil }, }, @@ -558,6 +539,8 @@ var commands = &CommandControl{ } cl.belongsTo.AddModNotice(cl.name + " has added emotes from the following channels: " + strings.Join(args, ", ")) + + commandReloadEmotes(cl) }() return "Emote download initiated for the following channels: " + strings.Join(args, ", "), nil }, @@ -633,3 +616,24 @@ func getHelp(lvl common.CommandLevel) map[string]string { } return helptext } + +func commandReloadEmotes(cl *Client) { + cl.SendServerMessage("Reloading emotes") + err := loadEmotes() + if err != nil { + common.LogErrorf("Unbale to reload emotes: %s\n", err) + //return "", err + + cl.SendChatData(common.NewChatMessage("", "", + err.Error(), + common.CmdlUser, common.MsgCommandResponse)) + return + } + + cl.belongsTo.AddChatMsg(common.NewChatHiddenMessage(common.CdEmote, common.Emotes)) + cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes") + + num := len(common.Emotes) + common.LogInfof("Loaded %d emotes\n", num) + cl.belongsTo.AddModNotice(fmt.Sprintf("%s reloaded %d emotes.", cl.name, num)) +} diff --git a/common/emotes.go b/common/emotes.go index fd49224..5f1d3ec 100644 --- a/common/emotes.go +++ b/common/emotes.go @@ -14,10 +14,14 @@ var Emotes EmotesMap var reStripStatic = regexp.MustCompile(`^(\\|/)?static`) func init() { - Emotes = map[string]string{} + Emotes = NewEmotesMap() } -func (em EmotesMap) Add(fullpath string) { +func NewEmotesMap() EmotesMap { + return map[string]string{} +} + +func (em EmotesMap) Add(fullpath string) EmotesMap { fullpath = reStripStatic.ReplaceAllLiteralString(fullpath, "") base := filepath.Base(fullpath) @@ -35,8 +39,9 @@ func (em EmotesMap) Add(fullpath string) { code = fmt.Sprintf("%s-%d", code, num) } - Emotes[code] = fullpath - fmt.Printf("Added emote %s at path %q\n", code, fullpath) + em[code] = fullpath + //fmt.Printf("Added emote %s at path %q\n", code, fullpath) + return em } func EmoteToHtml(file, title string) string { diff --git a/emotes.go b/emotes.go index 34e80df..d8fd00b 100644 --- a/emotes.go +++ b/emotes.go @@ -29,18 +29,20 @@ type EmoteInfo struct { func loadEmotes() error { //fmt.Println(processEmoteDir(emoteDir)) - err := processEmoteDir(emoteDir) + newEmotes, err := processEmoteDir(emoteDir) if err != nil { return err } + common.Emotes = newEmotes + return nil } -func processEmoteDir(path string) error { +func processEmoteDir(path string) (common.EmotesMap, error) { dirInfo, err := ioutil.ReadDir(path) if err != nil { - return errors.Wrap(err, "could not open emoteDir:") + return nil, errors.Wrap(err, "could not open emoteDir:") } subDirs := []string{} @@ -53,54 +55,62 @@ func processEmoteDir(path string) error { } } + em := common.NewEmotesMap() // Find top level emotes - err = findEmotes(path) + em, err = findEmotes(path, em) if err != nil { - return errors.Wrap(err, "could not findEmotes() in top level directory:") + return nil, errors.Wrap(err, "could not findEmotes() in top level directory:") } // Get second level subdirs (eg, "twitch", "zorchenhimer", etc) for _, dir := range subDirs { subd, err := ioutil.ReadDir(filepath.Join(path, dir)) if err != nil { + fmt.Printf("Error reading dir %q: %v\n", subd, err) continue } for _, d := range subd { if d.IsDir() { //emotes = append(emotes, findEmotes(filepath.Join(path, dir, d.Name()))...) - findEmotes(filepath.Join(path, dir, d.Name())) + p := filepath.Join(path, dir, d.Name()) + em, err = findEmotes(p, em) + if err != nil { + fmt.Printf("Error finding emotes in %q: %v\n", p, err) + } } } } - return nil + fmt.Printf("processEmoteDir: %d\n", len(em)) + return em, nil } -func findEmotes(dir string) error { +func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) { + //em := NewEmotesMap() + fmt.Printf("finding emotes in %q\n", dir) emotePNGs, err := filepath.Glob(filepath.Join(dir, "*.png")) if err != nil { - //return 0, fmt.Errorf("unable to glob emote directory: %s\n", err) - return nil + return em, fmt.Errorf("unable to glob emote directory: %s\n", err) } fmt.Printf("%d emotePNGs\n", len(emotePNGs)) emoteGIFs, err := filepath.Glob(filepath.Join(dir, "*.gif")) if err != nil { - return errors.Wrap(err, "unable to glob emote directory:") + return em, errors.Wrap(err, "unable to glob emote directory:") } fmt.Printf("%d emoteGIFs\n", len(emoteGIFs)) for _, file := range emotePNGs { - common.Emotes.Add(file) + em = em.Add(file) //emotes = append(emotes, common.Emote{FullPath: dir, Code: file}) } for _, file := range emoteGIFs { - common.Emotes.Add(file) + em = em.Add(file) } - return nil + return em, nil } func getEmotes(names []string) error {