fix: Some spaghetti and advanced command load stabilty

This commit is contained in:
Siwoo Jeon 2025-03-30 18:44:33 +09:00
parent 11f22c627d
commit 58e7d54658
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
8 changed files with 95 additions and 112 deletions

View file

@ -27,6 +27,8 @@ const (
userLearn userLearn
) )
var ch chan chStruct = make(chan chStruct)
var DataLengthCommand *Command = &Command{ var DataLengthCommand *Command = &Command{
ApplicationCommand: &discordgo.ApplicationCommand{ ApplicationCommand: &discordgo.ApplicationCommand{
Type: discordgo.ChatApplicationCommand, Type: discordgo.ChatApplicationCommand,
@ -38,8 +40,13 @@ var DataLengthCommand *Command = &Command{
Usage: "머핀아 학습데이터량", Usage: "머핀아 학습데이터량",
}, },
Category: Generals, 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) { func getLength(data dataType, userId string) {
var err error var err error
@ -75,7 +82,7 @@ func getLength(data dataType, userId string) {
ch <- chStruct{name: data, length: len(datas)} 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 i *discordgo.Interaction
var referance *discordgo.MessageReference var referance *discordgo.MessageReference
var username, userId, channelId string 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)
}

View file

@ -3,11 +3,13 @@ package commands
import ( import (
// "fmt" // "fmt"
"sync"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
type messageRun func(ctx *MsgContext) type messageRun func(ctx *MsgContext)
type chatInputRun func(s *InterContext) type chatInputRun func(ctx *InterContext)
type Category string type Category string
@ -20,26 +22,27 @@ type Command struct {
*discordgo.ApplicationCommand *discordgo.ApplicationCommand
Aliases []string Aliases []string
DetailedDescription *DetailedDescription DetailedDescription *DetailedDescription
discommand *DiscommandStruct
Category Category Category Category
MessageRun messageRun
ChatInputRun chatInputRun
} }
type DiscommandStruct struct { type DiscommandStruct struct {
Commands map[string]*Command Commands map[string]*Command
Aliases map[string]string Aliases map[string]string
messageRuns map[string]messageRun
chatInputRuns map[string]chatInputRun
} }
type MsgContext struct { type MsgContext struct {
Session *discordgo.Session Session *discordgo.Session
Msg *discordgo.MessageCreate Msg *discordgo.MessageCreate
Args []string Args []string
Command *Command
} }
type InterContext struct { type InterContext struct {
Session *discordgo.Session Session *discordgo.Session
Inter *discordgo.InteractionCreate Inter *discordgo.InteractionCreate
Command *Command
} }
const ( const (
@ -47,59 +50,49 @@ const (
Generals Category = "일반" Generals Category = "일반"
) )
var mutex *sync.Mutex = &sync.Mutex{}
func new() *DiscommandStruct { func new() *DiscommandStruct {
discommand := DiscommandStruct{ discommand := DiscommandStruct{
Commands: map[string]*Command{}, Commands: map[string]*Command{},
Aliases: map[string]string{}, Aliases: map[string]string{},
messageRuns: map[string]messageRun{},
chatInputRuns: map[string]chatInputRun{},
} }
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 return &discommand
} }
func (d *DiscommandStruct) loadCommands(c *Command) { func (d *DiscommandStruct) LoadCommand(c *Command) {
mutex.Lock()
d.Commands[c.Name] = c d.Commands[c.Name] = c
d.Aliases[c.Name] = c.Name d.Aliases[c.Name] = c.Name
c.discommand = d
for _, alias := range c.Aliases { for _, alias := range c.Aliases {
d.Aliases[alias] = c.Name d.Aliases[alias] = c.Name
} }
mutex.Unlock()
} }
func (d *DiscommandStruct) addMessageRun(name string, run messageRun) { // func (d *DiscommandStruct) addMessageRun(name string, run messageRun) {
d.messageRuns[name] = run // 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) { func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, i *discordgo.InteractionCreate) {
d.chatInputRuns[name] = run command := d.Commands[name]
} if command == nil {
return
func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) { }
// 더욱 나아진 command.ChatInputRun(&InterContext{s, i, command})
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})
} }
var Discommand *DiscommandStruct = new() var Discommand *DiscommandStruct = new()

View file

@ -8,16 +8,6 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// 씨발 나중에 구조 개선 하면서 갈아 엎어야지
var commandNames []string = []string{
"도움말",
"데이터학습량",
"정보",
"배워",
"리스트",
// "삭제",
}
var HelpCommand *Command = &Command{ var HelpCommand *Command = &Command{
ApplicationCommand: &discordgo.ApplicationCommand{ ApplicationCommand: &discordgo.ApplicationCommand{
Type: discordgo.ChatApplicationCommand, Type: discordgo.ChatApplicationCommand,
@ -30,10 +20,10 @@ var HelpCommand *Command = &Command{
Description: "해당 명령어에 대ㅎ한 도움말을 볼 수 있어요.", Description: "해당 명령어에 대ㅎ한 도움말을 볼 수 있어요.",
Choices: func() []*discordgo.ApplicationCommandOptionChoice { Choices: func() []*discordgo.ApplicationCommandOptionChoice {
choices := []*discordgo.ApplicationCommandOptionChoice{} choices := []*discordgo.ApplicationCommandOptionChoice{}
for _, name := range commandNames { for _, command := range Discommand.Commands {
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
Name: name, Name: command.Name,
Value: name, Value: command.Name,
}) })
} }
return choices return choices
@ -47,6 +37,13 @@ var HelpCommand *Command = &Command{
Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"}, Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"},
}, },
Category: Generals, 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 { func getCommandsByCategory(d *DiscommandStruct, category Category) []string {
@ -59,7 +56,7 @@ func getCommandsByCategory(d *DiscommandStruct, category Category) []string {
return commands 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 var commandName string
embed := &discordgo.MessageEmbed{ embed := &discordgo.MessageEmbed{
Color: int(utils.EDefault), 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.Title = s.State.User.Username + "의 도움말"
embed.Description = utils.CodeBlockWithLanguage( embed.Description = utils.CodeBlockWithLanguage(
"md", "md",
"# 일반\n"+ "# 일반\n"+
strings.Join(getCommandsByCategory(c.discommand, Generals), "\n")+ strings.Join(getCommandsByCategory(Discommand, Generals), "\n")+
"\n\n# 채팅\n"+ "\n\n# 채팅\n"+
strings.Join(getCommandsByCategory(c.discommand, Chattings), "\n"), strings.Join(getCommandsByCategory(Discommand, Chattings), "\n"),
) )
switch m := m.(type) { switch m := m.(type) {
@ -108,7 +105,7 @@ func (c *Command) helpRun(s *discordgo.Session, m any, args *[]string) {
return return
} }
command := c.discommand.Commands[commandName] command := Discommand.Commands[commandName]
embed.Title = s.State.User.Username + "의 " + command.Name + " 도움말" embed.Title = s.State.User.Username + "의 " + command.Name + " 도움말"
embed.Fields = []*discordgo.MessageEmbedField{ 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)
}

View file

@ -17,9 +17,15 @@ var InformationCommand *Command = &Command{
Usage: "머핀아 정보", Usage: "머핀아 정보",
}, },
Category: Generals, 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) owner, _ := s.User(configs.Config.Bot.OwnerId)
embed := &discordgo.MessageEmbed{ embed := &discordgo.MessageEmbed{
Title: s.State.User.Username + "의 정보", 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)
}

View file

@ -42,6 +42,13 @@ var LearnCommand *Command = &Command{
}, },
}, },
Category: Chattings, 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) { func addPrefix(arr []string) (newArr []string) {
@ -51,7 +58,7 @@ func addPrefix(arr []string) (newArr []string) {
return 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 var userId, command, result string
igCommands := []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.Name)
igCommands = append(igCommands, command.Aliases...) 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)
}

View file

@ -24,6 +24,12 @@ var LearnedDataListCommand *Command = &Command{
Usage: "머핀아 리스트", Usage: "머핀아 리스트",
}, },
Category: Chattings, 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) { func getDescriptions(datas *[]databases.Learn) (descriptions []string) {
@ -33,7 +39,7 @@ func getDescriptions(datas *[]databases.Learn) (descriptions []string) {
return return
} }
func (c *Command) learnedDataListRun(s *discordgo.Session, m any) { func learnedDataListRun(s *discordgo.Session, m any) {
var userId, globalName, avatarUrl string var userId, globalName, avatarUrl string
var datas []databases.Learn var datas []databases.Learn
switch m := m.(type) { 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)
}

View file

@ -6,5 +6,8 @@ import (
) )
func InteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { 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
}
} }

10
main.go
View file

@ -25,8 +25,14 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
dg.AddHandler(handler.MessageCreate) go commands.Discommand.LoadCommand(commands.HelpCommand)
dg.AddHandler(handler.InteractionCreate) 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() dg.Open()