feat: add search in learnedDataList

This commit is contained in:
Siwoo Jeon 2025-05-10 19:34:02 +09:00
parent 302fa9d3d4
commit 052924fcb5
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 72 additions and 9 deletions

View file

@ -13,22 +13,39 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo"
)
const (
learnArgsCommand = "단어:"
learnArgsResult = "대답:"
)
var LearnedDataListCommand *Command = &Command{
ApplicationCommand: &discordgo.ApplicationCommand{
Type: discordgo.ChatApplicationCommand,
Name: "리스트",
Description: "당신이 가ㄹ르쳐준 지식을 나열해요.",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "쿼리",
Description: "해당 단어가 포함된 결과만 찾아요.",
Required: false,
},
},
},
Aliases: []string{"list", "목록", "지식목록"},
DetailedDescription: &DetailedDescription{
Usage: fmt.Sprintf("%s리스트", configs.Config.Bot.Prefix),
Examples: []string{
fmt.Sprintf("%s리스트 ㅁㄴㅇㄹ", configs.Config.Bot.Prefix),
fmt.Sprintf("%s리스트 단어:안녕", configs.Config.Bot.Prefix),
fmt.Sprintf("%s리스트 대답:머핀", configs.Config.Bot.Prefix),
},
},
Category: Chatting,
MessageRun: func(ctx *MsgContext) {
learnedDataListRun(ctx.Session, ctx.Msg)
learnedDataListRun(ctx.Session, ctx.Msg, &ctx.Args)
},
ChatInputRun: func(ctx *ChatInputContext) {
learnedDataListRun(ctx.Session, ctx.Inter)
learnedDataListRun(ctx.Session, ctx.Inter, nil)
},
}
@ -39,24 +56,66 @@ func getDescriptions(data *[]databases.Learn) (descriptions []string) {
return
}
func learnedDataListRun(s *discordgo.Session, m any) {
func learnedDataListRun(s *discordgo.Session, m any, args *[]string) {
var userId, globalName, avatarUrl string
var data []databases.Learn
var filter bson.E
switch m := m.(type) {
case *discordgo.MessageCreate:
userId = m.Author.ID
globalName = m.Author.GlobalName
avatarUrl = m.Author.AvatarURL("512")
query := strings.Join(*args, " ")
if strings.HasPrefix(query, learnArgsResult) {
query, _ = strings.CutPrefix(query, learnArgsResult)
filter = bson.E{
Key: "result",
Value: bson.M{
"$regex": query,
},
}
} else {
query, _ = strings.CutPrefix(query, learnArgsCommand)
filter = bson.E{
Key: "command",
Value: bson.M{
"$regex": query,
},
}
}
case *utils.InteractionCreate:
m.DeferReply(true)
userId = m.Member.User.ID
globalName = m.Member.User.GlobalName
avatarUrl = m.Member.User.AvatarURL("512")
if opt, ok := m.Options["쿼리"]; ok {
query := opt.StringValue()
if strings.HasPrefix(query, learnArgsResult) {
query, _ = strings.CutPrefix(query, learnArgsResult)
filter = bson.E{
Key: "result",
Value: bson.M{
"$regex": query,
},
}
} else {
query, _ = strings.CutPrefix(query, learnArgsCommand)
filter = bson.E{
Key: "command",
Value: bson.M{
"$regex": query,
},
}
}
}
}
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}, filter})
if err != nil {
if err == mongo.ErrNoDocuments {
embed := &discordgo.MessageEmbed{

View file

@ -7,7 +7,7 @@ import (
"git.wh64.net/muffin/goMuffin/utils"
)
const MUFFIN_VERSION = "5.0.1-gopher_release.250505a"
const MUFFIN_VERSION = "5.1.0-gopher_dev.250510a"
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]

View file

@ -2,7 +2,11 @@ package utils
import "regexp"
var FlexibleStringParser *regexp.Regexp = regexp.MustCompile("[^\\s\"'「」«»]+|\"([^\"]*)\"|'([^']*)'|「([^」]*)」|«([^»]*)»")
var Decimals *regexp.Regexp = regexp.MustCompile(`\d+`)
var ItemIdRegexp *regexp.Regexp = regexp.MustCompile(`No.\d+`)
var EmojiRegexp *regexp.Regexp = regexp.MustCompile(`<a?:\w+:\d+>`)
var (
FlexibleStringParser = regexp.MustCompile("[^\\s\"'「」«»]+|\"([^\"]*)\"|'([^']*)'|「([^」]*)」|«([^»]*)»")
Decimals = regexp.MustCompile(`\d+`)
ItemIdRegexp = regexp.MustCompile(`No.\d+`)
EmojiRegexp = regexp.MustCompile(`<a?:\w+:\d+>`)
LearnQueryCommand = regexp.MustCompile(`^단어:`)
LearnQueryResult = regexp.MustCompile(`^대답:`)
)