forked from git.klmp200.net/ALFRED
Begin commands package
This commit is contained in:
parent
8a9592ad27
commit
f7f3308372
4 changed files with 53 additions and 15 deletions
14
shared/bot.go
Normal file
14
shared/bot.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-24 11:56:47
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-24 11:58:34
|
||||
*/
|
||||
|
||||
package shared
|
||||
|
||||
import (
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
)
|
||||
|
||||
var Bot *tb.Bot
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-24 01:27:11
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-24 02:09:53
|
||||
* @Last Modified time: 2018-07-24 11:51:52
|
||||
*/
|
||||
|
||||
package shared
|
||||
|
@ -19,6 +19,7 @@ type history struct {
|
|||
|
||||
var History history
|
||||
|
||||
// Init a chat history of a given size
|
||||
func InitHistory(size int) {
|
||||
History = history{}
|
||||
History.mutex.Lock()
|
||||
|
@ -27,12 +28,14 @@ func InitHistory(size int) {
|
|||
History.data = make(map[int64][]string)
|
||||
}
|
||||
|
||||
// Get the number of messages saved in the history
|
||||
func (h history) Size() int {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
return h.size
|
||||
}
|
||||
|
||||
// Get a selected message in a chat history
|
||||
func (h history) Message(chatID int64, n int) string {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
|
@ -43,6 +46,7 @@ func (h history) Message(chatID int64, n int) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// Append a message to a given chat
|
||||
func (h history) AddMessage(chatID int64, m string) {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
|
@ -52,6 +56,7 @@ func (h history) AddMessage(chatID int64, m string) {
|
|||
h.append(chatID, m)
|
||||
}
|
||||
|
||||
// Get the last message of a given chat
|
||||
func (h history) LastMessage(chatID int64) string {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
|
@ -61,6 +66,7 @@ func (h history) LastMessage(chatID int64) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// Get a copy of a given chat history
|
||||
func (h history) ChatHistory(chatID int64) []string {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
|
@ -73,6 +79,7 @@ func (h history) ChatHistory(chatID int64) []string {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Add a message at the end of a chat and move everithyng up
|
||||
// Assert that the slice exists and mutex already locked
|
||||
func (h history) append(chatID int64, m string) {
|
||||
c := make([]string, h.size-1, h.size-1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue