Compare commits

...

2 commits

Author SHA1 Message Date
11f22c627d
feat: Add help command (with Spaghetti) 2025-03-30 17:22:08 +09:00
d6a47dbfd0
feat: Add args 2025-03-30 15:08:29 +09:00
9 changed files with 239 additions and 65 deletions

View file

@ -37,6 +37,7 @@ var DataLengthCommand *Command = &Command{
DetailedDescription: &DetailedDescription{
Usage: "머핀아 학습데이터량",
},
Category: Generals,
}
var ch chan chStruct = make(chan chStruct)
@ -172,10 +173,10 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m any) {
}
}
func (c *Command) dataLengthMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
c.dataLengthRun(s, m)
func (c *Command) dataLengthMessageRun(ctx *MsgContext) {
c.dataLengthRun(ctx.Session, ctx.Msg)
}
func (c *Command) dataLenghChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.dataLengthRun(s, i)
func (c *Command) dataLenghChatInputRun(ctx *InterContext) {
c.dataLengthRun(ctx.Session, ctx.Inter)
}

View file

@ -6,8 +6,10 @@ import (
"github.com/bwmarrin/discordgo"
)
type messageRun func(s *discordgo.Session, m *discordgo.MessageCreate)
type chatInputRun func(s *discordgo.Session, m *discordgo.InteractionCreate)
type messageRun func(ctx *MsgContext)
type chatInputRun func(s *InterContext)
type Category string
type DetailedDescription struct {
Usage string
@ -19,6 +21,7 @@ type Command struct {
Aliases []string
DetailedDescription *DetailedDescription
discommand *DiscommandStruct
Category Category
}
type DiscommandStruct struct {
@ -28,6 +31,22 @@ type DiscommandStruct struct {
chatInputRuns map[string]chatInputRun
}
type MsgContext struct {
Session *discordgo.Session
Msg *discordgo.MessageCreate
Args []string
}
type InterContext struct {
Session *discordgo.Session
Inter *discordgo.InteractionCreate
}
const (
Chattings Category = "채팅"
Generals Category = "일반"
)
func new() *DiscommandStruct {
discommand := DiscommandStruct{
Commands: map[string]*Command{},
@ -48,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)
@ -55,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
}
}
@ -73,13 +93,13 @@ func (d *DiscommandStruct) addChatInputRun(name string, run chatInputRun) {
d.chatInputRuns[name] = run
}
func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate) {
func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) {
// 더욱 나아진
d.messageRuns[command](s, m)
d.messageRuns[command](&MsgContext{s, m, args})
}
func (d *DiscommandStruct) ChatInputRun(command string, s *discordgo.Session, i *discordgo.InteractionCreate) {
d.chatInputRuns[command](s, i)
d.chatInputRuns[command](&InterContext{s, i})
}
var Discommand *DiscommandStruct = new()

View file

@ -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)
}

View file

@ -16,6 +16,7 @@ var InformationCommand *Command = &Command{
DetailedDescription: &DetailedDescription{
Usage: "머핀아 정보",
},
Category: Generals,
}
func (c *Command) informationRun(s *discordgo.Session, m any) {
@ -65,10 +66,10 @@ func (c *Command) informationRun(s *discordgo.Session, m any) {
}
}
func (c *Command) informationMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
c.informationRun(s, m)
func (c *Command) informationMessageRun(ctx *MsgContext) {
c.informationRun(ctx.Session, ctx.Msg)
}
func (c *Command) informationChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.informationRun(s, i)
func (c *Command) informationChatInputRun(ctx *InterContext) {
c.informationRun(ctx.Session, ctx.Inter)
}

View file

@ -9,9 +9,8 @@ import (
"git.wh64.net/muffin/goMuffin/configs"
"git.wh64.net/muffin/goMuffin/databases"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
"github.com/LoperLee/golang-hangul-toolkit/hangul"
"github.com/bwmarrin/discordgo"
)
var LearnCommand *Command = &Command{
@ -42,6 +41,7 @@ var LearnCommand *Command = &Command{
"머핀아 배워 미간은_누구야? 이봇의_개발자요",
},
},
Category: Chattings,
}
func addPrefix(arr []string) (newArr []string) {
@ -51,44 +51,36 @@ func addPrefix(arr []string) (newArr []string) {
return
}
func (c *Command) learnRun(s *discordgo.Session, m any) {
func (c *Command) learnRun(s *discordgo.Session, m any, args *[]string) {
var userId, command, result string
igCommands := []string{}
switch m := m.(type) {
case *discordgo.MessageCreate:
userId = m.Author.ID
matches := utils.ExtractQuotedText.FindAllStringSubmatch(strings.TrimPrefix(m.Content, configs.Config.Bot.Prefix), 2)
if len(matches) < 2 {
content := strings.TrimPrefix(m.Content, configs.Config.Bot.Prefix)
command = strings.ReplaceAll(strings.Split(content, " ")[1], "_", "")
result = strings.ReplaceAll(strings.Split(content, " ")[2], "_", "")
if command == "" || result == "" {
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
Title: "❌ 오류",
Description: "올바르지 않ㅇ은 용법이에요.",
Fields: []*discordgo.MessageEmbedField{
{
Name: "사용법",
Value: utils.InlineCode(c.DetailedDescription.Usage),
Inline: true,
},
{
Name: "예시",
Value: strings.Join(addPrefix(c.DetailedDescription.Examples), "\n"),
},
if len(*args) < 2 {
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
Title: "❌ 오류",
Description: "올바르지 않ㅇ은 용법이에요.",
Fields: []*discordgo.MessageEmbedField{
{
Name: "사용법",
Value: utils.InlineCode(c.DetailedDescription.Usage),
Inline: true,
},
Color: int(utils.EFail),
}, m.Reference())
return
}
} else {
command = matches[0][1]
result = matches[1][1]
{
Name: "예시",
Value: strings.Join(addPrefix(c.DetailedDescription.Examples), "\n"),
},
},
Color: int(utils.EFail),
}, m.Reference())
return
}
command = strings.ReplaceAll((*args)[0], "_", " ")
result = strings.ReplaceAll((*args)[1], "_", " ")
case *discordgo.InteractionCreate:
s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
@ -206,10 +198,11 @@ func (c *Command) learnRun(s *discordgo.Session, m any) {
}
}
func (c *Command) learnMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
c.learnRun(s, m)
func (c *Command) learnMessageRun(ctx *MsgContext) {
c.learnRun(ctx.Session, ctx.Msg, &ctx.Args)
}
func (c *Command) learnChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.learnRun(s, i)
func (c *Command) learnChatInputRun(ctx *InterContext) {
var args *[]string
c.learnRun(ctx.Session, ctx.Inter, args)
}

View file

@ -23,6 +23,7 @@ var LearnedDataListCommand *Command = &Command{
DetailedDescription: &DetailedDescription{
Usage: "머핀아 리스트",
},
Category: Chattings,
}
func getDescriptions(datas *[]databases.Learn) (descriptions []string) {
@ -114,10 +115,10 @@ func (c *Command) learnedDataListRun(s *discordgo.Session, m any) {
}
}
func (c *Command) learnedDataListMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
c.learnedDataListRun(s, m)
func (c *Command) learnedDataListMessageRun(ctx *MsgContext) {
c.learnedDataListRun(ctx.Session, ctx.Msg)
}
func (c *Command) learnedDataListChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.learnedDataListRun(s, i)
func (c *Command) learnedDataListChatInputRun(ctx *InterContext) {
c.learnedDataListRun(ctx.Session, ctx.Inter)
}

View file

@ -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]

View file

@ -16,6 +16,17 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo"
)
func argParser(content string) (args []string) {
for _, arg := range utils.FlexibleStringParser.FindAllStringSubmatch(content, -1) {
if arg[1] != "" {
args = append(args, arg[1])
} else {
args = append(args, arg[0])
}
}
return
}
// MessageCreate is handlers of messageCreate event
func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
config := configs.Config
@ -25,7 +36,8 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if strings.HasPrefix(m.Content, config.Bot.Prefix) {
content := strings.TrimPrefix(m.Content, config.Bot.Prefix)
command := commands.Discommand.Aliases[strings.Split(content, " ")[0]]
args := argParser(content)
command := commands.Discommand.Aliases[args[0]]
if m.Author.ID == config.Train.UserID {
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
@ -104,7 +116,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}
commands.Discommand.MessageRun(command, s, m)
commands.Discommand.MessageRun(command, s, m, args[1:])
return
} else {
return

View file

@ -2,5 +2,5 @@ package utils
import "regexp"
var ExtractQuotedText *regexp.Regexp = regexp.MustCompile("[\"'`](.*?)[\"'`]")
var FlexibleStringParser *regexp.Regexp = regexp.MustCompile("[^\\s\"'「」«»]+|\"([^\"]*)\"|'([^']*)'|「([^」]*)」|«([^»]*)»")
var Decimals *regexp.Regexp = regexp.MustCompile(`\d+`)