From d6a47dbfd083219d0b74338e27793d469adc2789 Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Sun, 30 Mar 2025 15:08:29 +0900 Subject: [PATCH] feat: Add args --- commands/dataLength.go | 9 +++--- commands/discommand.go | 29 +++++++++++++++--- commands/information.go | 9 +++--- commands/learn.go | 61 ++++++++++++++++--------------------- commands/learnedDataList.go | 9 +++--- handler/messageCreate.go | 16 ++++++++-- utils/regexp.go | 2 +- 7 files changed, 81 insertions(+), 54 deletions(-) diff --git a/commands/dataLength.go b/commands/dataLength.go index 4d5d74c..b51053f 100644 --- a/commands/dataLength.go +++ b/commands/dataLength.go @@ -37,6 +37,7 @@ var DataLengthCommand *Command = &Command{ DetailedDescription: &DetailedDescription{ Usage: "머핀아 학습데이터량", }, + Category: Generals, } var ch chan chStruct = make(chan chStruct) @@ -172,10 +173,10 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m any) { } } -func (c *Command) dataLengthMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { - c.dataLengthRun(s, m) +func (c *Command) dataLengthMessageRun(ctx *MsgContext) { + c.dataLengthRun(ctx.Session, ctx.Msg) } -func (c *Command) dataLenghChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) { - c.dataLengthRun(s, i) +func (c *Command) dataLenghChatInputRun(ctx *InterContext) { + c.dataLengthRun(ctx.Session, ctx.Inter) } diff --git a/commands/discommand.go b/commands/discommand.go index 351eaad..a5b1f8a 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -6,8 +6,10 @@ import ( "github.com/bwmarrin/discordgo" ) -type messageRun func(s *discordgo.Session, m *discordgo.MessageCreate) -type chatInputRun func(s *discordgo.Session, m *discordgo.InteractionCreate) +type messageRun func(ctx *MsgContext) +type chatInputRun func(s *InterContext) + +type Category string type DetailedDescription struct { Usage string @@ -19,6 +21,7 @@ type Command struct { Aliases []string DetailedDescription *DetailedDescription discommand *DiscommandStruct + Category Category } type DiscommandStruct struct { @@ -28,6 +31,22 @@ type DiscommandStruct struct { chatInputRuns map[string]chatInputRun } +type MsgContext struct { + Session *discordgo.Session + Msg *discordgo.MessageCreate + Args []string +} + +type InterContext struct { + Session *discordgo.Session + Inter *discordgo.InteractionCreate +} + +const ( + Chattings Category = "채팅" + Generals Category = "일반" +) + func new() *DiscommandStruct { discommand := DiscommandStruct{ Commands: map[string]*Command{}, @@ -73,13 +92,13 @@ func (d *DiscommandStruct) addChatInputRun(name string, run chatInputRun) { d.chatInputRuns[name] = run } -func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate) { +func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) { // 더욱 나아진 - d.messageRuns[command](s, m) + d.messageRuns[command](&MsgContext{s, m, args}) } func (d *DiscommandStruct) ChatInputRun(command string, s *discordgo.Session, i *discordgo.InteractionCreate) { - d.chatInputRuns[command](s, i) + d.chatInputRuns[command](&InterContext{s, i}) } var Discommand *DiscommandStruct = new() diff --git a/commands/information.go b/commands/information.go index 2f25cb2..7e2dc28 100644 --- a/commands/information.go +++ b/commands/information.go @@ -16,6 +16,7 @@ var InformationCommand *Command = &Command{ DetailedDescription: &DetailedDescription{ Usage: "머핀아 정보", }, + Category: Generals, } func (c *Command) informationRun(s *discordgo.Session, m any) { @@ -65,10 +66,10 @@ func (c *Command) informationRun(s *discordgo.Session, m any) { } } -func (c *Command) informationMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { - c.informationRun(s, m) +func (c *Command) informationMessageRun(ctx *MsgContext) { + c.informationRun(ctx.Session, ctx.Msg) } -func (c *Command) informationChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) { - c.informationRun(s, i) +func (c *Command) informationChatInputRun(ctx *InterContext) { + c.informationRun(ctx.Session, ctx.Inter) } diff --git a/commands/learn.go b/commands/learn.go index 511b1d7..72bc5ce 100644 --- a/commands/learn.go +++ b/commands/learn.go @@ -9,9 +9,8 @@ import ( "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" + "github.com/bwmarrin/discordgo" ) var LearnCommand *Command = &Command{ @@ -42,6 +41,7 @@ var LearnCommand *Command = &Command{ "머핀아 배워 미간은_누구야? 이봇의_개발자요", }, }, + Category: Chattings, } func addPrefix(arr []string) (newArr []string) { @@ -51,44 +51,36 @@ func addPrefix(arr []string) (newArr []string) { return } -func (c *Command) learnRun(s *discordgo.Session, m any) { +func (c *Command) learnRun(s *discordgo.Session, m any, args *[]string) { 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"), - }, + if len(*args) < 2 { + s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{ + Title: "❌ 오류", + Description: "올바르지 않ㅇ은 용법이에요.", + Fields: []*discordgo.MessageEmbedField{ + { + Name: "사용법", + Value: utils.InlineCode(c.DetailedDescription.Usage), + Inline: true, }, - Color: int(utils.EFail), - }, m.Reference()) - return - } - } else { - command = matches[0][1] - result = matches[1][1] + { + Name: "예시", + Value: strings.Join(addPrefix(c.DetailedDescription.Examples), "\n"), + }, + }, + Color: int(utils.EFail), + }, m.Reference()) + return } + command = strings.ReplaceAll((*args)[0], "_", " ") + result = strings.ReplaceAll((*args)[1], "_", " ") case *discordgo.InteractionCreate: s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, @@ -206,10 +198,11 @@ func (c *Command) learnRun(s *discordgo.Session, m any) { } } -func (c *Command) learnMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { - c.learnRun(s, m) +func (c *Command) learnMessageRun(ctx *MsgContext) { + c.learnRun(ctx.Session, ctx.Msg, &ctx.Args) } -func (c *Command) learnChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) { - c.learnRun(s, i) +func (c *Command) learnChatInputRun(ctx *InterContext) { + var args *[]string + c.learnRun(ctx.Session, ctx.Inter, args) } diff --git a/commands/learnedDataList.go b/commands/learnedDataList.go index 67906f1..b17142a 100644 --- a/commands/learnedDataList.go +++ b/commands/learnedDataList.go @@ -23,6 +23,7 @@ var LearnedDataListCommand *Command = &Command{ DetailedDescription: &DetailedDescription{ Usage: "머핀아 리스트", }, + Category: Chattings, } func getDescriptions(datas *[]databases.Learn) (descriptions []string) { @@ -114,10 +115,10 @@ func (c *Command) learnedDataListRun(s *discordgo.Session, m any) { } } -func (c *Command) learnedDataListMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { - c.learnedDataListRun(s, m) +func (c *Command) learnedDataListMessageRun(ctx *MsgContext) { + c.learnedDataListRun(ctx.Session, ctx.Msg) } -func (c *Command) learnedDataListChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) { - c.learnedDataListRun(s, i) +func (c *Command) learnedDataListChatInputRun(ctx *InterContext) { + c.learnedDataListRun(ctx.Session, ctx.Inter) } diff --git a/handler/messageCreate.go b/handler/messageCreate.go index 1401949..db51036 100644 --- a/handler/messageCreate.go +++ b/handler/messageCreate.go @@ -16,6 +16,17 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo" ) +func argParser(content string) (args []string) { + for _, arg := range utils.FlexibleStringParser.FindAllStringSubmatch(content, -1) { + if arg[1] != "" { + args = append(args, arg[1]) + } else { + args = append(args, arg[0]) + } + } + return +} + // MessageCreate is handlers of messageCreate event func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { config := configs.Config @@ -25,7 +36,8 @@ 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[strings.Split(content, " ")[0]] + args := argParser(content) + command := commands.Discommand.Aliases[args[0]] if m.Author.ID == config.Train.UserID { if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{ @@ -104,7 +116,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - commands.Discommand.MessageRun(command, s, m) + commands.Discommand.MessageRun(command, s, m, args[1:]) return } else { return diff --git a/utils/regexp.go b/utils/regexp.go index 726e22e..1cd2002 100644 --- a/utils/regexp.go +++ b/utils/regexp.go @@ -2,5 +2,5 @@ package utils import "regexp" -var ExtractQuotedText *regexp.Regexp = regexp.MustCompile("[\"'`](.*?)[\"'`]") +var FlexibleStringParser *regexp.Regexp = regexp.MustCompile("[^\\s\"'「」«»]+|\"([^\"]*)\"|'([^']*)'|「([^」]*)」|«([^»]*)»") var Decimals *regexp.Regexp = regexp.MustCompile(`\d+`)