ALFRED/commands/subscribe.go

107 lines
3.4 KiB
Go

/*
* @Author: Amalvy Arthur
*/
// This module use a property named "subscribed_chats" in the shared Users structure.
// It consists of a list of chatID as a string, chatID are separated by ":" (unix PATH style)
package commands
import (
"strconv"
"strings"
"../shared"
tb "gopkg.in/tucnak/telebot.v2"
)
// Subscribe user sending the command to the current chat
// Command syntax : /subscribe
func Subscribe(m *tb.Message) {
if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat")
return
}
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists {
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10))
return
}
splittedChats := strings.Split(userSubscribedChats, ":")
for _, chatID := range splittedChats {
if chatID == strconv.FormatInt(m.Chat.ID, 10) {
shared.Bot.Send(m.Chat, "Vous êtes déjà abonné à ce chat : "+m.Chat.Title)
return
}
}
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
if len(userSubscribedChats) != 0 {
shared.Users.Set(m.Sender.Username, "subscribed_chats", userSubscribedChats+":"+strconv.FormatInt(m.Chat.ID, 10))
} else {
shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10))
}
}
// Unsubscribe user sending the command from the current chat
// Command syntax : /unsubscribe
func Unsubscribe(m *tb.Message) {
if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat")
return
}
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists || len(userSubscribedChats) == 0 {
shared.Bot.Send(m.Chat, "Vous n'êtes abonné à aucun chat")
return
}
splittedChats := strings.Split(userSubscribedChats, ":")
for i, chatID := range splittedChats {
if chatID == strconv.FormatInt(m.Chat.ID, 10) {
shared.Bot.Send(m.Chat, "désabonnement du chat : "+m.Chat.Title)
splittedChats = append(splittedChats[:i], splittedChats[i+1:]...)
break
}
}
shared.Users.Set(m.Sender.Username, "subscribed_chats", strings.Join(splittedChats, ":"))
}
func ListSubscribers(m *tb.Message) {
// if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
// shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat")
// return
// }
}
func Publish(m *tb.Message) {
if m.ReplyTo == nil {
shared.Bot.Send(m.Chat, "Please reply to a message to save it")
return
}
defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text)
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages")
if !exists {
messageList := []string{m.ReplyTo.Text}
shared.ChatData.Set(m.Chat.ID, "saved_messages", messageList)
return
}
shared.ChatData.Set(m.Chat.ID, "saved_messages",
append(savedMessages.([]string), m.ReplyTo.Text))
}
func Unpublish(m *tb.Message) {
}
func Retrieve(m *tb.Message) {
if _, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages"); !exists {
shared.Bot.Send(m.Chat, "Aucun message sauvegardé")
return
}
savedMessages, _ := shared.ChatData.Get(m.Chat.ID, "saved_messages")
for _, message := range savedMessages.([]string) {
shared.Bot.Send(m.Chat, "message : "+message)
}
}