/* * @Author: Bartuccio Antoine * @Date: 2018-11-14 00:15:43 * @Last Modified by: Bartuccio Antoine * @Last Modified time: 2019-01-04 10:40:01 */ package commands import ( "encoding/json" "io/ioutil" "math/rand" "sync" "git.klmp200.net/ALFRED/ALFRED/shared" tb "gopkg.in/tucnak/telebot.v2" ) type quoteStorage struct { mutex sync.Mutex data []string } var sharedQuotes *quoteStorage // Quote display a quote on the chat func Quote(m *tb.Message) { if sharedQuotes == nil { loadQuotes("quotes.json") } sharedQuotes.mutex.Lock() defer sharedQuotes.mutex.Unlock() shared.Bot.Send(m.Chat, sharedQuotes.data[rand.Intn(len(sharedQuotes.data))]) } func loadQuotes(path string) { sharedQuotes = "eStorage{} sharedQuotes.mutex.Lock() defer sharedQuotes.mutex.Unlock() sharedQuotes.data = []string{} data, err := ioutil.ReadFile(path) if err != nil { return } if json.Unmarshal(data, &sharedQuotes.data) != nil { sharedQuotes.data = []string{} } }