diff --git a/commands/discommand.go b/commands/discommand.go index a5b1f8a..e016420 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -67,6 +67,7 @@ func new() *DiscommandStruct { 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) @@ -74,13 +75,13 @@ func new() *DiscommandStruct { return &discommand } -func (d *DiscommandStruct) loadCommands(command *Command) { - d.Commands[command.Name] = command - d.Aliases[command.Name] = command.Name - command.discommand = d +func (d *DiscommandStruct) loadCommands(c *Command) { + d.Commands[c.Name] = c + d.Aliases[c.Name] = c.Name + c.discommand = d - for _, alias := range command.Aliases { - d.Aliases[alias] = command.Name + for _, alias := range c.Aliases { + d.Aliases[alias] = c.Name } } diff --git a/commands/help.go b/commands/help.go index 8aa6c0b..5037fd8 100644 --- a/commands/help.go +++ b/commands/help.go @@ -1,25 +1,171 @@ package commands import ( - "fmt" + "strings" + "git.wh64.net/muffin/goMuffin/configs" + "git.wh64.net/muffin/goMuffin/utils" "github.com/bwmarrin/discordgo" ) +// 씨발 나중에 구조 개선 하면서 갈아 엎어야지 +var commandNames []string = []string{ + "도움말", + "데이터학습량", + "정보", + "배워", + "리스트", + // "삭제", +} + var HelpCommand *Command = &Command{ ApplicationCommand: &discordgo.ApplicationCommand{ Type: discordgo.ChatApplicationCommand, Name: "도움말", Description: "기본적인 사용ㅂ법이에요.", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "명령어", + Description: "해당 명령어에 대ㅎ한 도움말을 볼 수 있어요.", + Choices: func() []*discordgo.ApplicationCommandOptionChoice { + choices := []*discordgo.ApplicationCommandOptionChoice{} + for _, name := range commandNames { + choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ + Name: name, + Value: name, + }) + } + return choices + }(), + }, + }, }, Aliases: []string{"도움", "명령어", "help"}, DetailedDescription: &DetailedDescription{ Usage: "머핀아 도움말 [명령어]", Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"}, }, + Category: Generals, } -func (c *Command) helpMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { - fmt.Println(c.Name) - s.ChannelMessageSend(m.ChannelID, "asdf") +func getCommandsByCategory(d *DiscommandStruct, category Category) []string { + commands := []string{} + for _, command := range d.Commands { + if command.Category == category { + commands = append(commands, "- "+command.Name+": "+command.Description) + } + } + return commands +} + +func (c *Command) helpRun(s *discordgo.Session, m any, args *[]string) { + var commandName string + embed := &discordgo.MessageEmbed{ + Color: int(utils.EDefault), + Footer: &discordgo.MessageEmbedFooter{ + Text: "버전:" + configs.MUFFIN_VERSION, + }, + Thumbnail: &discordgo.MessageEmbedThumbnail{ + URL: s.State.User.AvatarURL("512"), + }, + } + + switch m := m.(type) { + case *discordgo.MessageCreate: + commandName = strings.Join(*args, " ") + case *discordgo.InteractionCreate: + optsMap := map[string]*discordgo.ApplicationCommandInteractionDataOption{} + for _, opt := range m.ApplicationCommandData().Options { + optsMap[opt.Name] = opt + } + if opt, ok := optsMap["명령어"]; ok { + commandName = opt.StringValue() + } + } + + if commandName == "" || c.discommand.Commands[commandName] == nil { + embed.Title = s.State.User.Username + "의 도움말" + embed.Description = utils.CodeBlockWithLanguage( + "md", + "# 일반\n"+ + strings.Join(getCommandsByCategory(c.discommand, Generals), "\n")+ + "\n\n# 채팅\n"+ + strings.Join(getCommandsByCategory(c.discommand, Chattings), "\n"), + ) + + switch m := m.(type) { + case *discordgo.MessageCreate: + s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference()) + case *discordgo.InteractionCreate: + s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{embed}, + }, + }) + } + return + } + + command := c.discommand.Commands[commandName] + + embed.Title = s.State.User.Username + "의 " + command.Name + " 도움말" + embed.Fields = []*discordgo.MessageEmbedField{ + { + Name: "설명", + Value: utils.InlineCode(command.Description), + Inline: true, + }, + { + Name: "사용법", + Value: utils.InlineCode(command.DetailedDescription.Usage), + Inline: true, + }, + } + + if command.Aliases != nil { + embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{ + Name: "별칭", + Value: utils.CodeBlockWithLanguage("md", strings.Join(addPrefix(command.Aliases), "\n")), + }) + } else { + embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{ + Name: "별칭", + Value: "없음", + }) + } + + if command.DetailedDescription.Examples != nil { + embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{ + Name: "예시", + Value: utils.CodeBlockWithLanguage("md", strings.Join(addPrefix(c.DetailedDescription.Examples), "\n")), + }) + } else { + embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{ + Name: "예시", + Value: "없음", + }) + } + + switch m := m.(type) { + case *discordgo.MessageCreate: + s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference()) + case *discordgo.InteractionCreate: + s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{embed}, + }, + }) + } +} + +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/configs/version.go b/configs/version.go index 21af01d..c2c600d 100644 --- a/configs/version.go +++ b/configs/version.go @@ -7,7 +7,7 @@ import ( "git.wh64.net/muffin/goMuffin/utils" ) -const MUFFIN_VERSION = "0.0.0-gopher_canary.250329c" +const MUFFIN_VERSION = "0.0.0-gopher_canary.250330a" var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]