Compare commits
5 commits
acc4b02f99
...
1b6e35cd6c
Author | SHA1 | Date | |
---|---|---|---|
1b6e35cd6c | |||
6ea5198fd2 | |||
7b965942c0 | |||
55b0ab9edb | |||
68c558a3d4 |
8 changed files with 123 additions and 17 deletions
|
@ -8,12 +8,21 @@ 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
|
||||
Prefix string
|
||||
}
|
||||
|
||||
type trainConfig struct {
|
||||
UserID string
|
||||
}
|
||||
|
||||
// MuffinConfig for Muffin bot
|
||||
type MuffinConfig struct {
|
||||
Token string
|
||||
Prefix string
|
||||
Bot botConfig
|
||||
Train trainConfig
|
||||
DatabaseURL string
|
||||
}
|
||||
|
||||
|
@ -23,15 +32,18 @@ func loadConfig() *MuffinConfig {
|
|||
fmt.Println("[goMuffin] 봇의 설절파일을 불러올 수가 없어요.")
|
||||
log.Fatalln(err)
|
||||
}
|
||||
config := MuffinConfig{}
|
||||
config := MuffinConfig{Bot: botConfig{}, Train: trainConfig{}}
|
||||
setConfig(&config)
|
||||
|
||||
return &config
|
||||
}
|
||||
|
||||
func setConfig(config *MuffinConfig) {
|
||||
config.Prefix = os.Getenv("PREFIX")
|
||||
config.Token = os.Getenv("TOKEN")
|
||||
config.Bot.Prefix = os.Getenv("BOT_PREFIX")
|
||||
config.Bot.Token = os.Getenv("BOT_TOKEN")
|
||||
|
||||
config.Train.UserID = os.Getenv("TRAIN_USER_ID")
|
||||
|
||||
config.DatabaseURL = os.Getenv("DATABASE_URL")
|
||||
}
|
||||
|
||||
|
|
25
databases/Learn.go
Normal file
25
databases/Learn.go
Normal 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")
|
|
@ -1,15 +1,23 @@
|
|||
package databases
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
type InsertText struct {
|
||||
Text string
|
||||
Persona string
|
||||
CreatedAt time.Time `bson:"created_at"`
|
||||
}
|
||||
|
||||
type Text struct {
|
||||
Id bson.ObjectID `bson:"_id"`
|
||||
Text string
|
||||
Persona string
|
||||
CreatedAt bson.DateTime `bson:"created_at"`
|
||||
CreatedAt time.Time `bson:"created_at"`
|
||||
}
|
||||
|
||||
var Texts *mongo.Collection = Client.Database("muffin_ai_test").Collection("text")
|
||||
|
|
2
go.mod
2
go.mod
|
@ -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
7
go.sum
|
@ -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=
|
||||
|
|
|
@ -5,50 +5,103 @@ import (
|
|||
"log"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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
|
||||
func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
config := configs.Config
|
||||
if m.Author.ID == s.State.User.ID && m.Author.Bot {
|
||||
if m.Author.ID == s.State.User.ID || m.Author.Bot {
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, config.Prefix) {
|
||||
content := strings.TrimPrefix(m.Content, config.Prefix)
|
||||
if strings.HasPrefix(m.Content, config.Bot.Prefix) {
|
||||
content := strings.TrimPrefix(m.Content, config.Bot.Prefix)
|
||||
command := commands.Discommand.Aliases[content]
|
||||
|
||||
if m.Author.ID == config.Train.UserID {
|
||||
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||
Text: content,
|
||||
Persona: "muffin",
|
||||
CreatedAt: time.Now(),
|
||||
}); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
if command == "" {
|
||||
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{{}}
|
||||
|
||||
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||
Text: content,
|
||||
Persona: "user:" + m.Author.Username,
|
||||
CreatedAt: time.Now(),
|
||||
}); 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.Background())
|
||||
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
|
||||
}
|
||||
|
||||
commands.Discommand.MessageRun(command, s, m)
|
||||
return
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
|
2
main.go
2
main.go
|
@ -17,7 +17,7 @@ import (
|
|||
func main() {
|
||||
config := configs.Config
|
||||
|
||||
dg, err := discordgo.New("Bot " + config.Token)
|
||||
dg, err := discordgo.New("Bot " + config.Bot.Token)
|
||||
if err != nil {
|
||||
fmt.Println("[goMuffin] 봇의 세션을 만들수가 없어요.")
|
||||
log.Fatalln(err)
|
||||
|
|
5
utils/inlineCode.go
Normal file
5
utils/inlineCode.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package utils
|
||||
|
||||
func InlineCode(str string) string {
|
||||
return "`" + str + "`"
|
||||
}
|
Loading…
Reference in a new issue