many new
This commit is contained in:
40
database/mdb/rp_chats.go
Normal file
40
database/mdb/rp_chats.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package mdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kurumibot/database"
|
||||
"kurumibot/laniakea"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type RPChatMessage struct {
|
||||
ChatID string `bson:"chat_id"`
|
||||
Role string `bson:"role"`
|
||||
Message string `bson:"message"`
|
||||
}
|
||||
|
||||
func GetChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*RPChatMessage, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
col := database.GetMongoCollection(db, "rp_chat_history")
|
||||
cursor, err := col.Find(ctx, bson.M{"chat_id": chatId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]*RPChatMessage, 0)
|
||||
err = cursor.All(ctx, &result)
|
||||
return result, err
|
||||
}
|
||||
func UpdateChatHistory(db *laniakea.DatabaseContext, chatId, role, message string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
col := database.GetMongoCollection(db, "rp_chat_history")
|
||||
_, err := col.InsertOne(ctx, RPChatMessage{
|
||||
chatId,
|
||||
role,
|
||||
message,
|
||||
})
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user