mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-01-18 10:36:44 +01:00
Added ListSubscriber, Unpublish and Retrieve commands
This commit is contained in:
parent
ef2866e89b
commit
ff91955361
@ -2,9 +2,12 @@
|
||||
* @Author: Amalvy Arthur
|
||||
*/
|
||||
|
||||
// This module use a property named "subscribed_chats" in the shared Users structure.
|
||||
// This module uses 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)
|
||||
|
||||
// This module uses a property named "saved_messages" in the shared Chats structure
|
||||
// It consists of a list of string
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
@ -66,41 +69,118 @@ func Unsubscribe(m *tb.Message) {
|
||||
shared.Users.Set(m.Sender.Username, "subscribed_chats", strings.Join(splittedChats, ":"))
|
||||
}
|
||||
|
||||
// List all subscribers of the current chat
|
||||
// Command syntax : /listsubscribers
|
||||
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
|
||||
// }
|
||||
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
|
||||
}
|
||||
subscribers := m.Chat.Title + " subscribers : \n\n"
|
||||
for _, subscriber := range getSubscribers(m.Chat.ID) {
|
||||
subscribers = subscribers + "- " + subscriber + "\n"
|
||||
}
|
||||
shared.Bot.Send(m.Chat, subscribers)
|
||||
}
|
||||
|
||||
// Publish a message from the current chat
|
||||
// Command syntax (while repying to a message) : /publish
|
||||
func Publish(m *tb.Message) {
|
||||
if m.ReplyTo == nil {
|
||||
shared.Bot.Send(m.Chat, "Please reply to a message to save it")
|
||||
shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le publier")
|
||||
return
|
||||
}
|
||||
|
||||
defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text)
|
||||
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages")
|
||||
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages")
|
||||
if !exists {
|
||||
messageList := []string{m.ReplyTo.Text}
|
||||
shared.ChatData.Set(m.Chat.ID, "saved_messages", messageList)
|
||||
shared.ChatData.Set(m.Chat.ID, "published_messages", messageList)
|
||||
return
|
||||
}
|
||||
shared.ChatData.Set(m.Chat.ID, "saved_messages",
|
||||
shared.ChatData.Set(m.Chat.ID, "published_messages",
|
||||
append(savedMessages.([]string), m.ReplyTo.Text))
|
||||
}
|
||||
|
||||
// Remove a message from published messages in the current chat
|
||||
// Command syntax : /unpublish [publication ID]
|
||||
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é")
|
||||
parsedCommand := strings.Split(m.Text, " ")
|
||||
if len(parsedCommand) < 2 {
|
||||
shared.Bot.Send(m.Chat, "syntaxe : /unpublish [publication ID]")
|
||||
return
|
||||
}
|
||||
savedMessages, _ := shared.ChatData.Get(m.Chat.ID, "saved_messages")
|
||||
for _, message := range savedMessages.([]string) {
|
||||
shared.Bot.Send(m.Chat, "message : "+message)
|
||||
index, err := strconv.Atoi(parsedCommand[1])
|
||||
if err != nil {
|
||||
shared.Bot.Send(m.Chat, "ID de publication invalide")
|
||||
return
|
||||
}
|
||||
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages")
|
||||
if !exists || len(savedMessages.([]string)) == 0 {
|
||||
shared.Bot.Send(m.Chat, "Aucun message publié")
|
||||
return
|
||||
}
|
||||
if len(savedMessages.([]string)) <= index || index < 0 {
|
||||
shared.Bot.Send(m.Chat, "Aucun message avec cet ID de publication n'existe")
|
||||
return
|
||||
}
|
||||
savedMessages = append(savedMessages.([]string)[:index], savedMessages.([]string)[index+1:]...)
|
||||
shared.ChatData.Set(m.Chat.ID, "published_messages", savedMessages.([]string))
|
||||
shared.Bot.Send(m.Chat, "Message supprimé des publication")
|
||||
}
|
||||
|
||||
// If performed in MP : retrieve all messages from all subscribed sources for the user
|
||||
// If performed in Group Chat : retrieved all published messages for this chat
|
||||
// Command syntax : /retrieve
|
||||
func Retrieve(m *tb.Message) {
|
||||
if m.Chat.Type == "group" || m.Chat.Type == "supergroup" {
|
||||
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages")
|
||||
if !exists || len(savedMessages.([]string)) == 0 {
|
||||
shared.Bot.Send(m.Chat, "Aucun message publié")
|
||||
return
|
||||
}
|
||||
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
|
||||
for index, message := range savedMessages.([]string) {
|
||||
shared.Bot.Send(m.Chat, strconv.Itoa(index)+" : "+message)
|
||||
}
|
||||
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
|
||||
return
|
||||
}
|
||||
if m.Chat.Type == "private" {
|
||||
// get subscribed sources
|
||||
// get messages from those sources
|
||||
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
|
||||
if !exists || len(userSubscribedChats) == 0 {
|
||||
shared.Bot.Send(m.Chat, "Aucun abonnement")
|
||||
return
|
||||
}
|
||||
for _, chat := range strings.Split(userSubscribedChats, ":") {
|
||||
chatID, _ := strconv.ParseInt(chat, 10, 64)
|
||||
savedMessages, _ := shared.ChatData.Get(chatID, "published_messages")
|
||||
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
|
||||
for index, message := range savedMessages.([]string) {
|
||||
shared.Bot.Send(m.Chat, strconv.Itoa(index)+" : "+message)
|
||||
}
|
||||
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
|
||||
}
|
||||
return
|
||||
}
|
||||
shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat")
|
||||
}
|
||||
|
||||
// Get all users subscribed to the provided channel
|
||||
func getSubscribers(chatID int64) []string {
|
||||
var subscribers []string
|
||||
for _, username := range shared.Users.GetUsernames() {
|
||||
userSubscribedChats, exists := shared.Users.Get(username, "subscribed_chats")
|
||||
if exists {
|
||||
splittedChats := strings.Split(userSubscribedChats, ":")
|
||||
for _, splittedChatID := range splittedChats {
|
||||
if splittedChatID == strconv.FormatInt(chatID, 10) {
|
||||
subscribers = append(subscribers, username)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return subscribers
|
||||
}
|
||||
|
@ -61,6 +61,17 @@ func (u users) Set(username string, key, data string) {
|
||||
go uf.write()
|
||||
}
|
||||
|
||||
// Get all known usernames
|
||||
func (u users) GetUsernames() []string {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
var usernames []string
|
||||
for username, _ := range u.data {
|
||||
usernames = append(usernames, username)
|
||||
}
|
||||
return usernames
|
||||
}
|
||||
|
||||
func (u usersFile) read() {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
|
Loading…
Reference in New Issue
Block a user