diff --git a/chatbot/parser.go b/chatbot/parser.go index 915a9b0..04e7a7a 100644 --- a/chatbot/parser.go +++ b/chatbot/parser.go @@ -2,22 +2,38 @@ package chatbot import ( "strings" + "time" "git.wh64.net/muffin/goMuffin/configs" "git.wh64.net/muffin/goMuffin/utils" "github.com/bwmarrin/discordgo" ) -func ParseResult(content string, s *discordgo.Session, m *discordgo.MessageCreate) string { +func ParseResult(content string, s *discordgo.Session, m any) string { result := content - userCreatedAt, _ := discordgo.SnowflakeTimestamp(m.Author.ID) - result = strings.ReplaceAll(result, "{user.name}", m.Author.Username) - result = strings.ReplaceAll(result, "{user.mention}", m.Author.Mention()) - result = strings.ReplaceAll(result, "{user.globalName}", m.Author.GlobalName) - result = strings.ReplaceAll(result, "{user.id}", m.Author.ID) + var user *discordgo.User + var joinedAt *time.Time + + switch m := m.(type) { + case *discordgo.MessageCreate: + case *utils.MessageCreate: + user = m.Author + joinedAt = &m.Member.JoinedAt + case *discordgo.IntegrationCreate: + case *utils.InteractionCreate: + user = m.Member.User + joinedAt = &m.Member.JoinedAt + } + + userCreatedAt, _ := discordgo.SnowflakeTimestamp(user.ID) + + result = strings.ReplaceAll(result, "{user.name}", user.Username) + result = strings.ReplaceAll(result, "{user.mention}", user.Mention()) + result = strings.ReplaceAll(result, "{user.globalName}", user.GlobalName) + result = strings.ReplaceAll(result, "{user.id}", user.ID) result = strings.ReplaceAll(result, "{user.createdAt}", utils.Time(&userCreatedAt, utils.RelativeTime)) - result = strings.ReplaceAll(result, "{user.joinedAt}", utils.Time(&m.Member.JoinedAt, utils.RelativeTime)) + result = strings.ReplaceAll(result, "{user.joinedAt}", utils.Time(joinedAt, utils.RelativeTime)) result = strings.ReplaceAll(result, "{muffin.version}", configs.MUFFIN_VERSION) result = strings.ReplaceAll(result, "{muffin.updatedAt}", utils.Time(configs.UpdatedAt, utils.RelativeTime)) diff --git a/commands/chat.go b/commands/chat.go new file mode 100644 index 0000000..a1ebbbd --- /dev/null +++ b/commands/chat.go @@ -0,0 +1,38 @@ +package commands + +import ( + "git.wh64.net/muffin/goMuffin/chatbot" + "git.wh64.net/muffin/goMuffin/utils" + "github.com/bwmarrin/discordgo" +) + +var ChatCommand *Command = &Command{ + ApplicationCommand: &discordgo.ApplicationCommand{ + Name: "대화", + Description: "이 봇이랑 대화해요. (슬래시 커맨드 전용)", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "내용", + Description: "대화할 내용", + Required: true, + }, + }, + }, + DetailedDescription: &DetailedDescription{ + Usage: "/대화 (내용:대화할 내용)", + Examples: []string{"/대화 내용:안녕", "/대화 내용:냠냠"}, + }, + Category: Chatting, + RegisterApplicationCommand: true, + RegisterMessageCommand: false, + ChatInputRun: func(ctx *ChatInputContext) { + i := ctx.Inter + i.DeferReply(&discordgo.InteractionResponseData{}) + + result := chatbot.ParseResult(chatbot.ChatBot.GetResponse(i.Member.User.ID, i.Options["내용"].StringValue()), ctx.Inter.Session, i) + i.EditReply(&utils.InteractionEdit{ + Content: &result, + }) + }, +} diff --git a/commands/dataLength.go b/commands/dataLength.go index 41990f0..4489cbb 100644 --- a/commands/dataLength.go +++ b/commands/dataLength.go @@ -43,6 +43,7 @@ var DataLengthCommand *Command = &Command{ }, Category: General, RegisterApplicationCommand: true, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { dataLengthRun(ctx.Msg.Session, ctx.Msg, ctx.Msg.Author.Username, ctx.Msg.Author.ID) }, diff --git a/commands/deleteLearnedData.go b/commands/deleteLearnedData.go index ec55d32..6a2fda1 100644 --- a/commands/deleteLearnedData.go +++ b/commands/deleteLearnedData.go @@ -31,6 +31,7 @@ var DeleteLearnedDataCommand *Command = &Command{ }, Category: Chatting, RegisterApplicationCommand: true, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { command := strings.Join(*ctx.Args, " ") if command == "" { diff --git a/commands/discommand.go b/commands/discommand.go index b1f28f8..fe30ed4 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -29,6 +29,7 @@ type Command struct { DetailedDescription *DetailedDescription Category Category RegisterApplicationCommand bool + RegisterMessageCommand bool MessageRun messageRun ChatInputRun chatInputRun } @@ -128,6 +129,10 @@ func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *disc return } + if !command.RegisterMessageCommand { + return + } + command.MessageRun(&MsgContext{&utils.MessageCreate{ MessageCreate: m, Session: s, @@ -136,7 +141,7 @@ func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *disc } func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, i *discordgo.InteractionCreate) { - if command, ok := d.Commands[name]; ok { + if command, ok := d.Commands[name]; ok && command.RegisterApplicationCommand { command.ChatInputRun(&ChatInputContext{&utils.InteractionCreate{ InteractionCreate: i, Session: s, diff --git a/commands/help.go b/commands/help.go index c4c21df..c92a574 100644 --- a/commands/help.go +++ b/commands/help.go @@ -30,6 +30,7 @@ var HelpCommand *Command = &Command{ }, Category: General, RegisterApplicationCommand: true, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { helpRun(ctx.Msg.Session, ctx.Msg, strings.Join(*ctx.Args, " ")) }, diff --git a/commands/information.go b/commands/information.go index 8a3251c..c981d51 100644 --- a/commands/information.go +++ b/commands/information.go @@ -18,6 +18,7 @@ var InformationCommand *Command = &Command{ }, Category: General, RegisterApplicationCommand: true, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { informationRun(ctx.Msg.Session, ctx.Msg) }, diff --git a/commands/learn.go b/commands/learn.go index 81580b7..588e1e6 100644 --- a/commands/learn.go +++ b/commands/learn.go @@ -58,6 +58,7 @@ var LearnCommand *Command = &Command{ }, Category: Chatting, RegisterApplicationCommand: true, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { if len(*ctx.Args) < 2 { utils.NewMessageSender(ctx.Msg). diff --git a/commands/learnedDataList.go b/commands/learnedDataList.go index 537b8fa..f31a4a5 100644 --- a/commands/learnedDataList.go +++ b/commands/learnedDataList.go @@ -58,6 +58,7 @@ var LearnedDataListCommand *Command = &Command{ }, Category: Chatting, RegisterApplicationCommand: true, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { var length int diff --git a/commands/reloadPrompt.go b/commands/reloadPrompt.go index 3b7ae34..3c4a033 100644 --- a/commands/reloadPrompt.go +++ b/commands/reloadPrompt.go @@ -20,6 +20,7 @@ var ReloadPromptCommand *Command = &Command{ }, Category: DeveloperOnly, RegisterApplicationCommand: false, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { err := chatbot.ChatBot.ReloadPrompt() if err != nil { diff --git a/commands/switchMode.go b/commands/switchMode.go index 653151e..673668e 100644 --- a/commands/switchMode.go +++ b/commands/switchMode.go @@ -19,6 +19,7 @@ var SwitchModeCommand *Command = &Command{ }, Category: DeveloperOnly, RegisterApplicationCommand: false, + RegisterMessageCommand: true, MessageRun: func(ctx *MsgContext) { chatbot.ChatBot.SwitchMode() diff --git a/handler/messageCreate.go b/handler/messageCreate.go index 9f7d40c..b130a30 100644 --- a/handler/messageCreate.go +++ b/handler/messageCreate.go @@ -37,10 +37,10 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { args := argParser(content) command := commands.Discommand.Aliases[args[0]] - if command == "" { + if command == "" || command == "대화" { s.ChannelTyping(m.ChannelID) - result := chatbot.ParseResult(chatbot.ChatBot.GetResponse(m.Author.ID, content), s, m) + result := chatbot.ParseResult(chatbot.ChatBot.GetResponse(m.Author.ID, strings.TrimPrefix(content, "대화 ")), s, m) utils.NewMessageSender(&utils.MessageCreate{ MessageCreate: m, Session: s, diff --git a/main.go b/main.go index c6bff8d..1869ee5 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ func init() { go commands.Discommand.LoadCommand(commands.DeleteLearnedDataCommand) go commands.Discommand.LoadCommand(commands.ReloadPromptCommand) go commands.Discommand.LoadCommand(commands.SwitchModeCommand) + go commands.Discommand.LoadCommand(commands.ChatCommand) go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent) go commands.Discommand.LoadComponent(components.PaginationEmbedComponent)