fix: database filter

This commit is contained in:
Siwoo Jeon 2025-07-08 20:50:40 +09:00
parent ef7ab396b5
commit 76cacdd660
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
11 changed files with 28 additions and 49 deletions

View file

@ -4,7 +4,6 @@ import (
"context"
"git.wh64.net/muffin/goMuffin/databases"
"go.mongodb.org/mongo-driver/v2/bson"
"google.golang.org/genai"
)
@ -18,7 +17,7 @@ func GetMemory(userId string) ([]*genai.Content, error) {
memory := []*genai.Content{}
cur, err := databases.Database.Memory.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
cur, err := databases.Database.Memory.Find(context.TODO(), databases.User{UserId: userId})
if err != nil {
return memory, err
}

View file

@ -57,7 +57,7 @@ var DataLengthCommand *Command = &Command{
},
}
func getLength(ch chan chStruct, dType dataType, coll *mongo.Collection, filter bson.D) {
func getLength(ch chan chStruct, dType dataType, coll *mongo.Collection, filter any) {
defer dataLengthWg.Done()
var err error
var cur *mongo.Cursor
@ -84,7 +84,7 @@ func dataLengthRun(s *discordgo.Session, m any, username, userId string) error {
dataLengthWg.Add(5)
go getLength(ch, text, databases.Database.Texts, bson.D{{}})
go getLength(ch, muffin, databases.Database.Texts, bson.D{{Key: "persona", Value: "muffin"}})
go getLength(ch, muffin, databases.Database.Texts, databases.Text{Persona: "muffin"})
go getLength(ch, nsfw, databases.Database.Texts, bson.D{
{
Key: "persona",
@ -94,7 +94,7 @@ func dataLengthRun(s *discordgo.Session, m any, username, userId string) error {
},
})
go getLength(ch, learn, databases.Database.Learns, bson.D{{}})
go getLength(ch, userLearn, databases.Database.Learns, bson.D{{Key: "user_id", Value: userId}})
go getLength(ch, userLearn, databases.Database.Learns, databases.Learn{UserId: userId})
go func() {
dataLengthWg.Wait()

View file

@ -9,7 +9,6 @@ import (
"git.wh64.net/muffin/goMuffin/databases"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/v2/bson"
)
var DeleteLearnedDataCommand *Command = &Command{
@ -78,7 +77,7 @@ func deleteLearnedDataRun(m any, command, userId string) error {
var sections []discordgo.Section
var containers []*discordgo.Container
cur, err := databases.Database.Learns.Find(context.TODO(), bson.M{"user_id": userId, "command": command})
cur, err := databases.Database.Learns.Find(context.TODO(), databases.Learn{UserId: userId, Command: command})
if err != nil {
return err
}

View file

@ -48,19 +48,10 @@ var BlockCommand *Command = &Command{
}
_, err = databases.Database.Users.UpdateOne(context.TODO(),
bson.D{{Key: "user_id", Value: userId}},
databases.User{UserId: userId},
bson.D{{
Key: "$set",
Value: bson.D{
{
Key: "blocked",
Value: true,
},
{
Key: "blocked_reason",
Value: reason,
},
},
Value: databases.User{Blocked: true, BlockedReason: reason},
}})
if err != nil {
return err

View file

@ -40,19 +40,10 @@ var UnblockCommand *Command = &Command{
}
_, err = databases.Database.Users.UpdateOne(context.TODO(),
bson.D{{Key: "user_id", Value: userId}},
databases.User{UserId: userId},
bson.D{{
Key: "$set",
Value: bson.D{
{
Key: "blocked",
Value: false,
},
{
Key: "blocked_reason",
Value: "",
},
},
Value: databases.User{Blocked: false, BlockedReason: ""},
}})
if err != nil {
return err

View file

@ -9,7 +9,6 @@ import (
"git.wh64.net/muffin/goMuffin/databases"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/v2/bson"
)
var DeleteLearnedDataComponent *commands.Component = &commands.Component{
@ -45,7 +44,7 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
id, itemId := utils.GetDeleteLearnedDataId(i.MessageComponentData().CustomID)
fmt.Println(id, itemId)
databases.Database.Learns.DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: id}})
databases.Database.Learns.DeleteOne(context.TODO(), databases.Learn{Id: id})
flags := discordgo.MessageFlagsIsComponentsV2
return i.EditReply(&utils.InteractionEdit{

View file

@ -8,7 +8,6 @@ import (
"git.wh64.net/muffin/goMuffin/databases"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/v2/bson"
)
var DeregisterComponent *commands.Component = &commands.Component{
@ -34,7 +33,7 @@ var DeregisterComponent *commands.Component = &commands.Component{
switch {
case strings.HasPrefix(customId, utils.DeregisterAgree):
filter := bson.D{{Key: "user_id", Value: ctx.Inter.User.ID}}
filter := databases.User{UserId: ctx.Inter.User.ID}
_, err := databases.Database.Users.DeleteOne(context.TODO(), filter)
if err != nil {
return err

View file

@ -8,8 +8,8 @@ import (
type Learn struct {
Id bson.ObjectID `bson:"_id,omitempty" json:"id"`
Command string `bson:"command"`
Result string `bson:"result"`
UserId string `bson:"user_id"`
CreatedAt time.Time `bson:"created_at"`
Command string `bson:"command,omitempty"`
Result string `bson:"result,omitempty"`
UserId string `bson:"user_id,omitempty"`
CreatedAt time.Time `bson:"created_at,omitempty"`
}

View file

@ -4,7 +4,8 @@ import "go.mongodb.org/mongo-driver/v2/bson"
type Memory struct {
Id bson.ObjectID `bson:"_id,omitempty"`
UserId string `bson:"user_id"`
Content string `bson:"content"`
Answer string `bson:"answer"`
UserId string `bson:"user_id,omitempty"`
Content string `bson:"content,omitempty"`
Answer string `bson:"answer,omitempty"`
ChatId bson.ObjectID `bson:"chat_id,omitempty"`
}

View file

@ -8,7 +8,7 @@ import (
type Text struct {
Id bson.ObjectID `bson:"_id,omitempty" json:"id"`
Text string `bson:"text" json:"text"`
Persona string `bson:"persona" json:"persona"`
CreatedAt time.Time `bson:"created_at"`
Text string `bson:"text,omitempty" json:"text"`
Persona string `bson:"persona,omitempty" json:"persona"`
CreatedAt time.Time `bson:"created_at,omitempty"`
}

View file

@ -9,10 +9,10 @@ import (
type User struct {
Id bson.ObjectID `bson:"_id,omitempty"`
UserId string `bson:"user_id"`
Blocked bool `bson:"blocked"`
BlockedReason string `bson:"blocked_reason"`
CreatedAt time.Time `bson:"created_at"`
UserId string `bson:"user_id,omitempty"`
Blocked bool `bson:"blocked,omitempty"`
BlockedReason string `bson:"blocked_reason,omitempty"`
CreatedAt time.Time `bson:"created_at,omitempty"`
}
func (d *MuffinDatabase) IsUser(userId string) bool {