From 58e7d546585e2ce4b5e45539281c045f1365581f Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Sun, 30 Mar 2025 18:44:33 +0900 Subject: [PATCH] fix: Some spaghetti and advanced command load stabilty --- commands/dataLength.go | 19 ++++----- commands/discommand.go | 79 ++++++++++++++++-------------------- commands/help.go | 42 +++++++------------ commands/information.go | 16 ++++---- commands/learn.go | 20 ++++----- commands/learnedDataList.go | 16 ++++---- handler/interactionCreate.go | 5 ++- main.go | 10 ++++- 8 files changed, 95 insertions(+), 112 deletions(-) diff --git a/commands/dataLength.go b/commands/dataLength.go index b51053f..588ba8b 100644 --- a/commands/dataLength.go +++ b/commands/dataLength.go @@ -27,6 +27,8 @@ const ( userLearn ) +var ch chan chStruct = make(chan chStruct) + var DataLengthCommand *Command = &Command{ ApplicationCommand: &discordgo.ApplicationCommand{ Type: discordgo.ChatApplicationCommand, @@ -38,8 +40,13 @@ var DataLengthCommand *Command = &Command{ Usage: "머핀아 학습데이터량", }, Category: Generals, + MessageRun: func(ctx *MsgContext) { + dataLengthRun(ctx.Session, ctx.Msg) + }, + ChatInputRun: func(ctx *InterContext) { + dataLengthRun(ctx.Session, ctx.Inter) + }, } -var ch chan chStruct = make(chan chStruct) func getLength(data dataType, userId string) { var err error @@ -75,7 +82,7 @@ func getLength(data dataType, userId string) { ch <- chStruct{name: data, length: len(datas)} } -func (c *Command) dataLengthRun(s *discordgo.Session, m any) { +func dataLengthRun(s *discordgo.Session, m any) { var i *discordgo.Interaction var referance *discordgo.MessageReference var username, userId, channelId string @@ -172,11 +179,3 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m any) { }) } } - -func (c *Command) dataLengthMessageRun(ctx *MsgContext) { - c.dataLengthRun(ctx.Session, ctx.Msg) -} - -func (c *Command) dataLenghChatInputRun(ctx *InterContext) { - c.dataLengthRun(ctx.Session, ctx.Inter) -} diff --git a/commands/discommand.go b/commands/discommand.go index e016420..da57e3f 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -3,11 +3,13 @@ package commands import ( // "fmt" + "sync" + "github.com/bwmarrin/discordgo" ) type messageRun func(ctx *MsgContext) -type chatInputRun func(s *InterContext) +type chatInputRun func(ctx *InterContext) type Category string @@ -20,26 +22,27 @@ type Command struct { *discordgo.ApplicationCommand Aliases []string DetailedDescription *DetailedDescription - discommand *DiscommandStruct Category Category + MessageRun messageRun + ChatInputRun chatInputRun } type DiscommandStruct struct { - Commands map[string]*Command - Aliases map[string]string - messageRuns map[string]messageRun - chatInputRuns map[string]chatInputRun + Commands map[string]*Command + Aliases map[string]string } type MsgContext struct { Session *discordgo.Session Msg *discordgo.MessageCreate Args []string + Command *Command } type InterContext struct { Session *discordgo.Session Inter *discordgo.InteractionCreate + Command *Command } const ( @@ -47,59 +50,49 @@ const ( Generals Category = "일반" ) +var mutex *sync.Mutex = &sync.Mutex{} + func new() *DiscommandStruct { discommand := DiscommandStruct{ - Commands: map[string]*Command{}, - Aliases: map[string]string{}, - messageRuns: map[string]messageRun{}, - chatInputRuns: map[string]chatInputRun{}, + Commands: map[string]*Command{}, + Aliases: map[string]string{}, } - - go discommand.loadCommands(HelpCommand) - go discommand.loadCommands(DataLengthCommand) - go discommand.loadCommands(LearnCommand) - go discommand.loadCommands(LearnedDataListCommand) - go discommand.loadCommands(InformationCommand) - - go discommand.addMessageRun(HelpCommand.Name, HelpCommand.helpMessageRun) - go discommand.addMessageRun(DataLengthCommand.Name, DataLengthCommand.dataLengthMessageRun) - go discommand.addMessageRun(LearnCommand.Name, LearnCommand.learnMessageRun) - go discommand.addMessageRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListMessageRun) - go discommand.addMessageRun(InformationCommand.Name, InformationCommand.informationMessageRun) - - go discommand.addChatInputRun(HelpCommand.Name, HelpCommand.helpChatInputRun) - go discommand.addChatInputRun(DataLengthCommand.Name, DataLengthCommand.dataLenghChatInputRun) - go discommand.addChatInputRun(LearnCommand.Name, LearnCommand.learnChatInputRun) - go discommand.addChatInputRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListChatInputRun) - go discommand.addChatInputRun(InformationCommand.Name, DataLengthCommand.informationChatInputRun) return &discommand } -func (d *DiscommandStruct) loadCommands(c *Command) { +func (d *DiscommandStruct) LoadCommand(c *Command) { + mutex.Lock() d.Commands[c.Name] = c d.Aliases[c.Name] = c.Name - c.discommand = d for _, alias := range c.Aliases { d.Aliases[alias] = c.Name } + mutex.Unlock() } -func (d *DiscommandStruct) addMessageRun(name string, run messageRun) { - d.messageRuns[name] = run +// func (d *DiscommandStruct) addMessageRun(name string, run messageRun) { +// d.messageRuns[name] = run +// } + +// func (d *DiscommandStruct) addChatInputRun(name string, run chatInputRun) { +// d.chatInputRuns[name] = run +// } + +func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) { + command := d.Commands[name] + if command == nil { + return + } + command.MessageRun(&MsgContext{s, m, args, command}) } -func (d *DiscommandStruct) addChatInputRun(name string, run chatInputRun) { - d.chatInputRuns[name] = run -} - -func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) { - // 더욱 나아진 - d.messageRuns[command](&MsgContext{s, m, args}) -} - -func (d *DiscommandStruct) ChatInputRun(command string, s *discordgo.Session, i *discordgo.InteractionCreate) { - d.chatInputRuns[command](&InterContext{s, i}) +func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, i *discordgo.InteractionCreate) { + command := d.Commands[name] + if command == nil { + return + } + command.ChatInputRun(&InterContext{s, i, command}) } var Discommand *DiscommandStruct = new() diff --git a/commands/help.go b/commands/help.go index 5037fd8..6a5c18a 100644 --- a/commands/help.go +++ b/commands/help.go @@ -8,16 +8,6 @@ import ( "github.com/bwmarrin/discordgo" ) -// 씨발 나중에 구조 개선 하면서 갈아 엎어야지 -var commandNames []string = []string{ - "도움말", - "데이터학습량", - "정보", - "배워", - "리스트", - // "삭제", -} - var HelpCommand *Command = &Command{ ApplicationCommand: &discordgo.ApplicationCommand{ Type: discordgo.ChatApplicationCommand, @@ -30,10 +20,10 @@ var HelpCommand *Command = &Command{ Description: "해당 명령어에 대ㅎ한 도움말을 볼 수 있어요.", Choices: func() []*discordgo.ApplicationCommandOptionChoice { choices := []*discordgo.ApplicationCommandOptionChoice{} - for _, name := range commandNames { + for _, command := range Discommand.Commands { choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ - Name: name, - Value: name, + Name: command.Name, + Value: command.Name, }) } return choices @@ -47,6 +37,13 @@ var HelpCommand *Command = &Command{ Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"}, }, Category: Generals, + MessageRun: func(ctx *MsgContext) { + helpRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args) + }, + ChatInputRun: func(ctx *InterContext) { + var args *[]string + helpRun(ctx.Command, ctx.Session, ctx.Inter, args) + }, } func getCommandsByCategory(d *DiscommandStruct, category Category) []string { @@ -59,7 +56,7 @@ func getCommandsByCategory(d *DiscommandStruct, category Category) []string { return commands } -func (c *Command) helpRun(s *discordgo.Session, m any, args *[]string) { +func helpRun(c *Command, s *discordgo.Session, m any, args *[]string) { var commandName string embed := &discordgo.MessageEmbed{ Color: int(utils.EDefault), @@ -84,14 +81,14 @@ func (c *Command) helpRun(s *discordgo.Session, m any, args *[]string) { } } - if commandName == "" || c.discommand.Commands[commandName] == nil { + if commandName == "" || Discommand.Commands[commandName] == nil { embed.Title = s.State.User.Username + "의 도움말" embed.Description = utils.CodeBlockWithLanguage( "md", "# 일반\n"+ - strings.Join(getCommandsByCategory(c.discommand, Generals), "\n")+ + strings.Join(getCommandsByCategory(Discommand, Generals), "\n")+ "\n\n# 채팅\n"+ - strings.Join(getCommandsByCategory(c.discommand, Chattings), "\n"), + strings.Join(getCommandsByCategory(Discommand, Chattings), "\n"), ) switch m := m.(type) { @@ -108,7 +105,7 @@ func (c *Command) helpRun(s *discordgo.Session, m any, args *[]string) { return } - command := c.discommand.Commands[commandName] + command := Discommand.Commands[commandName] embed.Title = s.State.User.Username + "의 " + command.Name + " 도움말" embed.Fields = []*discordgo.MessageEmbedField{ @@ -160,12 +157,3 @@ func (c *Command) helpRun(s *discordgo.Session, m any, args *[]string) { }) } } - -func (c *Command) helpMessageRun(ctx *MsgContext) { - c.helpRun(ctx.Session, ctx.Msg, &ctx.Args) -} - -func (c *Command) helpChatInputRun(ctx *InterContext) { - var args *[]string - c.helpRun(ctx.Session, ctx.Inter, args) -} diff --git a/commands/information.go b/commands/information.go index 7e2dc28..30bc14d 100644 --- a/commands/information.go +++ b/commands/information.go @@ -17,9 +17,15 @@ var InformationCommand *Command = &Command{ Usage: "머핀아 정보", }, Category: Generals, + MessageRun: func(ctx *MsgContext) { + informationRun(ctx.Session, ctx.Msg) + }, + ChatInputRun: func(ctx *InterContext) { + informationRun(ctx.Session, ctx.Inter) + }, } -func (c *Command) informationRun(s *discordgo.Session, m any) { +func informationRun(s *discordgo.Session, m any) { owner, _ := s.User(configs.Config.Bot.OwnerId) embed := &discordgo.MessageEmbed{ Title: s.State.User.Username + "의 정보", @@ -65,11 +71,3 @@ func (c *Command) informationRun(s *discordgo.Session, m any) { }) } } - -func (c *Command) informationMessageRun(ctx *MsgContext) { - c.informationRun(ctx.Session, ctx.Msg) -} - -func (c *Command) informationChatInputRun(ctx *InterContext) { - c.informationRun(ctx.Session, ctx.Inter) -} diff --git a/commands/learn.go b/commands/learn.go index 72bc5ce..5980cf0 100644 --- a/commands/learn.go +++ b/commands/learn.go @@ -42,6 +42,13 @@ var LearnCommand *Command = &Command{ }, }, Category: Chattings, + MessageRun: func(ctx *MsgContext) { + learnRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args) + }, + ChatInputRun: func(ctx *InterContext) { + var args *[]string + learnRun(ctx.Command, ctx.Session, ctx.Inter, args) + }, } func addPrefix(arr []string) (newArr []string) { @@ -51,7 +58,7 @@ func addPrefix(arr []string) (newArr []string) { return } -func (c *Command) learnRun(s *discordgo.Session, m any, args *[]string) { +func learnRun(c *Command, s *discordgo.Session, m any, args *[]string) { var userId, command, result string igCommands := []string{} @@ -105,7 +112,7 @@ func (c *Command) learnRun(s *discordgo.Session, m any, args *[]string) { } } - for _, command := range c.discommand.Commands { + for _, command := range Discommand.Commands { igCommands = append(igCommands, command.Name) igCommands = append(igCommands, command.Aliases...) } @@ -197,12 +204,3 @@ func (c *Command) learnRun(s *discordgo.Session, m any, args *[]string) { }) } } - -func (c *Command) learnMessageRun(ctx *MsgContext) { - c.learnRun(ctx.Session, ctx.Msg, &ctx.Args) -} - -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 b17142a..8b40787 100644 --- a/commands/learnedDataList.go +++ b/commands/learnedDataList.go @@ -24,6 +24,12 @@ var LearnedDataListCommand *Command = &Command{ Usage: "머핀아 리스트", }, Category: Chattings, + MessageRun: func(ctx *MsgContext) { + learnedDataListRun(ctx.Session, ctx.Msg) + }, + ChatInputRun: func(ctx *InterContext) { + learnedDataListRun(ctx.Session, ctx.Inter) + }, } func getDescriptions(datas *[]databases.Learn) (descriptions []string) { @@ -33,7 +39,7 @@ func getDescriptions(datas *[]databases.Learn) (descriptions []string) { return } -func (c *Command) learnedDataListRun(s *discordgo.Session, m any) { +func learnedDataListRun(s *discordgo.Session, m any) { var userId, globalName, avatarUrl string var datas []databases.Learn switch m := m.(type) { @@ -114,11 +120,3 @@ func (c *Command) learnedDataListRun(s *discordgo.Session, m any) { }) } } - -func (c *Command) learnedDataListMessageRun(ctx *MsgContext) { - c.learnedDataListRun(ctx.Session, ctx.Msg) -} - -func (c *Command) learnedDataListChatInputRun(ctx *InterContext) { - c.learnedDataListRun(ctx.Session, ctx.Inter) -} diff --git a/handler/interactionCreate.go b/handler/interactionCreate.go index 199e1a8..e24c51d 100644 --- a/handler/interactionCreate.go +++ b/handler/interactionCreate.go @@ -6,5 +6,8 @@ import ( ) func InteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { - commands.Discommand.ChatInputRun(i.ApplicationCommandData().Name, s, i) + if i.Type == discordgo.InteractionApplicationCommand { + commands.Discommand.ChatInputRun(i.ApplicationCommandData().Name, s, i) + return + } } diff --git a/main.go b/main.go index 3f68600..35347d0 100644 --- a/main.go +++ b/main.go @@ -25,8 +25,14 @@ func main() { log.Fatalln(err) } - dg.AddHandler(handler.MessageCreate) - dg.AddHandler(handler.InteractionCreate) + go commands.Discommand.LoadCommand(commands.HelpCommand) + go commands.Discommand.LoadCommand(commands.DataLengthCommand) + go commands.Discommand.LoadCommand(commands.LearnCommand) + go commands.Discommand.LoadCommand(commands.LearnedDataListCommand) + go commands.Discommand.LoadCommand(commands.InformationCommand) + + go dg.AddHandler(handler.MessageCreate) + go dg.AddHandler(handler.InteractionCreate) dg.Open()