mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-05-24 12:51:16 +02:00
Add cool quotes
This commit is contained in:
parent
0d142365d1
commit
14a3bc27d7
4 changed files with 298 additions and 5 deletions
53
commands/quote.go
Normal file
53
commands/quote.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-11-14 00:15:43
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-11-14 00:31:48
|
||||
*/
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"sync"
|
||||
|
||||
"../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{}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue