goMuffin/commands/help.go
2025-05-17 15:09:42 +09:00

130 lines
3.7 KiB
Go

package commands
import (
"fmt"
"strings"
"git.wh64.net/muffin/goMuffin/configs"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
)
var HelpCommand *Command = &Command{
ApplicationCommand: &discordgo.ApplicationCommand{
Type: discordgo.ChatApplicationCommand,
Name: "도움말",
Description: "기본적인 사용ㅂ법이에요.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "명령어",
Description: "해당 명령어에 대ㅎ한 도움말을 볼 수 있어요.",
Choices: []*discordgo.ApplicationCommandOptionChoice{},
},
},
},
Aliases: []string{"도움", "명령어", "help"},
DetailedDescription: &DetailedDescription{
Usage: fmt.Sprintf("%s도움말 [명령어]", configs.Config.Bot.Prefix),
Examples: []string{fmt.Sprintf("%s도움말", configs.Config.Bot.Prefix), fmt.Sprintf("%s도움말 배워", configs.Config.Bot.Prefix)},
},
Category: General,
MessageRun: func(ctx *MsgContext) {
helpRun(ctx.Msg.Session, ctx.Msg, strings.Join(*ctx.Args, " "))
},
ChatInputRun: func(ctx *ChatInputContext) {
var command string
if opt, ok := ctx.Inter.Options["명령어"]; ok {
command = opt.StringValue()
}
helpRun(ctx.Inter.Session, ctx.Inter, command)
},
}
func getCommandsByCategory(d *DiscommandStruct, category Category) []string {
commands := []string{}
for _, command := range d.Commands {
if command.Category == category {
commands = append(commands, fmt.Sprintf("- %s: %s", command.Name, command.Description))
}
}
return commands
}
func helpRun(s *discordgo.Session, m any, commandName string) {
embed := &discordgo.MessageEmbed{
Color: utils.EmbedDefault,
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("버전: %s", configs.MUFFIN_VERSION),
},
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: s.State.User.AvatarURL("512"),
},
}
commandName = Discommand.Aliases[commandName]
if commandName == "" || Discommand.Commands[commandName] == nil {
embed.Title = fmt.Sprintf("%s의 도움말", s.State.User.Username)
embed.Description = utils.CodeBlock(
"md",
fmt.Sprintf("# 일반\n%s\n\n# 채팅\n%s",
strings.Join(getCommandsByCategory(Discommand, General), "\n"),
strings.Join(getCommandsByCategory(Discommand, Chatting), "\n")),
)
utils.NewMessageSender(m).AddEmbed(embed).SetReply(true).Send()
return
}
command := Discommand.Commands[commandName]
embed.Title = fmt.Sprintf("%s의 %s 명령어의 도움말", 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.Name == LearnCommand.Name {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "대답에 쓸 수 있는 인자",
Value: learnArguments,
})
}
if command.Aliases != nil {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "별칭",
Value: utils.CodeBlock("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.CodeBlock("md", strings.Join(addPrefix(command.DetailedDescription.Examples), "\n")),
})
} else {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "예시",
Value: "없음",
})
}
utils.NewMessageSender(m).AddEmbed(embed).SetReply(true).Send()
}