From e9cef78d54ee2233a4a09bc98a5e18ae724a3e13 Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Wed, 26 Mar 2025 21:14:28 +0900 Subject: [PATCH] feat: Add dataLength command's defer cur.Close() & Add pointer --- commands/dataLength.go | 19 ++++++++++++++++--- commands/discommand.go | 20 ++++++++++---------- commands/help.go | 4 ++-- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/commands/dataLength.go b/commands/dataLength.go index abea1e2..d4c8bcf 100644 --- a/commands/dataLength.go +++ b/commands/dataLength.go @@ -26,14 +26,14 @@ const ( userLearn ) -var DataLengthCommand Command = Command{ +var DataLengthCommand *Command = &Command{ ApplicationCommand: &discordgo.ApplicationCommand{ Type: discordgo.ChatApplicationCommand, Name: "데이터학습량", Description: "봇이 학습한 데ㅇ이터량을 보여줘요.", }, Aliases: []string{"학습데이터량", "데이터량", "학습량"}, - DetailedDescription: DetailedDescription{ + DetailedDescription: &DetailedDescription{ Usage: "머핀아 학습데이터량", }, } @@ -77,9 +77,11 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) { if err != nil { log.Fatalln(err) } + + defer cur.Close(context.TODO()) + cur.All(context.TODO(), &datas) ch <- chStruct{name: text, length: len(datas)} - }() go func() { @@ -89,6 +91,9 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) { if err != nil { log.Fatalln(err) } + + defer cur.Close(context.TODO()) + cur.All(context.TODO(), &datas) ch <- chStruct{name: muffin, length: len(datas)} }() @@ -107,6 +112,9 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) { if err != nil { log.Fatalln(err) } + + defer cur.Close(context.TODO()) + cur.All(context.TODO(), &datas) ch <- chStruct{name: nsfw, length: len(datas)} }() @@ -118,6 +126,9 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) { if err != nil { log.Fatalln(err) } + + defer cur.Close(context.TODO()) + cur.All(context.TODO(), &datas) ch <- chStruct{name: learn, length: len(datas)} }() @@ -130,6 +141,8 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) { log.Fatalln(err) } + defer cur.Close(context.TODO()) + cur.All(context.TODO(), &datas) ch <- chStruct{name: userLearn, length: len(datas)} }() diff --git a/commands/discommand.go b/commands/discommand.go index af36c1f..ddc1bd2 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -15,22 +15,22 @@ type DetailedDescription struct { type Command struct { *discordgo.ApplicationCommand Aliases []string - DetailedDescription DetailedDescription + DetailedDescription *DetailedDescription } 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) @@ -43,7 +43,7 @@ func new() *DiscommandStruct { 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 @@ -62,11 +62,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() diff --git a/commands/help.go b/commands/help.go index 7c473a9..8aa6c0b 100644 --- a/commands/help.go +++ b/commands/help.go @@ -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{"머핀아 도움말", "머핀아 도움말 배워"}, },