chore: edit database struct
This commit is contained in:
parent
da63ce7ff9
commit
f69b3f13a3
11 changed files with 42 additions and 58 deletions
|
@ -89,9 +89,9 @@ func dataLengthRun(s *discordgo.Session, m any) {
|
|||
}
|
||||
|
||||
dataLengthWg.Add(5)
|
||||
go getLength(text, databases.Texts, bson.D{{}})
|
||||
go getLength(muffin, databases.Texts, bson.D{{Key: "persona", Value: "muffin"}})
|
||||
go getLength(nsfw, databases.Texts, bson.D{
|
||||
go getLength(text, databases.Database.Texts, bson.D{{}})
|
||||
go getLength(muffin, databases.Database.Texts, bson.D{{Key: "persona", Value: "muffin"}})
|
||||
go getLength(nsfw, databases.Database.Texts, bson.D{
|
||||
{
|
||||
Key: "persona",
|
||||
Value: bson.M{
|
||||
|
@ -99,8 +99,8 @@ func dataLengthRun(s *discordgo.Session, m any) {
|
|||
},
|
||||
},
|
||||
})
|
||||
go getLength(learn, databases.Learns, bson.D{{}})
|
||||
go getLength(userLearn, databases.Learns, bson.D{{Key: "user_id", Value: userId}})
|
||||
go getLength(learn, databases.Database.Learns, bson.D{{}})
|
||||
go getLength(userLearn, databases.Database.Learns, bson.D{{Key: "user_id", Value: userId}})
|
||||
|
||||
go func() {
|
||||
dataLengthWg.Wait()
|
||||
|
|
|
@ -74,7 +74,7 @@ func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]strin
|
|||
userId = m.Member.User.ID
|
||||
}
|
||||
|
||||
cur, err := databases.Learns.Find(context.TODO(), bson.M{"user_id": userId, "command": command})
|
||||
cur, err := databases.Database.Learns.Find(context.TODO(), bson.M{"user_id": userId, "command": command})
|
||||
if err != nil {
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
|
|
|
@ -173,7 +173,7 @@ func learnRun(c *Command, s *discordgo.Session, m any, args *[]string) {
|
|||
}
|
||||
}
|
||||
|
||||
_, err := databases.Learns.InsertOne(context.TODO(), databases.InsertLearn{
|
||||
_, err := databases.Database.Learns.InsertOne(context.TODO(), databases.InsertLearn{
|
||||
Command: command,
|
||||
Result: result,
|
||||
UserId: userId,
|
||||
|
|
|
@ -115,7 +115,7 @@ func learnedDataListRun(s *discordgo.Session, m any, args *[]string) {
|
|||
}
|
||||
}
|
||||
|
||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}, filter})
|
||||
cur, err := databases.Database.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}, filter})
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
embed := &discordgo.MessageEmbed{
|
||||
|
|
|
@ -69,7 +69,7 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{
|
|||
id, _ := bson.ObjectIDFromHex(strings.ReplaceAll(utils.ItemIdRegexp.ReplaceAllString(i.MessageComponentData().Values[0][len(utils.DeleteLearnedData):], ""), "&", ""))
|
||||
itemId := strings.ReplaceAll(utils.ItemIdRegexp.FindAllString(i.MessageComponentData().Values[0], 1)[0], "No.", "")
|
||||
|
||||
databases.Learns.DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: id}})
|
||||
databases.Database.Learns.DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: id}})
|
||||
|
||||
i.EditReply(&discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{
|
||||
|
|
|
@ -3,9 +3,7 @@ package databases
|
|||
import (
|
||||
"time"
|
||||
|
||||
"git.wh64.net/muffin/goMuffin/configs"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
type InsertLearn struct {
|
||||
|
@ -22,5 +20,3 @@ type Learn struct {
|
|||
UserId string `bson:"user_id" json:"user_id"`
|
||||
CreatedAt time.Time `bson:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
var Learns *mongo.Collection = Client.Database(configs.Config.DBName).Collection("learn")
|
||||
|
|
|
@ -3,9 +3,7 @@ package databases
|
|||
import (
|
||||
"time"
|
||||
|
||||
"git.wh64.net/muffin/goMuffin/configs"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
type InsertText struct {
|
||||
|
@ -20,5 +18,3 @@ type Text struct {
|
|||
Persona string `bson:"persona" json:"persona"`
|
||||
CreatedAt time.Time `bson:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
var Texts *mongo.Collection = Client.Database(configs.Config.DBName).Collection("text")
|
||||
|
|
|
@ -8,13 +8,31 @@ import (
|
|||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
func connect() *mongo.Client {
|
||||
client, err := mongo.Connect(options.Client().ApplyURI(configs.Config.DatabaseURL))
|
||||
type MuffinDatabase struct {
|
||||
Client *mongo.Client
|
||||
Learns *mongo.Collection
|
||||
Texts *mongo.Collection
|
||||
}
|
||||
|
||||
var Database *MuffinDatabase
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
|
||||
Database, err = Connect()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
var Client *mongo.Client = connect()
|
||||
func Connect() (*MuffinDatabase, error) {
|
||||
client, err := mongo.Connect(options.Client().ApplyURI(configs.Config.DatabaseURL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MuffinDatabase{
|
||||
Client: client,
|
||||
Learns: client.Database(configs.Config.DBName).Collection("learn"),
|
||||
Texts: client.Database(configs.Config.DBName).Collection("text"),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
command := commands.Discommand.Aliases[args[0]]
|
||||
|
||||
if m.Author.ID == config.Train.UserID {
|
||||
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||
if _, err := databases.Database.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||
Text: content,
|
||||
Persona: "muffin",
|
||||
CreatedAt: time.Now(),
|
||||
|
@ -83,7 +83,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
if channel.NSFW {
|
||||
filter = bson.D{{}}
|
||||
|
||||
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||
if _, err := databases.Database.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||
Text: content,
|
||||
Persona: fmt.Sprintf("user:%s", m.Author.Username),
|
||||
CreatedAt: time.Now(),
|
||||
|
@ -96,7 +96,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
}
|
||||
|
||||
go func() {
|
||||
cur, err := databases.Texts.Find(context.TODO(), filter)
|
||||
cur, err := databases.Database.Texts.Find(context.TODO(), filter)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
ch <- 1
|
||||
}()
|
||||
go func() {
|
||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "command", Value: content}})
|
||||
cur, err := databases.Database.Learns.Find(context.TODO(), bson.D{{Key: "command", Value: content}})
|
||||
if err != nil {
|
||||
if err == mongo.ErrNilDocument {
|
||||
learnData = []databases.Learn{}
|
||||
|
|
2
main.go
2
main.go
|
@ -108,7 +108,7 @@ func main() {
|
|||
go dg.ApplicationCommandCreate(dg.State.User.ID, "", cmd.ApplicationCommand)
|
||||
}
|
||||
|
||||
defer databases.Client.Disconnect(context.TODO())
|
||||
defer databases.Database.Client.Disconnect(context.TODO())
|
||||
|
||||
log.Println("[goMuffin] 봇이 실행되고 있어요. 버전:", configs.MUFFIN_VERSION)
|
||||
sc := make(chan os.Signal, 1)
|
||||
|
|
|
@ -10,14 +10,11 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"git.wh64.net/muffin/goMuffin/configs"
|
||||
"git.wh64.net/muffin/goMuffin/databases"
|
||||
"git.wh64.net/muffin/goMuffin/utils"
|
||||
"github.com/devproje/commando"
|
||||
"github.com/devproje/commando/option"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var date time.Time = time.Now()
|
||||
|
@ -130,10 +127,11 @@ func saveFileToJSONL(path, name string, data any) error {
|
|||
}
|
||||
|
||||
func ExportData(n *commando.Node) error {
|
||||
defer databases.Database.Client.Disconnect(context.TODO())
|
||||
|
||||
var wg sync.WaitGroup
|
||||
ch := make(chan error, 3)
|
||||
|
||||
databases.Client.Disconnect(context.TODO()) // databases 패키지의 DB 연결은 필요 없음 (나중에 수정 예정)
|
||||
fileType, err := option.ParseString(*n.MustGetOpt("type"), n)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -172,15 +170,7 @@ func ExportData(n *commando.Node) error {
|
|||
|
||||
var data []databases.Text
|
||||
|
||||
conn, err := mongo.Connect(options.Client().ApplyURI(configs.Config.DatabaseURL))
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
|
||||
defer conn.Disconnect(context.TODO())
|
||||
|
||||
cur, err := conn.Database(configs.Config.DBName).Collection("text").Find(context.TODO(), bson.D{{Key: "persona", Value: "muffin"}})
|
||||
cur, err := databases.Database.Texts.Find(context.TODO(), bson.D{{Key: "persona", Value: "muffin"}})
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
|
@ -234,15 +224,7 @@ func ExportData(n *commando.Node) error {
|
|||
|
||||
var data []databases.Text
|
||||
|
||||
conn, err := mongo.Connect(options.Client().ApplyURI(configs.Config.DatabaseURL))
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
|
||||
defer conn.Disconnect(context.TODO())
|
||||
|
||||
cur, err := conn.Database(configs.Config.DBName).Collection("text").Find(context.TODO(), bson.D{
|
||||
cur, err := databases.Database.Texts.Find(context.TODO(), bson.D{
|
||||
{
|
||||
Key: "persona",
|
||||
Value: bson.M{
|
||||
|
@ -292,15 +274,7 @@ func ExportData(n *commando.Node) error {
|
|||
|
||||
var data []databases.Learn
|
||||
|
||||
conn, err := mongo.Connect(options.Client().ApplyURI(configs.Config.DatabaseURL))
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
|
||||
defer conn.Disconnect(context.TODO())
|
||||
|
||||
cur, err := conn.Database(configs.Config.DBName).Collection("learn").Find(context.TODO(), bson.D{{}})
|
||||
cur, err := databases.Database.Learns.Find(context.TODO(), bson.D{{}})
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
|
|
Loading…
Reference in a new issue