-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new Cmds : /getChat /getUsers /getAllBots Todo: broadCast & ..
- Loading branch information
Showing
7 changed files
with
230 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package db | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/go-redis/redis/v8" | ||
"strconv" | ||
) | ||
|
||
type State struct { | ||
BotID int64 `json:"botId"` | ||
Chats []int64 `json:"chats"` | ||
Users []int64 `json:"users"` | ||
} | ||
|
||
func stateKey(botId int64) string { | ||
return "stats:" + strconv.FormatInt(botId, 10) | ||
} | ||
|
||
func GetStats(botId int64) (*State, error) { | ||
data, err := rdb.Get(ctx, stateKey(botId)).Result() | ||
if errors.Is(err, redis.Nil) { | ||
return &State{BotID: botId}, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
var state State | ||
if err = json.Unmarshal([]byte(data), &state); err != nil { | ||
return nil, err | ||
} | ||
return &state, nil | ||
} | ||
|
||
// setStats sets the stats for a bot | ||
func setStats(state *State) error { | ||
data, err := json.Marshal(state) | ||
if err != nil { | ||
return err | ||
} | ||
return rdb.Set(ctx, stateKey(state.BotID), string(data), 0).Err() | ||
} | ||
|
||
// AddChat adds a chat to the stats | ||
func AddChat(botId int64, chatId int64) error { | ||
state, err := GetStats(botId) | ||
if err != nil { | ||
return err | ||
} | ||
if !findInInt64Slice(state.Chats, chatId) { | ||
state.Chats = append(state.Chats, chatId) | ||
return setStats(state) | ||
} | ||
return nil | ||
} | ||
|
||
// AddUser adds a user to the stats | ||
func AddUser(botId int64, userId int64) error { | ||
state, err := GetStats(botId) | ||
if err != nil { | ||
return err | ||
} | ||
if !findInInt64Slice(state.Users, userId) { | ||
state.Users = append(state.Users, userId) | ||
return setStats(state) | ||
} | ||
return nil | ||
} | ||
|
||
// AllChats gets all chats in the stats | ||
func AllChats(botId int64) ([]int64, error) { | ||
state, err := GetStats(botId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return state.Chats, nil | ||
} | ||
|
||
// AllUsers gets all users in the stats | ||
func AllUsers(botId int64) ([]int64, error) { | ||
state, err := GetStats(botId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return state.Users, nil | ||
} | ||
|
||
// GetAllBots gets all bots in the stats | ||
func GetAllBots() ([]int64, error) { | ||
keys, _, err := rdb.Scan(ctx, 0, "stats:*", 0).Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var bots []int64 | ||
for _, key := range keys { | ||
botId, err := strconv.ParseInt(key[6:], 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bots = append(bots, botId) | ||
} | ||
return bots, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package modules | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config" | ||
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/db" | ||
"github.com/PaulSonOfLars/gotgbot/v2" | ||
"github.com/PaulSonOfLars/gotgbot/v2/ext" | ||
) | ||
|
||
// getChats is the handler for the /getChats command | ||
func getChats(b *gotgbot.Bot, ctx *ext.Context) error { | ||
msg := ctx.EffectiveMessage | ||
if msg.From.Id != config.OwnerId { | ||
_, _ = msg.Reply(b, "You are not allowed to use this command.", nil) | ||
return ext.EndGroups | ||
} | ||
|
||
// Get all chats | ||
chats, err := db.AllChats(b.Id) | ||
if err != nil { | ||
return logError("Error getting chats", err) | ||
} | ||
|
||
// Create a message with the list of chats | ||
var chatList string | ||
for _, chat := range chats { | ||
chatList += fmt.Sprintf("» %d\n", chat) | ||
} | ||
|
||
// Send the list of chats as a message | ||
_, err = b.SendMessage(msg.Chat.Id, fmt.Sprintf("Total chats: %d\n\n%s", len(chats), chatList), nil) | ||
if err != nil { | ||
return logError("Error sending message", err) | ||
} | ||
|
||
return ext.EndGroups | ||
} | ||
|
||
// getUsers is the handler for the /getUsers command | ||
func getUsers(b *gotgbot.Bot, ctx *ext.Context) error { | ||
msg := ctx.EffectiveMessage | ||
if msg.From.Id != config.OwnerId { | ||
_, _ = msg.Reply(b, "You are not allowed to use this command.", nil) | ||
return ext.EndGroups | ||
} | ||
|
||
// Get all users | ||
users, err := db.AllUsers(b.Id) | ||
if err != nil { | ||
return logError("Error getting users", err) | ||
} | ||
|
||
// Create a message with the list of users | ||
var userList string | ||
for _, user := range users { | ||
userList += fmt.Sprintf("» %d\n", user) | ||
} | ||
|
||
// Send the list of users as a message | ||
_, err = b.SendMessage(msg.Chat.Id, fmt.Sprintf("Total users: %d\n\n%s", len(users), userList), nil) | ||
if err != nil { | ||
return logError("Error sending message", err) | ||
} | ||
|
||
return ext.EndGroups | ||
} | ||
|
||
// getAllBots is the handler for the /getAllBots command | ||
func getAllBots(b *gotgbot.Bot, ctx *ext.Context) error { | ||
msg := ctx.EffectiveMessage | ||
if msg.From.Id != config.OwnerId { | ||
_, _ = msg.Reply(b, "You are not allowed to use this command.", nil) | ||
return ext.EndGroups | ||
} | ||
|
||
// Get all bots | ||
bots, err := db.GetAllBots() | ||
if err != nil { | ||
return logError("Error getting bots", err) | ||
} | ||
|
||
// Create a message with the list of bots | ||
var botList string | ||
for _, bot := range bots { | ||
botList += fmt.Sprintf("» %d\n", bot) | ||
} | ||
|
||
// Send the list of bots as a message | ||
_, err = b.SendMessage(msg.Chat.Id, fmt.Sprintf("Total bots: %d\n\n%s", len(bots), botList), nil) | ||
if err != nil { | ||
return logError("Error sending message", err) | ||
} | ||
|
||
return ext.EndGroups | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters