Compare commits
9 commits
b7257e42d6
...
1e1c5ce561
Author | SHA1 | Date | |
---|---|---|---|
1e1c5ce561 | |||
0779a4d321 | |||
4c94cb4cfd | |||
e2c85c1ccb | |||
87f16948d6 | |||
f44e91cbff | |||
3d0ae3b8ec | |||
3768bb162e | |||
e9cef78d54 |
14 changed files with 467 additions and 109 deletions
|
@ -2,43 +2,79 @@ package commands
|
|||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type chStruct struct {
|
||||
name lenType
|
||||
name dataType
|
||||
length int
|
||||
}
|
||||
|
||||
type lenType int
|
||||
type dataType int
|
||||
|
||||
const (
|
||||
text lenType = iota
|
||||
text dataType = iota
|
||||
muffin
|
||||
nsfw
|
||||
learn
|
||||
userLearn
|
||||
)
|
||||
|
||||
var DataLengthCommand Command = Command{
|
||||
var DataLengthCommand *Command = &Command{
|
||||
ApplicationCommand: &discordgo.ApplicationCommand{
|
||||
Type: discordgo.ChatApplicationCommand,
|
||||
Name: "데이터학습량",
|
||||
Description: "봇이 학습한 데ㅇ이터량을 보여줘요.",
|
||||
},
|
||||
Aliases: []string{"학습데이터량", "데이터량", "학습량"},
|
||||
DetailedDescription: DetailedDescription{
|
||||
DetailedDescription: &DetailedDescription{
|
||||
Usage: "머핀아 학습데이터량",
|
||||
},
|
||||
}
|
||||
var ch chan chStruct = make(chan chStruct)
|
||||
|
||||
func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) {
|
||||
func getLength(data dataType, userId string) {
|
||||
var err error
|
||||
var cur *mongo.Cursor
|
||||
var datas []bson.M
|
||||
|
||||
switch data {
|
||||
case text:
|
||||
cur, err = databases.Texts.Find(context.TODO(), bson.D{{}})
|
||||
case muffin:
|
||||
cur, err = databases.Texts.Find(context.TODO(), bson.D{{Key: "persona", Value: "muffin"}})
|
||||
case nsfw:
|
||||
cur, err = databases.Texts.Find(context.TODO(), bson.D{
|
||||
{
|
||||
Key: "persona",
|
||||
Value: bson.M{
|
||||
"$regex": "^user",
|
||||
},
|
||||
},
|
||||
})
|
||||
case learn:
|
||||
cur, err = databases.Learns.Find(context.TODO(), bson.D{{}})
|
||||
case userLearn:
|
||||
cur, err = databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
defer cur.Close(context.TODO())
|
||||
|
||||
cur.All(context.TODO(), &datas)
|
||||
ch <- chStruct{name: data, length: len(datas)}
|
||||
}
|
||||
|
||||
func (c *Command) dataLengthRun(s *discordgo.Session, m any) {
|
||||
var i *discordgo.Interaction
|
||||
var referance *discordgo.MessageReference
|
||||
var username, userId, channelId string
|
||||
|
@ -48,8 +84,6 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) {
|
|||
learnLength,
|
||||
userLearnLength int
|
||||
|
||||
ch := make(chan chStruct)
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
username = m.Author.Username
|
||||
|
@ -70,73 +104,15 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) {
|
|||
})
|
||||
}
|
||||
|
||||
go func() {
|
||||
var datas []databases.Text
|
||||
go getLength(text, "")
|
||||
go getLength(muffin, "")
|
||||
go getLength(nsfw, "")
|
||||
go getLength(learn, "")
|
||||
go getLength(userLearn, userId)
|
||||
|
||||
cur, err := databases.Texts.Find(context.TODO(), bson.D{{}})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
cur.All(context.TODO(), &datas)
|
||||
ch <- chStruct{name: text, length: len(datas)}
|
||||
|
||||
}()
|
||||
|
||||
go func() {
|
||||
var datas []databases.Text
|
||||
|
||||
cur, err := databases.Texts.Find(context.TODO(), bson.D{{Key: "persona", Value: "muffin"}})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
cur.All(context.TODO(), &datas)
|
||||
ch <- chStruct{name: muffin, length: len(datas)}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
var datas []databases.Text
|
||||
|
||||
cur, err := databases.Texts.Find(context.TODO(), bson.D{
|
||||
{
|
||||
Key: "persona",
|
||||
Value: bson.M{
|
||||
"$regex": "^user",
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
cur.All(context.TODO(), &datas)
|
||||
ch <- chStruct{name: nsfw, length: len(datas)}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
var datas []databases.Learn
|
||||
|
||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{}})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
cur.All(context.TODO(), &datas)
|
||||
ch <- chStruct{name: learn, length: len(datas)}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
var datas []databases.Learn
|
||||
|
||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
cur.All(context.TODO(), &datas)
|
||||
ch <- chStruct{name: userLearn, length: len(datas)}
|
||||
}()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
for range 5 {
|
||||
resp := <-ch
|
||||
switch lenType(resp.name) {
|
||||
switch dataType(resp.name) {
|
||||
case text:
|
||||
textLength = resp.length
|
||||
case muffin:
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
|
@ -15,37 +17,45 @@ type DetailedDescription struct {
|
|||
type Command struct {
|
||||
*discordgo.ApplicationCommand
|
||||
Aliases []string
|
||||
DetailedDescription DetailedDescription
|
||||
DetailedDescription *DetailedDescription
|
||||
discommand *DiscommandStruct
|
||||
}
|
||||
|
||||
type DiscommandStruct struct {
|
||||
Commands map[string]Command
|
||||
Commands map[string]*Command
|
||||
Aliases map[string]string
|
||||
messageRuns map[string]interface{}
|
||||
chatInputRuns map[string]interface{}
|
||||
messageRuns map[string]messageRun
|
||||
chatInputRuns map[string]chatInputRun
|
||||
}
|
||||
|
||||
func new() *DiscommandStruct {
|
||||
discommand := DiscommandStruct{
|
||||
Commands: map[string]Command{},
|
||||
Commands: map[string]*Command{},
|
||||
Aliases: map[string]string{},
|
||||
messageRuns: map[string]interface{}{},
|
||||
chatInputRuns: map[string]interface{}{},
|
||||
messageRuns: map[string]messageRun{},
|
||||
chatInputRuns: map[string]chatInputRun{},
|
||||
}
|
||||
|
||||
go discommand.loadCommands(HelpCommand)
|
||||
go discommand.loadCommands(DataLengthCommand)
|
||||
go discommand.loadCommands(LearnCommand)
|
||||
go discommand.loadCommands(LearnedDataListCommand)
|
||||
|
||||
go discommand.addMessageRun(HelpCommand.Name, HelpCommand.helpMessageRun)
|
||||
go discommand.addMessageRun(DataLengthCommand.Name, DataLengthCommand.dataLengthMessageRun)
|
||||
go discommand.addMessageRun(LearnCommand.Name, LearnCommand.learnMessageRun)
|
||||
go discommand.addMessageRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListMessageRun)
|
||||
|
||||
go discommand.addChatInputRun(DataLengthCommand.Name, DataLengthCommand.dataLenghChatInputRun)
|
||||
go discommand.addChatInputRun(LearnCommand.Name, LearnCommand.learnChatInputRun)
|
||||
go discommand.addChatInputRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListChatInputRun)
|
||||
return &discommand
|
||||
}
|
||||
|
||||
func (d *DiscommandStruct) loadCommands(command Command) {
|
||||
func (d *DiscommandStruct) loadCommands(command *Command) {
|
||||
d.Commands[command.Name] = command
|
||||
d.Aliases[command.Name] = command.Name
|
||||
command.discommand = d
|
||||
|
||||
for _, alias := range command.Aliases {
|
||||
d.Aliases[alias] = command.Name
|
||||
|
@ -62,11 +72,11 @@ func (d *DiscommandStruct) addChatInputRun(name string, run chatInputRun) {
|
|||
|
||||
func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
// 더욱 나아진
|
||||
d.messageRuns[command].(messageRun)(s, m)
|
||||
d.messageRuns[command](s, m)
|
||||
}
|
||||
|
||||
func (d *DiscommandStruct) ChatInputRun(command string, s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
d.chatInputRuns[command].(chatInputRun)(s, i)
|
||||
d.chatInputRuns[command](s, i)
|
||||
}
|
||||
|
||||
var Discommand *DiscommandStruct = new()
|
||||
|
|
|
@ -6,14 +6,14 @@ import (
|
|||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
var HelpCommand Command = Command{
|
||||
var HelpCommand *Command = &Command{
|
||||
ApplicationCommand: &discordgo.ApplicationCommand{
|
||||
Type: discordgo.ChatApplicationCommand,
|
||||
Name: "도움말",
|
||||
Description: "기본적인 사용ㅂ법이에요.",
|
||||
},
|
||||
Aliases: []string{"도움", "명령어", "help"},
|
||||
DetailedDescription: DetailedDescription{
|
||||
DetailedDescription: &DetailedDescription{
|
||||
Usage: "머핀아 도움말 [명령어]",
|
||||
Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"},
|
||||
},
|
||||
|
|
215
commands/learn.go
Normal file
215
commands/learn.go
Normal file
|
@ -0,0 +1,215 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.wh64.net/muffin/goMuffin/configs"
|
||||
"git.wh64.net/muffin/goMuffin/databases"
|
||||
"git.wh64.net/muffin/goMuffin/utils"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"github.com/LoperLee/golang-hangul-toolkit/hangul"
|
||||
)
|
||||
|
||||
var LearnCommand *Command = &Command{
|
||||
ApplicationCommand: &discordgo.ApplicationCommand{
|
||||
Type: discordgo.ChatApplicationCommand,
|
||||
Name: "배워",
|
||||
Description: "단어를 가르치는 명령ㅇ어에요.",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "단어",
|
||||
Description: "등록할 단어를 입력해주세요.",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "대답",
|
||||
Description: "해당 단어의 대답을 입력해주세요.",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
Aliases: []string{"공부"},
|
||||
DetailedDescription: &DetailedDescription{
|
||||
Usage: "머핀아 배워 (등록할 단어) (대답)",
|
||||
Examples: []string{"머핀아 배워 안녕 안녕!",
|
||||
"머핀아 배워 \"야 죽을래?\" \"아니요 ㅠㅠㅠ\"",
|
||||
"머핀아 배워 미간은_누구야? 이봇의_개발자요",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func addPrefix(arr []string) (newArr []string) {
|
||||
for _, item := range arr {
|
||||
newArr = append(newArr, "- "+item)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Command) learnRun(s *discordgo.Session, m any) {
|
||||
var userId, command, result string
|
||||
|
||||
igCommands := []string{}
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
userId = m.Author.ID
|
||||
matches := utils.ExtractQuotedText.FindAllStringSubmatch(strings.TrimPrefix(m.Content, configs.Config.Bot.Prefix), 2)
|
||||
|
||||
if len(matches) < 2 {
|
||||
content := strings.TrimPrefix(m.Content, configs.Config.Bot.Prefix)
|
||||
command = strings.ReplaceAll(strings.Split(content, " ")[1], "_", "")
|
||||
result = strings.ReplaceAll(strings.Split(content, " ")[2], "_", "")
|
||||
|
||||
if command == "" || result == "" {
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "올바르지 않ㅇ은 용법이에요.",
|
||||
Fields: []*discordgo.MessageEmbedField{
|
||||
{
|
||||
Name: "사용법",
|
||||
Value: utils.InlineCode(c.DetailedDescription.Usage),
|
||||
Inline: true,
|
||||
},
|
||||
{
|
||||
Name: "예시",
|
||||
Value: strings.Join(addPrefix(c.DetailedDescription.Examples), "\n"),
|
||||
},
|
||||
},
|
||||
Color: int(utils.EFail),
|
||||
}, m.Reference())
|
||||
return
|
||||
}
|
||||
} else {
|
||||
command = matches[0][1]
|
||||
result = matches[1][1]
|
||||
}
|
||||
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
userId = m.Member.User.ID
|
||||
|
||||
optsMap := map[string]*discordgo.ApplicationCommandInteractionDataOption{}
|
||||
for _, opt := range m.ApplicationCommandData().Options {
|
||||
optsMap[opt.Name] = opt
|
||||
}
|
||||
|
||||
if opt, ok := optsMap["단어"]; ok {
|
||||
command = opt.StringValue()
|
||||
}
|
||||
|
||||
if opt, ok := optsMap["대답"]; ok {
|
||||
result = opt.StringValue()
|
||||
}
|
||||
}
|
||||
|
||||
for _, command := range c.discommand.Commands {
|
||||
igCommands = append(igCommands, command.Name)
|
||||
igCommands = append(igCommands, command.Aliases...)
|
||||
}
|
||||
|
||||
ignores := []string{"미간", "Migan", "migan", "간미"}
|
||||
ignores = append(ignores, igCommands...)
|
||||
|
||||
disallows := []string{
|
||||
"@everyone",
|
||||
"@here",
|
||||
"<@" + configs.Config.Bot.OwnerId + ">"}
|
||||
|
||||
for _, ig := range ignores {
|
||||
if strings.Contains(command, ig) {
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "해ㄷ당 단어는 배우기 껄끄ㄹ럽네요.",
|
||||
Color: int(utils.EFail),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, di := range disallows {
|
||||
if strings.Contains(result, di) {
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "해당 단ㅇ어의 대답으로 하기 좀 그렇ㄴ네요.",
|
||||
Color: int(utils.EFail),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err := databases.Learns.InsertOne(context.TODO(), databases.InsertLearn{
|
||||
Command: command,
|
||||
Result: result,
|
||||
UserId: userId,
|
||||
CreatedAt: time.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "단어를 배우는데 오류가 생겼어요.",
|
||||
Color: int(utils.EFail),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "✅ 성공",
|
||||
Description: hangul.GetJosa(command, hangul.EUL_REUL) + " 배웠어요.",
|
||||
Color: int(utils.ESuccess),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Command) learnMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
c.learnRun(s, m)
|
||||
}
|
||||
|
||||
func (c *Command) learnChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
c.learnRun(s, i)
|
||||
}
|
123
commands/learnedDataList.go
Normal file
123
commands/learnedDataList.go
Normal file
|
@ -0,0 +1,123 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
var LearnedDataListCommand *Command = &Command{
|
||||
ApplicationCommand: &discordgo.ApplicationCommand{
|
||||
Type: discordgo.ChatApplicationCommand,
|
||||
Name: "리스트",
|
||||
Description: "당신이 가ㄹ르쳐준 단어를 나열해요.",
|
||||
},
|
||||
Aliases: []string{"list", "목록", "지식목록"},
|
||||
DetailedDescription: &DetailedDescription{
|
||||
Usage: "머핀아 리스트",
|
||||
},
|
||||
}
|
||||
|
||||
func getDescriptions(datas *[]databases.Learn) (descriptions []string) {
|
||||
for _, data := range *datas {
|
||||
descriptions = append(descriptions, "- "+data.Command+": "+data.Result)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Command) learnedDataListRun(s *discordgo.Session, m any) {
|
||||
var userId, globalName, avatarUrl string
|
||||
var datas []databases.Learn
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
userId = m.Author.ID
|
||||
globalName = m.Author.GlobalName
|
||||
avatarUrl = m.Author.AvatarURL("512")
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
userId = m.Member.User.ID
|
||||
globalName = m.Member.User.GlobalName
|
||||
avatarUrl = m.User.AvatarURL("512")
|
||||
}
|
||||
|
||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "당신은 지식ㅇ을 가르쳐준 적이 없어요!",
|
||||
Color: int(utils.EFail),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(err)
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "❌ 오류",
|
||||
Description: "데이터를 가져오는데 실패했어요.",
|
||||
Color: int(utils.EFail),
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
defer cur.Close(context.TODO())
|
||||
|
||||
cur.All(context.TODO(), &datas)
|
||||
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: globalName + "님이 알려주신 지식",
|
||||
Description: utils.CodeBlockWithLanguage("md", "# 총 "+strconv.Itoa(len(datas))+"개에요.\n"+strings.Join(getDescriptions(&datas), "\n")),
|
||||
Color: int(utils.EDefault),
|
||||
Thumbnail: &discordgo.MessageEmbedThumbnail{
|
||||
URL: avatarUrl,
|
||||
},
|
||||
}
|
||||
|
||||
switch m := m.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||
case *discordgo.InteractionCreate:
|
||||
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Command) learnedDataListMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
c.learnedDataListRun(s, m)
|
||||
}
|
||||
|
||||
func (c *Command) learnedDataListChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
c.learnedDataListRun(s, i)
|
||||
}
|
|
@ -8,11 +8,11 @@ import (
|
|||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var MUFFIN_VERSION = "0.0.0-gopher_canary.250326a"
|
||||
|
||||
type botConfig struct {
|
||||
Token string
|
||||
Prefix string
|
||||
Token string
|
||||
Prefix string
|
||||
OwnerId string
|
||||
}
|
||||
|
||||
type trainConfig struct {
|
||||
|
@ -41,6 +41,7 @@ func loadConfig() *MuffinConfig {
|
|||
func setConfig(config *MuffinConfig) {
|
||||
config.Bot.Prefix = os.Getenv("BOT_PREFIX")
|
||||
config.Bot.Token = os.Getenv("BOT_TOKEN")
|
||||
config.Bot.OwnerId = os.Getenv("BOT_OWNER_ID")
|
||||
|
||||
config.Train.UserID = os.Getenv("TRAIN_USER_ID")
|
||||
|
||||
|
|
3
configs/version.go
Normal file
3
configs/version.go
Normal file
|
@ -0,0 +1,3 @@
|
|||
package configs
|
||||
|
||||
var MUFFIN_VERSION = "0.0.0-gopher_canary.250329a"
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module git.wh64.net/muffin/goMuffin
|
|||
go 1.23.2
|
||||
|
||||
require (
|
||||
github.com/LoperLee/golang-hangul-toolkit v1.1.0
|
||||
github.com/bwmarrin/discordgo v0.28.1
|
||||
github.com/joho/godotenv v1.5.1
|
||||
go.mongodb.org/mongo-driver/v2 v2.1.0
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
|||
github.com/LoperLee/golang-hangul-toolkit v1.1.0 h1:JEyLpLyA2hDQwWY9oCprHClnKIdkYVOSJzAat2uFX/A=
|
||||
github.com/LoperLee/golang-hangul-toolkit v1.1.0/go.mod h1:CDbZ23/IL4v2ovWIOb7xDEiFcSc0pIIbbYTpg+gP+Sk=
|
||||
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=
|
||||
|
|
|
@ -7,5 +7,4 @@ import (
|
|||
|
||||
func InteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
commands.Discommand.ChatInputRun(i.ApplicationCommandData().Name, s, i)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
|
||||
if strings.HasPrefix(m.Content, config.Bot.Prefix) {
|
||||
content := strings.TrimPrefix(m.Content, config.Bot.Prefix)
|
||||
command := commands.Discommand.Aliases[content]
|
||||
command := commands.Discommand.Aliases[strings.Split(content, " ")[0]]
|
||||
|
||||
if m.Author.ID == config.Train.UserID {
|
||||
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||
|
@ -44,6 +44,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
var learnDatas []databases.Learn
|
||||
var filter bson.D
|
||||
|
||||
ch := make(chan int)
|
||||
x := rand.Intn(5)
|
||||
|
||||
channel, _ := s.Channel(m.ChannelID)
|
||||
|
@ -62,21 +63,35 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
filter = bson.D{{Key: "persona", Value: "muffin"}}
|
||||
}
|
||||
|
||||
tCur, err := databases.Texts.Find(context.TODO(), filter)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
lCur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "command", Value: content}})
|
||||
if err != nil {
|
||||
if err == mongo.ErrNilDocument {
|
||||
learnDatas = []databases.Learn{}
|
||||
go func() {
|
||||
cur, err := databases.Texts.Find(context.TODO(), filter)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
tCur.All(context.TODO(), &datas)
|
||||
lCur.All(context.TODO(), &learnDatas)
|
||||
defer cur.Close(context.TODO())
|
||||
|
||||
cur.All(context.TODO(), &datas)
|
||||
ch <- 1
|
||||
}()
|
||||
go func() {
|
||||
cur, 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)
|
||||
}
|
||||
|
||||
defer cur.Close(context.TODO())
|
||||
|
||||
cur.All(context.TODO(), &learnDatas)
|
||||
ch <- 1
|
||||
}()
|
||||
|
||||
for range 2 {
|
||||
<-ch
|
||||
}
|
||||
|
||||
if x > 2 && len(learnDatas) != 0 {
|
||||
data := learnDatas[rand.Intn(len(learnDatas))]
|
||||
|
|
13
utils/codeFormatter.go
Normal file
13
utils/codeFormatter.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package utils
|
||||
|
||||
func InlineCode(str string) string {
|
||||
return "`" + str + "`"
|
||||
}
|
||||
|
||||
func CodeBlockWithLanguage(language string, content string) string {
|
||||
return "```" + language + "\n" + content + "\n" + "```"
|
||||
}
|
||||
|
||||
func CodeBlock(content string) string {
|
||||
return "```\n" + content + "\n" + "```"
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package utils
|
||||
|
||||
func InlineCode(str string) string {
|
||||
return "`" + str + "`"
|
||||
}
|
5
utils/regexp.go
Normal file
5
utils/regexp.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package utils
|
||||
|
||||
import "regexp"
|
||||
|
||||
var ExtractQuotedText *regexp.Regexp = regexp.MustCompile("[\"'`](.*?)[\"'`]")
|
Loading…
Reference in a new issue