Skip to content

Commit

Permalink
mongoDb to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
AshokShau committed Jul 25, 2024
1 parent 6d594e9 commit 13bec7c
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 251 deletions.
2 changes: 0 additions & 2 deletions FallenSub/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var (
OwnerId int64
LoggerId int64
DatabaseURI string
DbName string
)

var (
Expand All @@ -24,7 +23,6 @@ func init() {
_ = godotenv.Load()

DatabaseURI = os.Getenv("DB_URI")
DbName = os.Getenv("DB_NAME")
Token = os.Getenv("TOKEN")
OwnerId = toInt64(os.Getenv("OWNER_ID"))
LoggerId = toInt64(os.Getenv("LOGGER_ID"))
Expand Down
36 changes: 0 additions & 36 deletions FallenSub/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,4 @@ func setDefaults() {
OwnerId = 5938660179
}

if DbName == "" {
DbName = "ForceSub"
}

}

// FindInInt64Slice Find takes a slice and looks for an element in it. If found it will
// return true, otherwise it will return a bool of false.
func FindInInt64Slice(slice []int64, val int64) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}

// FindInStringSlice Find takes a slice and looks for an element in it. If found it will
// return true, otherwise it will return a bool of false.
func FindInStringSlice(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}

// RemoveFromInt64Slice Find takes a slice and looks for an element in it. If found it will
func RemoveFromInt64Slice(s []int64, r int64) []int64 {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}
124 changes: 108 additions & 16 deletions FallenSub/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,131 @@ package db

import (
"context"
"encoding/json"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/go-redis/redis/v8"
"log"
"strconv"
)

var (
tdCtx = context.TODO()

fSubCell *mongo.Collection
rdb *redis.Client
ctx = context.Background()
)

func init() {
mongoClient, err := mongo.Connect(tdCtx, options.Client().ApplyURI(config.DatabaseURI))

opt, err := redis.ParseURL(config.DatabaseURI)
if err != nil {
log.Fatalf("[Database][Connect]: %v", err)
}
rdb = redis.NewClient(opt)

if _, err = rdb.Ping(ctx).Result(); err != nil {
log.Fatalf("[Database][Ping]: %v", err)
}

log.Println("[Database][Connect]: Connected to Redis")
}

type FSub struct {
ChatId int64 `json:"chatId"`
ForceSub bool `json:"forceSub"`
ForceSubChannel int64 `json:"forceSubChannel"`
FSubMuted []int64 `json:"mutedUsers"`
}

func redisKey(chatId int64) string {
return "forceSub:" + strconv.FormatInt(chatId, 10)
}

// 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 {
return &FSub{ChatId: chatId}, nil
} else if err != nil {
return nil, err
}
var fSub FSub
if err := json.Unmarshal([]byte(data), &fSub); err != nil {
return nil, err
}
return &fSub, nil
}

// SetFSubSetting sets the FSub setting for a chat
func SetFSubSetting(fSub *FSub) error {
data, err := json.Marshal(fSub)
if err != nil {
return err
}
return rdb.Set(ctx, redisKey(fSub.ChatId), string(data), 0).Err()
}

fSubCell = mongoClient.Database(config.DbName).Collection("forceSub")
// UpdateMuted adds a user to the muted list
func UpdateMuted(chatId int64, userid int64) error {
fSub, err := GetFSubSetting(chatId)
if err != nil {
return err
}
if !findInInt64Slice(fSub.FSubMuted, userid) {
fSub.FSubMuted = append(fSub.FSubMuted, userid)
return SetFSubSetting(fSub)
}
return nil
}

// RemoveMuted removes a user from the muted list
func RemoveMuted(chatId int64, userid int64) error {
fSub, err := GetFSubSetting(chatId)
if err != nil {
return err
}
for i, v := range fSub.FSubMuted {
if v == userid {
fSub.FSubMuted = append(fSub.FSubMuted[:i], fSub.FSubMuted[i+1:]...)
return SetFSubSetting(fSub)
}
}
return nil
}

// updateOne func to update one document
func updateOne(collection *mongo.Collection, filter bson.M, data interface{}) (err error) {
_, err = collection.UpdateOne(tdCtx, filter, bson.M{"$set": data}, options.Update().SetUpsert(true))
// SetFSub sets the FSub setting for a chat
func SetFSub(chatId int64, fSub bool) error {
fSubUpdate, err := GetFSubSetting(chatId)
if err != nil {
config.ErrorLog.Printf("[Database][updateOne]: %v", err)
return err
}
return
fSubUpdate.ForceSub = fSub
return SetFSubSetting(fSubUpdate)
}

// findOne func to find one document
func findOne(collection *mongo.Collection, filter bson.M) (res *mongo.SingleResult) {
return collection.FindOne(tdCtx, filter)
// SetFSubChannel sets the FSub setting for a chat
func SetFSubChannel(chatId int64, channel int64) error {
fSubUpdate, err := GetFSubSetting(chatId)
if err != nil {
return err
}
fSubUpdate.ForceSubChannel = channel
return SetFSubSetting(fSubUpdate)
}

// IsMuted checks if a user is muted
func IsMuted(chatId int64, userId int64) (bool, error) {
fSub, err := GetFSubSetting(chatId)
if err != nil {
return false, err
}
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
}
86 changes: 0 additions & 86 deletions FallenSub/db/fsub_db.go

This file was deleted.

Loading

0 comments on commit 13bec7c

Please sign in to comment.