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