From 5f5c14df7ec41af8c2100e25554f6e803be31762 Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Wed, 2 Jul 2025 13:59:44 +0900 Subject: [PATCH] fix: error handling (maybe) --- commands/chat.go | 6 ++--- commands/dataLength.go | 16 ++++++------- commands/deleteLearnedData.go | 27 +++++++++++----------- commands/deregister.go | 8 +++---- commands/discommand.go | 40 ++++++++++++++++++++------------- commands/help.go | 18 +++++++-------- commands/information.go | 17 ++++++++------ commands/learn.go | 29 ++++++++++++------------ commands/learnedDataList.go | 30 ++++++++++++------------- commands/register.go | 14 ++++++------ commands/reloadPrompt.go | 13 +++-------- commands/switchMode.go | 4 ++-- components/deleteLearnedData.go | 9 +++++--- components/deregister.go | 28 ++++++++++------------- components/paginationEmbed.go | 5 ++++- components/register.go | 28 +++++++++-------------- handler/interactionCreate.go | 35 +++++++++++++++++++++++++---- handler/messageCreate.go | 14 +++++++++++- modals/paginationEmbed.go | 4 ++-- 19 files changed, 191 insertions(+), 154 deletions(-) diff --git a/commands/chat.go b/commands/chat.go index ec6a85e..7e2b0a2 100644 --- a/commands/chat.go +++ b/commands/chat.go @@ -29,7 +29,7 @@ var ChatCommand *Command = &Command{ RegisterApplicationCommand: true, RegisterMessageCommand: false, Flags: CommandFlagsIsRegistered, - ChatInputRun: func(ctx *ChatInputContext) { + ChatInputRun: func(ctx *ChatInputContext) error { i := ctx.Inter i.DeferReply(&discordgo.InteractionResponseData{}) @@ -39,11 +39,11 @@ var ChatCommand *Command = &Command{ i.EditReply(&utils.InteractionEdit{ Content: &str, }) - return + return nil } result := chatbot.ParseResult(str, ctx.Inter.Session, i) - i.EditReply(&utils.InteractionEdit{ + return i.EditReply(&utils.InteractionEdit{ Content: &result, }) }, diff --git a/commands/dataLength.go b/commands/dataLength.go index a380c5b..974cec8 100644 --- a/commands/dataLength.go +++ b/commands/dataLength.go @@ -43,17 +43,17 @@ var DataLengthCommand *Command = &Command{ }, Category: General, RegisterApplicationCommand: true, - RegisterMessageCommand: true, - Flags: CommandFlagsIsRegistered, - MessageRun: func(ctx *MsgContext) { - dataLengthRun(ctx.Msg.Session, ctx.Msg, ctx.Msg.Author.Username, ctx.Msg.Author.ID) + RegisterMessageCommand: true, + Flags: CommandFlagsIsRegistered, + MessageRun: func(ctx *MsgContext) error { + return dataLengthRun(ctx.Msg.Session, ctx.Msg, ctx.Msg.Author.Username, ctx.Msg.Author.ID) }, - ChatInputRun: func(ctx *ChatInputContext) { + ChatInputRun: func(ctx *ChatInputContext) error { ctx.Inter.DeferReply(&discordgo.InteractionResponseData{ Flags: discordgo.MessageFlagsEphemeral, }) - dataLengthRun(ctx.Inter.Session, ctx.Inter, ctx.Inter.Member.User.Username, ctx.Inter.Member.User.ID) + return dataLengthRun(ctx.Inter.Session, ctx.Inter, ctx.Inter.Member.User.Username, ctx.Inter.Member.User.ID) }, } @@ -74,7 +74,7 @@ func getLength(ch chan chStruct, dType dataType, coll *mongo.Collection, filter ch <- chStruct{name: dType, length: len(data)} } -func dataLengthRun(s *discordgo.Session, m any, username, userId string) { +func dataLengthRun(s *discordgo.Session, m any, username, userId string) error { ch := make(chan chStruct) var textLength, muffinLength, @@ -118,7 +118,7 @@ func dataLengthRun(s *discordgo.Session, m any, username, userId string) { sum := textLength + learnLength - utils.NewMessageSender(m). + return utils.NewMessageSender(m). AddComponents(discordgo.Container{ Components: []discordgo.MessageComponent{ discordgo.Section{ diff --git a/commands/deleteLearnedData.go b/commands/deleteLearnedData.go index 7cb037d..26fdff7 100644 --- a/commands/deleteLearnedData.go +++ b/commands/deleteLearnedData.go @@ -33,7 +33,7 @@ var DeleteLearnedDataCommand *Command = &Command{ RegisterApplicationCommand: true, RegisterMessageCommand: true, Flags: CommandFlagsIsRegistered, - MessageRun: func(ctx *MsgContext) { + MessageRun: func(ctx *MsgContext) error { command := strings.Join(*ctx.Args, " ") if command == "" { utils.NewMessageSender(ctx.Msg). @@ -51,13 +51,17 @@ var DeleteLearnedDataCommand *Command = &Command{ SetComponentsV2(true). SetReply(true). Send() + return nil } - deleteLearnedDataRun(ctx.Msg, strings.Join(*ctx.Args, " "), ctx.Msg.Author.ID) + return deleteLearnedDataRun(ctx.Msg, strings.Join(*ctx.Args, " "), ctx.Msg.Author.ID) }, - ChatInputRun: func(ctx *ChatInputContext) { - ctx.Inter.DeferReply(&discordgo.InteractionResponseData{ + ChatInputRun: func(ctx *ChatInputContext) error { + err := ctx.Inter.DeferReply(&discordgo.InteractionResponseData{ Flags: discordgo.MessageFlagsEphemeral, }) + if err != nil { + return err + } var command string @@ -65,23 +69,18 @@ var DeleteLearnedDataCommand *Command = &Command{ command = opt.StringValue() } - deleteLearnedDataRun(ctx.Inter, command, ctx.Inter.Member.User.ID) + return deleteLearnedDataRun(ctx.Inter, command, ctx.Inter.Member.User.ID) }, } -func deleteLearnedDataRun(m any, command, userId string) { +func deleteLearnedDataRun(m any, command, userId string) error { var data []databases.Learn var sections []discordgo.Section var containers []*discordgo.Container cur, err := databases.Database.Learns.Find(context.TODO(), bson.M{"user_id": userId, "command": command}) if err != nil { - utils.NewMessageSender(m). - AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "데이터를 가져오는데 실패했어요."})). - SetComponentsV2(true). - SetReply(true). - Send() - return + return err } cur.All(context.TODO(), &data) @@ -92,7 +91,7 @@ func deleteLearnedDataRun(m any, command, userId string) { SetComponentsV2(true). SetReply(true). Send() - return + return nil } for i, data := range data { @@ -127,7 +126,7 @@ func deleteLearnedDataRun(m any, command, userId string) { containers = append(containers, container) } - utils.PaginationEmbedBuilder(m). + return utils.PaginationEmbedBuilder(m). AddContainers(containers...). Start() } diff --git a/commands/deregister.go b/commands/deregister.go index 79677bb..4c3424b 100644 --- a/commands/deregister.go +++ b/commands/deregister.go @@ -20,13 +20,13 @@ var DeregisterCommand *Command = &Command{ RegisterMessageCommand: true, RegisterApplicationCommand: true, Flags: CommandFlagsIsRegistered, - MessageRun: func(ctx *MsgContext) { - deregisterRun(ctx.Msg, ctx.Msg.Author.ID, ctx.Msg.Session.State.User.Username) + MessageRun: func(ctx *MsgContext) error { + return deregisterRun(ctx.Msg, ctx.Msg.Author.ID, ctx.Msg.Session.State.User.Username) }, } -func deregisterRun(m any, userId, botName string) { - utils.NewMessageSender(m). +func deregisterRun(m any, userId, botName string) error { + return utils.NewMessageSender(m). AddComponents(discordgo.Container{ Components: []discordgo.MessageComponent{ discordgo.TextDisplay{ diff --git a/commands/discommand.go b/commands/discommand.go index 6caeb36..0c6f874 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -9,10 +9,10 @@ import ( "github.com/bwmarrin/discordgo" ) -type modalRun func(ctx *ModalContext) -type messageRun func(ctx *MsgContext) -type chatInputRun func(ctx *ChatInputContext) -type componentRun func(ctx *ComponentContext) +type modalRun func(ctx *ModalContext) error +type messageRun func(ctx *MsgContext) error +type chatInputRun func(ctx *ChatInputContext) error +type componentRun func(ctx *ComponentContext) error type modalParse func(ctx *ModalContext) bool type componentParse func(ctx *ComponentContext) bool @@ -126,7 +126,7 @@ func (d *DiscommandStruct) LoadModal(m *Modal) { d.Modals = append(d.Modals, m) } -func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, msg *discordgo.MessageCreate, args []string) { +func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, msg *discordgo.MessageCreate, args []string) error { m := &utils.MessageCreate{ MessageCreate: msg, Session: s, @@ -139,7 +139,7 @@ func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, msg *di SetComponentsV2(true). SetReply(true). Send() - return + return nil } if command.Flags&CommandFlagsIsRegistered != 0 && !databases.Database.IsUser(m.Author.ID) { @@ -148,14 +148,15 @@ func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, msg *di SetComponentsV2(true). SetReply(true). Send() - return + return nil } - command.MessageRun(&MsgContext{m, &args, command}) + return command.MessageRun(&MsgContext{m, &args, command}) } + return nil } -func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, inter *discordgo.InteractionCreate) { +func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, inter *discordgo.InteractionCreate) error { i := &utils.InteractionCreate{ InteractionCreate: inter, Session: s, @@ -172,7 +173,7 @@ func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, inter SetEphemeral(true). SetReply(true). Send() - return + return nil } if command.Flags&CommandFlagsIsRegistered != 0 && !databases.Database.IsUser(i.User.ID) { @@ -182,14 +183,17 @@ func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, inter SetEphemeral(true). SetReply(true). Send() - return + return nil } - command.ChatInputRun(&ChatInputContext{i, command}) + return command.ChatInputRun(&ChatInputContext{i, command}) } + return nil } -func (d *DiscommandStruct) ComponentRun(s *discordgo.Session, inter *discordgo.InteractionCreate) { +func (d *DiscommandStruct) ComponentRun(s *discordgo.Session, inter *discordgo.InteractionCreate) error { + var err error + i := &utils.InteractionCreate{ InteractionCreate: inter, Session: s, @@ -207,12 +211,15 @@ func (d *DiscommandStruct) ComponentRun(s *discordgo.Session, inter *discordgo.I continue } - c.Run(data) + err = c.Run(data) break } + return err } -func (d *DiscommandStruct) ModalRun(s *discordgo.Session, i *discordgo.InteractionCreate) { +func (d *DiscommandStruct) ModalRun(s *discordgo.Session, i *discordgo.InteractionCreate) error { + var err error + data := &ModalContext{ Inter: &utils.InteractionCreate{ InteractionCreate: i, @@ -227,7 +234,8 @@ func (d *DiscommandStruct) ModalRun(s *discordgo.Session, i *discordgo.Interacti continue } - m.Run(data) + err = m.Run(data) break } + return err } diff --git a/commands/help.go b/commands/help.go index c92a574..075a3cb 100644 --- a/commands/help.go +++ b/commands/help.go @@ -31,17 +31,17 @@ var HelpCommand *Command = &Command{ Category: General, RegisterApplicationCommand: true, RegisterMessageCommand: true, - MessageRun: func(ctx *MsgContext) { - helpRun(ctx.Msg.Session, ctx.Msg, strings.Join(*ctx.Args, " ")) + MessageRun: func(ctx *MsgContext) error { + return helpRun(ctx.Msg.Session, ctx.Msg, strings.Join(*ctx.Args, " ")) }, - ChatInputRun: func(ctx *ChatInputContext) { + ChatInputRun: func(ctx *ChatInputContext) error { var command string if opt, ok := ctx.Inter.Options["명령어"]; ok { command = opt.StringValue() } - helpRun(ctx.Inter.Session, ctx.Inter, command) + return helpRun(ctx.Inter.Session, ctx.Inter, command) }, } @@ -55,7 +55,7 @@ func getCommandsByCategory(d *DiscommandStruct, category Category) []string { return commands } -func helpRun(s *discordgo.Session, m any, commandName string) { +func helpRun(s *discordgo.Session, m any, commandName string) error { section := &discordgo.Section{ Accessory: discordgo.Thumbnail{ Media: discordgo.UnfurledMediaItem{ @@ -78,14 +78,13 @@ func helpRun(s *discordgo.Session, m any, commandName string) { Content: fmt.Sprintf("- **채팅**\n%s", strings.Join(getCommandsByCategory(Discommand, Chatting), "\n")), }, ) - utils.NewMessageSender(m). + return utils.NewMessageSender(m). AddComponents(&discordgo.Container{ Components: []discordgo.MessageComponent{section}, }). SetComponentsV2(true). SetReply(true). Send() - return } var aliases, examples discordgo.TextDisplay @@ -128,17 +127,16 @@ func helpRun(s *discordgo.Session, m any, commandName string) { learnArgs := discordgo.TextDisplay{ Content: fmt.Sprintf("- **대답에 쓸 수 있는 인자**\n%s", learnArguments), } - utils.NewMessageSender(m). + return utils.NewMessageSender(m). AddComponents(discordgo.Container{ Components: []discordgo.MessageComponent{section, aliases, examples, learnArgs}, }). SetComponentsV2(true). SetReply(true). Send() - return } - utils.NewMessageSender(m). + return utils.NewMessageSender(m). AddComponents(discordgo.Container{ Components: []discordgo.MessageComponent{section, aliases, examples}, }). diff --git a/commands/information.go b/commands/information.go index c981d51..9b2a972 100644 --- a/commands/information.go +++ b/commands/information.go @@ -19,17 +19,20 @@ var InformationCommand *Command = &Command{ Category: General, RegisterApplicationCommand: true, RegisterMessageCommand: true, - MessageRun: func(ctx *MsgContext) { - informationRun(ctx.Msg.Session, ctx.Msg) + MessageRun: func(ctx *MsgContext) error { + return informationRun(ctx.Msg.Session, ctx.Msg) }, - ChatInputRun: func(ctx *ChatInputContext) { - informationRun(ctx.Inter.Session, ctx.Inter) + ChatInputRun: func(ctx *ChatInputContext) error { + return informationRun(ctx.Inter.Session, ctx.Inter) }, } -func informationRun(s *discordgo.Session, m any) { - owner, _ := s.User(configs.Config.Bot.OwnerId) - utils.NewMessageSender(m). +func informationRun(s *discordgo.Session, m any) error { + owner, err := s.User(configs.Config.Bot.OwnerId) + if err != nil { + return err + } + return utils.NewMessageSender(m). AddComponents(discordgo.Container{ Components: []discordgo.MessageComponent{ discordgo.Section{ diff --git a/commands/learn.go b/commands/learn.go index 4c2be1c..4f3b23c 100644 --- a/commands/learn.go +++ b/commands/learn.go @@ -3,7 +3,6 @@ package commands import ( "context" "fmt" - "log" "strings" "time" @@ -60,7 +59,7 @@ var LearnCommand *Command = &Command{ RegisterApplicationCommand: true, RegisterMessageCommand: true, Flags: CommandFlagsIsRegistered, - MessageRun: func(ctx *MsgContext) { + MessageRun: func(ctx *MsgContext) error { if len(*ctx.Args) < 2 { utils.NewMessageSender(ctx.Msg). AddComponents(utils.GetErrorContainer( @@ -80,15 +79,18 @@ var LearnCommand *Command = &Command{ SetComponentsV2(true). SetReply(true). Send() - return + return nil } - learnRun(ctx.Msg, ctx.Msg.Author.ID, strings.ReplaceAll((*ctx.Args)[0], "_", " "), strings.ReplaceAll((*ctx.Args)[1], "_", " ")) + return learnRun(ctx.Msg, ctx.Msg.Author.ID, strings.ReplaceAll((*ctx.Args)[0], "_", " "), strings.ReplaceAll((*ctx.Args)[1], "_", " ")) }, - ChatInputRun: func(ctx *ChatInputContext) { - ctx.Inter.DeferReply(&discordgo.InteractionResponseData{ + ChatInputRun: func(ctx *ChatInputContext) error { + err := ctx.Inter.DeferReply(&discordgo.InteractionResponseData{ Flags: discordgo.MessageFlagsEphemeral, }) + if err != nil { + return err + } var command, result string @@ -100,11 +102,11 @@ var LearnCommand *Command = &Command{ result = opt.StringValue() } - learnRun(ctx.Inter, ctx.Inter.Member.User.ID, command, result) + return learnRun(ctx.Inter, ctx.Inter.Member.User.ID, command, result) }, } -func learnRun(m any, userId, command, result string) { +func learnRun(m any, userId, command, result string) error { igCommands := []string{} for _, command := range Discommand.Commands { @@ -128,7 +130,7 @@ func learnRun(m any, userId, command, result string) { SetComponentsV2(true). SetReply(true). Send() - return + return nil } } @@ -139,7 +141,7 @@ func learnRun(m any, userId, command, result string) { SetComponentsV2(true). SetReply(true). Send() - return + return nil } } @@ -149,6 +151,7 @@ func learnRun(m any, userId, command, result string) { SetComponentsV2(true). SetReply(true). Send() + return nil } _, err := databases.Database.Learns.InsertOne(context.TODO(), databases.InsertLearn{ @@ -158,17 +161,15 @@ func learnRun(m any, userId, command, result string) { CreatedAt: time.Now(), }) if err != nil { - log.Println(err) - utils.NewMessageSender(m). AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "단어를 배우는데 오류가 생겼어요."})). SetComponentsV2(true). SetReply(true). Send() - return + return err } - utils.NewMessageSender(m). + return utils.NewMessageSender(m). AddComponents(utils.GetSuccessContainer( discordgo.TextDisplay{ Content: fmt.Sprintf("%s 배웠어요.", hangul.GetJosa(command, hangul.EUL_REUL)), diff --git a/commands/learnedDataList.go b/commands/learnedDataList.go index f42f753..60bf19a 100644 --- a/commands/learnedDataList.go +++ b/commands/learnedDataList.go @@ -54,7 +54,7 @@ var LearnedDataListCommand *Command = &Command{ RegisterApplicationCommand: true, RegisterMessageCommand: true, Flags: CommandFlagsIsRegistered, - MessageRun: func(ctx *MsgContext) { + MessageRun: func(ctx *MsgContext) error { var length int filter := bson.D{{Key: "user_id", Value: ctx.Msg.Author.ID}} @@ -76,7 +76,7 @@ var LearnedDataListCommand *Command = &Command{ SetComponentsV2(true). SetReply(true). Send() - return + return nil } if float64(length) > LIST_MAX_VALUE { @@ -85,15 +85,18 @@ var LearnedDataListCommand *Command = &Command{ SetComponentsV2(true). SetReply(true). Send() - return + return nil } } - learnedDataListRun(ctx.Msg, ctx.Msg.Author.GlobalName, ctx.Msg.Author.AvatarURL("512"), filter, length) + return learnedDataListRun(ctx.Msg, ctx.Msg.Author.GlobalName, ctx.Msg.Author.AvatarURL("512"), filter, length) }, - ChatInputRun: func(ctx *ChatInputContext) { - ctx.Inter.DeferReply(&discordgo.InteractionResponseData{ + ChatInputRun: func(ctx *ChatInputContext) error { + err := ctx.Inter.DeferReply(&discordgo.InteractionResponseData{ Flags: discordgo.MessageFlagsEphemeral, }) + if err != nil { + return err + } var length int @@ -109,7 +112,7 @@ var LearnedDataListCommand *Command = &Command{ if opt, ok := ctx.Inter.Options["개수"]; ok { length = int(opt.IntValue()) } - learnedDataListRun(ctx.Inter, ctx.Inter.Member.User.GlobalName, ctx.Inter.Member.User.AvatarURL("512"), filter, length) + return learnedDataListRun(ctx.Inter, ctx.Inter.Member.User.GlobalName, ctx.Inter.Member.User.AvatarURL("512"), filter, length) }, } @@ -173,7 +176,7 @@ func getContainers(accessory *discordgo.Thumbnail, defaultDesc string, items []s return containers } -func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, length int) { +func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, length int) error { var data []databases.Learn itemsMap := map[string]string{} @@ -187,17 +190,15 @@ func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, leng SetComponentsV2(true). SetReply(true). Send() - return + return nil } - fmt.Println(err) - utils.NewMessageSender(m). AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "데이터를 가져오는데 실패했어요."})). SetComponentsV2(true). SetReply(true). Send() - return + return err } defer cur.Close(context.TODO()) @@ -217,10 +218,9 @@ func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, leng }, }, fmt.Sprintf("### %s님이 알려주신 지식\n- **%s**\n", globalName, command)+"%s", items, length) - utils.PaginationEmbedBuilder(m). + return utils.PaginationEmbedBuilder(m). AddContainers(containers...). Start() - return } for _, data := range data { @@ -241,7 +241,7 @@ func learnedDataListRun(m any, globalName, avatarUrl string, filter bson.D, leng }, }, fmt.Sprintf("### %s님이 알려주신 지식\n총 %d개에요.\n", globalName, len(items))+"%s", items, length) - utils.PaginationEmbedBuilder(m). + return utils.PaginationEmbedBuilder(m). AddContainers(containers...). Start() } diff --git a/commands/register.go b/commands/register.go index 7882fd5..a1fd972 100644 --- a/commands/register.go +++ b/commands/register.go @@ -20,25 +20,25 @@ var RegisterCommand *Command = &Command{ Category: General, RegisterMessageCommand: true, RegisterApplicationCommand: true, - MessageRun: func(ctx *MsgContext) { - registerRun(ctx.Msg, ctx.Msg.Author.ID, ctx.Msg.Session.State.User.Username) + MessageRun: func(ctx *MsgContext) error { + return registerRun(ctx.Msg, ctx.Msg.Author.ID, ctx.Msg.Session.State.User.Username) }, - ChatInputRun: func(ctx *ChatInputContext) { - registerRun(ctx.Inter, ctx.Inter.User.ID, ctx.Inter.Session.State.User.Username) + ChatInputRun: func(ctx *ChatInputContext) error { + return registerRun(ctx.Inter, ctx.Inter.User.ID, ctx.Inter.Session.State.User.Username) }, } -func registerRun(m any, userId, botName string) { +func registerRun(m any, userId, botName string) error { if databases.Database.IsUser(userId) { utils.NewMessageSender(m). AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: fmt.Sprintf("당신은 이미 가입되어있어요. 만약 탈퇴를 원하시면 %s탈퇴를 이용해주세요.", configs.Config.Bot.Prefix)})). SetComponentsV2(true). SetReply(true). Send() - return + return nil } - utils.NewMessageSender(m). + return utils.NewMessageSender(m). AddComponents(discordgo.Container{ Components: []discordgo.MessageComponent{ discordgo.TextDisplay{ diff --git a/commands/reloadPrompt.go b/commands/reloadPrompt.go index 0681b95..b939737 100644 --- a/commands/reloadPrompt.go +++ b/commands/reloadPrompt.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "log" "git.wh64.net/muffin/goMuffin/chatbot" "git.wh64.net/muffin/goMuffin/configs" @@ -22,19 +21,13 @@ var ReloadPromptCommand *Command = &Command{ RegisterApplicationCommand: false, RegisterMessageCommand: true, Flags: CommandFlagsIsDeveloper, - MessageRun: func(ctx *MsgContext) { + MessageRun: func(ctx *MsgContext) error { err := chatbot.ChatBot.ReloadPrompt() if err != nil { - log.Fatalln(err) - utils.NewMessageSender(ctx.Msg). - AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "프롬프트를 다시 불러오는 데 문제가 생겼어요."})). - SetComponentsV2(true). - SetReply(true). - Send() - return + return err } - utils.NewMessageSender(ctx.Msg). + return utils.NewMessageSender(ctx.Msg). AddComponents(utils.GetSuccessContainer(discordgo.TextDisplay{Content: "프롬프트를 다시 불러왔어요."})). SetComponentsV2(true). SetReply(true). diff --git a/commands/switchMode.go b/commands/switchMode.go index 4888b56..60dd5b3 100644 --- a/commands/switchMode.go +++ b/commands/switchMode.go @@ -21,10 +21,10 @@ var SwitchModeCommand *Command = &Command{ RegisterApplicationCommand: false, RegisterMessageCommand: true, Flags: CommandFlagsIsDeveloper, - MessageRun: func(ctx *MsgContext) { + MessageRun: func(ctx *MsgContext) error { chatbot.ChatBot.SwitchMode() - utils.NewMessageSender(ctx.Msg). + return utils.NewMessageSender(ctx.Msg). AddComponents(utils.GetSuccessContainer(discordgo.TextDisplay{Content: fmt.Sprintf("모드를 %s로 바꾸었어요.", chatbot.ChatBot.ModeString())})). SetComponentsV2(true). SetReply(true). diff --git a/components/deleteLearnedData.go b/components/deleteLearnedData.go index 723765c..6217b52 100644 --- a/components/deleteLearnedData.go +++ b/components/deleteLearnedData.go @@ -34,10 +34,13 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{ } return true }, - Run: func(ctx *commands.ComponentContext) { + Run: func(ctx *commands.ComponentContext) error { i := ctx.Inter - i.DeferUpdate() + err := i.DeferUpdate() + if err != nil { + return err + } id, itemId := utils.GetDeleteLearnedDataId(i.MessageComponentData().CustomID) fmt.Println(id, itemId) @@ -45,7 +48,7 @@ var DeleteLearnedDataComponent *commands.Component = &commands.Component{ databases.Database.Learns.DeleteOne(context.TODO(), bson.D{{Key: "_id", Value: id}}) flags := discordgo.MessageFlagsIsComponentsV2 - i.EditReply(&utils.InteractionEdit{ + return i.EditReply(&utils.InteractionEdit{ Flags: &flags, Components: &[]discordgo.MessageComponent{ utils.GetSuccessContainer(discordgo.TextDisplay{Content: fmt.Sprintf("%d번을 삭ㅈ제했어요.", itemId)}), diff --git a/components/deregister.go b/components/deregister.go index 07224bd..9fa12d8 100644 --- a/components/deregister.go +++ b/components/deregister.go @@ -2,7 +2,6 @@ package components import ( "context" - "log" "strings" "git.wh64.net/muffin/goMuffin/commands" @@ -24,8 +23,12 @@ var DeregisterComponent *commands.Component = &commands.Component{ } return true }, - Run: func(ctx *commands.ComponentContext) { - ctx.Inter.DeferUpdate() + Run: func(ctx *commands.ComponentContext) error { + err := ctx.Inter.DeferUpdate() + if err != nil { + return err + } + customId := ctx.Inter.MessageComponentData().CustomID flags := discordgo.MessageFlagsIsComponentsV2 @@ -34,26 +37,20 @@ var DeregisterComponent *commands.Component = &commands.Component{ filter := bson.D{{Key: "user_id", Value: ctx.Inter.User.ID}} _, err := databases.Database.Users.DeleteOne(context.TODO(), filter) if err != nil { - log.Println(err) - // 나중에 에러처리 바꿔야지 :( - return + return err } _, err = databases.Database.Learns.DeleteMany(context.TODO(), filter) if err != nil { - log.Println(err) - // 나중에 에러처리 바꿔야지 :( - return + return err } _, err = databases.Database.Memory.DeleteMany(context.TODO(), filter) if err != nil { - log.Println(err) - // 나중에 에러처리 바꿔야지 :( - return + return err } - ctx.Inter.EditReply(&utils.InteractionEdit{ + return ctx.Inter.EditReply(&utils.InteractionEdit{ Flags: &flags, Components: &[]discordgo.MessageComponent{ utils.GetSuccessContainer(discordgo.TextDisplay{ @@ -61,9 +58,8 @@ var DeregisterComponent *commands.Component = &commands.Component{ }), }, }) - return case strings.HasPrefix(customId, utils.DeregisterDisagree): - ctx.Inter.EditReply(&utils.InteractionEdit{ + return ctx.Inter.EditReply(&utils.InteractionEdit{ Flags: &flags, Components: &[]discordgo.MessageComponent{ utils.GetDeclineContainer(discordgo.TextDisplay{ @@ -71,7 +67,7 @@ var DeregisterComponent *commands.Component = &commands.Component{ }), }, }) - return } + return nil }, } diff --git a/components/paginationEmbed.go b/components/paginationEmbed.go index f2175aa..e6ca26a 100644 --- a/components/paginationEmbed.go +++ b/components/paginationEmbed.go @@ -33,17 +33,20 @@ var PaginationEmbedComponent *commands.Component = &commands.Component{ } return true }, - Run: func(ctx *commands.ComponentContext) { + Run: func(ctx *commands.ComponentContext) error { customId := ctx.Inter.MessageComponentData().CustomID id := utils.GetPaginationEmbedId(customId) p := utils.GetPaginationEmbed(id) if strings.HasPrefix(customId, utils.PaginationEmbedPrev) { p.Prev(ctx.Inter) + return nil } else if strings.HasPrefix(customId, utils.PaginationEmbedNext) { p.Next(ctx.Inter) + return nil } else { p.ShowModal(ctx.Inter) + return nil } }, } diff --git a/components/register.go b/components/register.go index c4a2757..a8bcf0b 100644 --- a/components/register.go +++ b/components/register.go @@ -3,7 +3,7 @@ package components import ( "context" "fmt" - "log" + "strings" "time" @@ -25,8 +25,12 @@ var RegisterComponent *commands.Component = &commands.Component{ } return true }, - Run: func(ctx *commands.ComponentContext) { - ctx.Inter.DeferUpdate() + Run: func(ctx *commands.ComponentContext) error { + err := ctx.Inter.DeferUpdate() + if err != nil { + return err + } + customId := ctx.Inter.MessageComponentData().CustomID flags := discordgo.MessageFlagsIsComponentsV2 @@ -37,19 +41,10 @@ var RegisterComponent *commands.Component = &commands.Component{ CreatedAt: time.Now(), }) if err != nil { - log.Println(err) - ctx.Inter.EditReply(&utils.InteractionEdit{ - Flags: &flags, - Components: &[]discordgo.MessageComponent{ - utils.GetErrorContainer(discordgo.TextDisplay{ - Content: "가입을 하다가 오류가 생겼어요.", - }), - }, - }) - return + return err } - ctx.Inter.EditReply(&utils.InteractionEdit{ + return ctx.Inter.EditReply(&utils.InteractionEdit{ Flags: &flags, Components: &[]discordgo.MessageComponent{ utils.GetSuccessContainer(discordgo.TextDisplay{ @@ -57,9 +52,8 @@ var RegisterComponent *commands.Component = &commands.Component{ }), }, }) - return case strings.HasPrefix(customId, utils.ServiceDisagree): - ctx.Inter.EditReply(&utils.InteractionEdit{ + return ctx.Inter.EditReply(&utils.InteractionEdit{ Flags: &flags, Components: &[]discordgo.MessageComponent{ utils.GetDeclineContainer(discordgo.TextDisplay{ @@ -67,7 +61,7 @@ var RegisterComponent *commands.Component = &commands.Component{ }), }, }) - return } + return nil }, } diff --git a/handler/interactionCreate.go b/handler/interactionCreate.go index 224b70a..ae58647 100644 --- a/handler/interactionCreate.go +++ b/handler/interactionCreate.go @@ -1,18 +1,45 @@ package handler import ( + "fmt" + "log" + "git.wh64.net/muffin/goMuffin/commands" + "git.wh64.net/muffin/goMuffin/configs" + "git.wh64.net/muffin/goMuffin/utils" "github.com/bwmarrin/discordgo" ) func InteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { + var err error switch i.Type { case discordgo.InteractionApplicationCommand: - commands.Discommand.ChatInputRun(i.ApplicationCommandData().Name, s, i) - return + err = commands.Discommand.ChatInputRun(i.ApplicationCommandData().Name, s, i) + if err != nil { + goto ErrMsg + } case discordgo.InteractionMessageComponent: - commands.Discommand.ComponentRun(s, i) + err = commands.Discommand.ComponentRun(s, i) + if err != nil { + goto ErrMsg + } case discordgo.InteractionModalSubmit: - commands.Discommand.ModalRun(s, i) + err = commands.Discommand.ModalRun(s, i) + if err != nil { + goto ErrMsg + } } + + // 아 몰라 goto 쓸래 +ErrMsg: + log.Println(err) + owner, _ := s.User(configs.Config.Bot.OwnerId) + utils.NewMessageSender(&utils.InteractionCreate{ + InteractionCreate: i, + Session: s, + }). + AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: fmt.Sprintf("오류가 발생하였어요. 만약 계속 발생한다면, %s으로 연락해주세요.", utils.InlineCode(owner.Username))})). + SetComponentsV2(true). + SetReply(true). + Send() } diff --git a/handler/messageCreate.go b/handler/messageCreate.go index 926ba35..afebb15 100644 --- a/handler/messageCreate.go +++ b/handler/messageCreate.go @@ -75,7 +75,19 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - commands.Discommand.MessageRun(command, s, m, args[1:]) + err := commands.Discommand.MessageRun(command, s, m, args[1:]) + if err != nil { + log.Println(err) + utils.NewMessageSender(&utils.MessageCreate{ + MessageCreate: m, + Session: s, + }). + AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "오류가 발생하였어요. 만약 계속 발생한다면, `migan.`으로 연락해주세요."})). + SetComponentsV2(true). + SetReply(true). + Send() + return + } return } else { if m.Author.ID == config.Chatbot.Train.UserId { diff --git a/modals/paginationEmbed.go b/modals/paginationEmbed.go index 3f30342..44eb983 100644 --- a/modals/paginationEmbed.go +++ b/modals/paginationEmbed.go @@ -52,7 +52,7 @@ var PaginationEmbedModal *commands.Modal = &commands.Modal{ return true }, - Run: func(ctx *commands.ModalContext) { + Run: func(ctx *commands.ModalContext) error { data := ctx.Inter.ModalSubmitData() customId := data.CustomID id := utils.GetPaginationEmbedId(customId) @@ -61,6 +61,6 @@ var PaginationEmbedModal *commands.Modal = &commands.Modal{ page, _ := strconv.Atoi(cmp.Value) - p.Set(ctx.Inter, page) + return p.Set(ctx.Inter, page) }, }