Begin commands package
the build was successful Details

This commit is contained in:
Antoine Bartuccio 2018-07-24 11:59:36 +02:00
parent 8a9592ad27
commit f7f3308372
Signed by: klmp200
GPG Key ID: E7245548C53F904B
4 changed files with 53 additions and 15 deletions

View File

@ -2,17 +2,17 @@
* @Author: Bartuccio Antoine * @Author: Bartuccio Antoine
* @Date: 2018-07-23 15:24:22 * @Date: 2018-07-23 15:24:22
* @Last Modified by: klmp200 * @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 03:05:51 * @Last Modified time: 2018-07-24 11:58:31
*/ */
package main package main
import ( import (
"./commands"
"./settings" "./settings"
"./shared" "./shared"
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
"log" "log"
"strings"
"time" "time"
) )
@ -33,6 +33,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
return return
} }
shared.Bot = b
b.Handle("/hello", func(m *tb.Message) { b.Handle("/hello", func(m *tb.Message) {
b.Send(m.Chat, "Bonjour "+m.Sender.Username) b.Send(m.Chat, "Bonjour "+m.Sender.Username)
@ -42,18 +43,7 @@ func main() {
shared.History.AddMessage(m.Chat.ID, m.Text) shared.History.AddMessage(m.Chat.ID, m.Text)
}) })
b.Handle("/sponge", func(m *tb.Message) { b.Handle("/sponge", commands.Sponge)
message := ""
for i, char := range shared.History.LastMessage(m.Chat.ID) {
if i%2 == 0 {
message += strings.ToLower(string(char))
} else {
message += strings.ToUpper(string(char))
}
}
b.Send(m.Chat, message)
})
b.Handle("/git", func(m *tb.Message) { b.Handle("/git", func(m *tb.Message) {
b.Send(m.Chat, "Mon code est accessible librement à l'adresse https://git.klmp200.net/ALFRED/ALFRED. Venez contribuer :)") b.Send(m.Chat, "Mon code est accessible librement à l'adresse https://git.klmp200.net/ALFRED/ALFRED. Venez contribuer :)")

27
commands/sponge.go Normal file
View File

@ -0,0 +1,27 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 11:52:11
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 11:58:42
*/
package commands
import (
"../shared"
tb "gopkg.in/tucnak/telebot.v2"
"strings"
)
func Sponge(m *tb.Message) {
message := ""
for i, char := range shared.History.LastMessage(m.Chat.ID) {
if i%2 == 0 {
message += strings.ToLower(string(char))
} else {
message += strings.ToUpper(string(char))
}
}
shared.Bot.Send(m.Chat, message)
}

14
shared/bot.go Normal file
View 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

View File

@ -2,7 +2,7 @@
* @Author: Bartuccio Antoine * @Author: Bartuccio Antoine
* @Date: 2018-07-24 01:27:11 * @Date: 2018-07-24 01:27:11
* @Last Modified by: klmp200 * @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 02:09:53 * @Last Modified time: 2018-07-24 11:51:52
*/ */
package shared package shared
@ -19,6 +19,7 @@ type history struct {
var History history var History history
// Init a chat history of a given size
func InitHistory(size int) { func InitHistory(size int) {
History = history{} History = history{}
History.mutex.Lock() History.mutex.Lock()
@ -27,12 +28,14 @@ func InitHistory(size int) {
History.data = make(map[int64][]string) History.data = make(map[int64][]string)
} }
// Get the number of messages saved in the history
func (h history) Size() int { func (h history) Size() int {
h.mutex.Lock() h.mutex.Lock()
defer h.mutex.Unlock() defer h.mutex.Unlock()
return h.size return h.size
} }
// Get a selected message in a chat history
func (h history) Message(chatID int64, n int) string { func (h history) Message(chatID int64, n int) string {
h.mutex.Lock() h.mutex.Lock()
defer h.mutex.Unlock() defer h.mutex.Unlock()
@ -43,6 +46,7 @@ func (h history) Message(chatID int64, n int) string {
return "" return ""
} }
// Append a message to a given chat
func (h history) AddMessage(chatID int64, m string) { func (h history) AddMessage(chatID int64, m string) {
h.mutex.Lock() h.mutex.Lock()
defer h.mutex.Unlock() defer h.mutex.Unlock()
@ -52,6 +56,7 @@ func (h history) AddMessage(chatID int64, m string) {
h.append(chatID, m) h.append(chatID, m)
} }
// Get the last message of a given chat
func (h history) LastMessage(chatID int64) string { func (h history) LastMessage(chatID int64) string {
h.mutex.Lock() h.mutex.Lock()
defer h.mutex.Unlock() defer h.mutex.Unlock()
@ -61,6 +66,7 @@ func (h history) LastMessage(chatID int64) string {
return "" return ""
} }
// Get a copy of a given chat history
func (h history) ChatHistory(chatID int64) []string { func (h history) ChatHistory(chatID int64) []string {
h.mutex.Lock() h.mutex.Lock()
defer h.mutex.Unlock() defer h.mutex.Unlock()
@ -73,6 +79,7 @@ func (h history) ChatHistory(chatID int64) []string {
return nil return nil
} }
// Add a message at the end of a chat and move everithyng up
// Assert that the slice exists and mutex already locked // Assert that the slice exists and mutex already locked
func (h history) append(chatID int64, m string) { func (h history) append(chatID int64, m string) {
c := make([]string, h.size-1, h.size-1) c := make([]string, h.size-1, h.size-1)