fix: Choices can't show in help command

This commit is contained in:
Siwoo Jeon 2025-04-05 18:54:20 +09:00
parent 92f875f3c5
commit f540a61f05
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 17 additions and 15 deletions

View file

@ -63,8 +63,8 @@ const (
Generals Category = "일반" Generals Category = "일반"
) )
var commandMutex *sync.Mutex = &sync.Mutex{} var commandMutex sync.Mutex
var componentMutex *sync.Mutex = &sync.Mutex{} var componentMutex sync.Mutex
func new() *DiscommandStruct { func new() *DiscommandStruct {
discommand := DiscommandStruct{ discommand := DiscommandStruct{
@ -76,6 +76,7 @@ func new() *DiscommandStruct {
} }
func (d *DiscommandStruct) LoadCommand(c *Command) { func (d *DiscommandStruct) LoadCommand(c *Command) {
defer commandMutex.Unlock()
commandMutex.Lock() commandMutex.Lock()
d.Commands[c.Name] = c d.Commands[c.Name] = c
d.Aliases[c.Name] = c.Name d.Aliases[c.Name] = c.Name
@ -83,13 +84,12 @@ func (d *DiscommandStruct) LoadCommand(c *Command) {
for _, alias := range c.Aliases { for _, alias := range c.Aliases {
d.Aliases[alias] = c.Name d.Aliases[alias] = c.Name
} }
commandMutex.Unlock()
} }
func (d *DiscommandStruct) LoadComponent(c *Component) { func (d *DiscommandStruct) LoadComponent(c *Component) {
defer componentMutex.Unlock()
componentMutex.Lock() componentMutex.Lock()
d.Components = append(d.Components, c) d.Components = append(d.Components, c)
componentMutex.Unlock()
} }
func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) { func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) {

View file

@ -19,16 +19,7 @@ var HelpCommand *Command = &Command{
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
Name: "명령어", Name: "명령어",
Description: "해당 명령어에 대ㅎ한 도움말을 볼 수 있어요.", Description: "해당 명령어에 대ㅎ한 도움말을 볼 수 있어요.",
Choices: func() []*discordgo.ApplicationCommandOptionChoice { Choices: []*discordgo.ApplicationCommandOptionChoice{},
choices := []*discordgo.ApplicationCommandOptionChoice{}
for _, command := range Discommand.Commands {
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
Name: command.Name,
Value: command.Name,
})
}
return choices
}(),
}, },
}, },
}, },
@ -73,7 +64,7 @@ func helpRun(c *Command, s *discordgo.Session, m any, args *[]string) {
case *discordgo.MessageCreate: case *discordgo.MessageCreate:
commandName = Discommand.Aliases[strings.Join(*args, " ")] commandName = Discommand.Aliases[strings.Join(*args, " ")]
case *utils.InteractionCreate: case *utils.InteractionCreate:
if opt, ok := m.Options["도움말"]; ok { if opt, ok := m.Options["명령어"]; ok {
commandName = opt.StringValue() commandName = opt.StringValue()
} else { } else {
commandName = "" commandName = ""

11
main.go
View file

@ -39,6 +39,17 @@ func main() {
dg.Open() dg.Open()
for _, cmd := range commands.Discommand.Commands { for _, cmd := range commands.Discommand.Commands {
if cmd.Name == "도움말" {
// 극한의 성능 똥망 코드 탄생!
// 무려 똑같은 걸 반복해서 돌리는!
for _, a := range commands.Discommand.Commands {
cmd.Options[0].Choices = append(cmd.Options[0].Choices, &discordgo.ApplicationCommandOptionChoice{
Name: a.Name,
Value: a.Name,
})
}
}
go dg.ApplicationCommandCreate(dg.State.User.ID, "", cmd.ApplicationCommand) go dg.ApplicationCommandCreate(dg.State.User.ID, "", cmd.ApplicationCommand)
} }