Skip to content

Commit

Permalink
Added new func (Owner Only)
Browse files Browse the repository at this point in the history
new Cmds :
/getChat
/getUsers
/getAllBots

Todo: broadCast & ..
  • Loading branch information
AshokShau committed Jul 28, 2024
1 parent 054a9d2 commit 694b694
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 12 deletions.
102 changes: 102 additions & 0 deletions FallenSub/db/chats.go
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
}
25 changes: 13 additions & 12 deletions FallenSub/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"
"encoding/json"
"errors"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config"
"github.com/go-redis/redis/v8"
"log"
Expand Down Expand Up @@ -40,16 +41,26 @@ func redisKey(chatId int64) string {
return "forceSub:" + strconv.FormatInt(chatId, 10)
}

// findInInt64Slice checks if a value is in a slice or not
func findInInt64Slice(slice []int64, val int64) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}

// GetFSubSetting gets the FSub setting for a chat
func GetFSubSetting(chatId int64) (*FSub, error) {
data, err := rdb.Get(ctx, redisKey(chatId)).Result()
if err == redis.Nil {
if errors.Is(err, redis.Nil) {
return &FSub{ChatId: chatId}, nil
} else if err != nil {
return nil, err
}
var fSub FSub
if err := json.Unmarshal([]byte(data), &fSub); err != nil {
if err = json.Unmarshal([]byte(data), &fSub); err != nil {
return nil, err
}
return &fSub, nil
Expand Down Expand Up @@ -120,13 +131,3 @@ func IsMuted(chatId int64, userId int64) (bool, error) {
}
return findInInt64Slice(fSub.FSubMuted, userId), nil
}

// findInInt64Slice checks if a value is in a slice or not
func findInInt64Slice(slice []int64, val int64) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
5 changes: 5 additions & 0 deletions FallenSub/modules/fSub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func setFSub(b *gotgbot.Bot, ctx *ext.Context) error {
return ext.EndGroups
}

go func() {
_ = db.AddChat(b.Id, ctx.EffectiveChat.Id)
}()

// If the message is a reply to a forwarded message, handle it
if repliedMsg := ctx.EffectiveMessage.ReplyToMessage; repliedMsg != nil && repliedMsg.ForwardOrigin != nil {
return handleForwardedMessage(ctx, b, repliedMsg)
}
Expand Down
3 changes: 3 additions & 0 deletions FallenSub/modules/loadModules.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func LoadModules(d *ext.Dispatcher) {
d.AddHandler(handlers.NewCommand("start", start))
d.AddHandler(handlers.NewCommand("ping", ping))
d.AddHandler(handlers.NewCommand("fsub", setFSub))
d.AddHandler(handlers.NewCommand("getChats", getChats))
d.AddHandler(handlers.NewCommand("getUsers", getUsers))
d.AddHandler(handlers.NewCommand("getAllBots", getAllBots))
d.AddHandlerToGroup(handlers.NewMessage(message.All, fSubWatcher), 0)
d.AddHandler(handlers.NewCallback(callbackquery.Prefix("unmuteMe_"), unMuteMe))
}
6 changes: 6 additions & 0 deletions FallenSub/modules/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package modules

import (
"fmt"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/db"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"time"
)

// start is the handler for the /start command
func start(b *gotgbot.Bot, ctx *ext.Context) error {
if ctx.EffectiveChat.Type == gotgbot.ChatTypePrivate {
_ = db.AddUser(b.Id, ctx.EffectiveMessage.From.Id)
}

_ = db.AddChat(b.Id, ctx.EffectiveChat.Id)
text := fmt.Sprintf("Hello, %s!\n\nI am a bot that can help you manage your group by forcing users to join a channel before they can send messages in the group.\n\nTo get started, add me to your group and make me an admin with ban users permission. Then, set the channel that you want users to join using /fsub command.\n\nFor more information, click the button below.", ctx.EffectiveMessage.From.FirstName)
button := gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
Expand Down
96 changes: 96 additions & 0 deletions FallenSub/modules/stats.go
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
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ source /etc/profile.d/golang_path.sh</code></pre>
<li><code>/fsub on</code> - Enable force subscription mode.</li>
<li><code>/fsub off</code> - Disable force subscription mode.</li>
<li><code>/fsub</code> - Get the current force subscription status.</li>

<h4>Owner Only Commands</h4>
<li><code>/getChats</code> - Get all chats.</li>
<li><code>/getUsers</code> - Get all users who started in private.</li>
<li><code>/getAllBots</code> - Get all bots running on webhook.</li>
</ul>
</section>

Expand Down

0 comments on commit 694b694

Please sign in to comment.