feat: Add learn data return

This commit is contained in:
Siwoo Jeon 2025-03-24 21:56:26 +09:00
parent 6ea5198fd2
commit 1b6e35cd6c
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
6 changed files with 72 additions and 7 deletions

View file

@ -8,7 +8,7 @@ import (
"github.com/joho/godotenv"
)
var MUFFIN_VERSION = "0.0.0-gopher_canary.250323b"
var MUFFIN_VERSION = "0.0.0-gopher_canary.250324a"
type botConfig struct {
Token string

25
databases/Learn.go Normal file
View file

@ -0,0 +1,25 @@
package databases
import (
"time"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
type InsertLearn struct {
Command string
Result string
UserId string `bson:"user_id"`
CreatedAt time.Time `bson:"created_at"`
}
type Learn struct {
Id bson.ObjectID `bson:"_id"`
Command string
Result string
UserId string `bson:"user_id"`
CreatedAt time.Time `bson:"created_at"`
}
var Learns *mongo.Collection = Client.Database("muffin_ai_test").Collection("learn")

2
go.mod
View file

@ -5,6 +5,7 @@ go 1.23.2
require (
github.com/bwmarrin/discordgo v0.28.1
github.com/joho/godotenv v1.5.1
go.mongodb.org/mongo-driver/v2 v2.1.0
)
require (
@ -15,7 +16,6 @@ require (
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.mongodb.org/mongo-driver/v2 v2.1.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect

7
go.sum
View file

@ -1,7 +1,11 @@
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
@ -20,7 +24,6 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
go.mongodb.org/mongo-driver/v2 v2.1.0 h1:/ELnVNjmfUKDsoBisXxuJL0noR9CfeUIrP7Yt3R+egg=
go.mongodb.org/mongo-driver/v2 v2.1.0/go.mod h1:AWiLRShSrk5RHQS3AEn3RL19rqOzVq49MCpWQ3x/huI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
@ -34,11 +37,11 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

View file

@ -10,8 +10,10 @@ import (
"git.wh64.net/muffin/goMuffin/commands"
"git.wh64.net/muffin/goMuffin/configs"
"git.wh64.net/muffin/goMuffin/databases"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
// MessageCreate is handlers of messageCreate event
@ -39,8 +41,12 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelTyping(m.ChannelID)
var datas []databases.Text
var learnDatas []databases.Learn
var filter bson.D
ch := make(chan bool)
x := rand.Intn(5)
channel, _ := s.Channel(m.ChannelID)
if channel.NSFW {
filter = bson.D{{}}
@ -52,17 +58,43 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
}); err != nil {
log.Fatalln(err)
}
} else {
filter = bson.D{{Key: "persona", Value: "muffin"}}
}
cur, err := databases.Texts.Find(context.TODO(), filter)
tCur, err := databases.Texts.Find(context.TODO(), filter)
if err != nil {
log.Fatalln(err)
}
defer cur.Close(context.TODO())
cur.All(context.TODO(), &datas)
lCur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "command", Value: content}})
if err != nil {
if err == mongo.ErrNilDocument {
learnDatas = []databases.Learn{}
}
log.Fatalln(err)
}
go func() {
defer func() {
tCur.Close(context.TODO())
lCur.Close(context.TODO())
}()
tCur.All(context.TODO(), &datas)
lCur.All(context.TODO(), &learnDatas)
ch <- true
}()
<-ch
if x > 2 && len(learnDatas) != 0 {
data := learnDatas[rand.Intn(len(learnDatas))]
user, _ := s.User(data.UserId)
s.ChannelMessageSendReply(m.ChannelID, data.Result+"\n"+utils.InlineCode(user.Username+"님이 알려주셨어요."), m.Reference())
return
}
s.ChannelMessageSendReply(m.ChannelID, datas[rand.Intn(len(datas))].Text, m.Reference())
return

5
utils/inlineCode.go Normal file
View file

@ -0,0 +1,5 @@
package utils
func InlineCode(str string) string {
return "`" + str + "`"
}