Compare commits

..

No commits in common. "11f22c627d5051cf4a2134434d6f6254d8162e5a" and "695b22c3d5f5d585ffb6cc714301fc8e7297f01f" have entirely different histories.

9 changed files with 65 additions and 239 deletions

View file

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

View file

@ -6,10 +6,8 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
type messageRun func(ctx *MsgContext) type messageRun func(s *discordgo.Session, m *discordgo.MessageCreate)
type chatInputRun func(s *InterContext) type chatInputRun func(s *discordgo.Session, m *discordgo.InteractionCreate)
type Category string
type DetailedDescription struct { type DetailedDescription struct {
Usage string Usage string
@ -21,7 +19,6 @@ type Command struct {
Aliases []string Aliases []string
DetailedDescription *DetailedDescription DetailedDescription *DetailedDescription
discommand *DiscommandStruct discommand *DiscommandStruct
Category Category
} }
type DiscommandStruct struct { type DiscommandStruct struct {
@ -31,22 +28,6 @@ type DiscommandStruct struct {
chatInputRuns map[string]chatInputRun 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 { func new() *DiscommandStruct {
discommand := DiscommandStruct{ discommand := DiscommandStruct{
Commands: map[string]*Command{}, Commands: map[string]*Command{},
@ -67,7 +48,6 @@ func new() *DiscommandStruct {
go discommand.addMessageRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListMessageRun) go discommand.addMessageRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListMessageRun)
go discommand.addMessageRun(InformationCommand.Name, InformationCommand.informationMessageRun) go discommand.addMessageRun(InformationCommand.Name, InformationCommand.informationMessageRun)
go discommand.addChatInputRun(HelpCommand.Name, HelpCommand.helpChatInputRun)
go discommand.addChatInputRun(DataLengthCommand.Name, DataLengthCommand.dataLenghChatInputRun) go discommand.addChatInputRun(DataLengthCommand.Name, DataLengthCommand.dataLenghChatInputRun)
go discommand.addChatInputRun(LearnCommand.Name, LearnCommand.learnChatInputRun) go discommand.addChatInputRun(LearnCommand.Name, LearnCommand.learnChatInputRun)
go discommand.addChatInputRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListChatInputRun) go discommand.addChatInputRun(LearnedDataListCommand.Name, LearnedDataListCommand.learnedDataListChatInputRun)
@ -75,13 +55,13 @@ func new() *DiscommandStruct {
return &discommand return &discommand
} }
func (d *DiscommandStruct) loadCommands(c *Command) { func (d *DiscommandStruct) loadCommands(command *Command) {
d.Commands[c.Name] = c d.Commands[command.Name] = command
d.Aliases[c.Name] = c.Name d.Aliases[command.Name] = command.Name
c.discommand = d command.discommand = d
for _, alias := range c.Aliases { for _, alias := range command.Aliases {
d.Aliases[alias] = c.Name d.Aliases[alias] = command.Name
} }
} }
@ -93,13 +73,13 @@ func (d *DiscommandStruct) addChatInputRun(name string, run chatInputRun) {
d.chatInputRuns[name] = run d.chatInputRuns[name] = run
} }
func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) { func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate) {
// 더욱 나아진 // 더욱 나아진
d.messageRuns[command](&MsgContext{s, m, args}) d.messageRuns[command](s, m)
} }
func (d *DiscommandStruct) ChatInputRun(command string, s *discordgo.Session, i *discordgo.InteractionCreate) { func (d *DiscommandStruct) ChatInputRun(command string, s *discordgo.Session, i *discordgo.InteractionCreate) {
d.chatInputRuns[command](&InterContext{s, i}) d.chatInputRuns[command](s, i)
} }
var Discommand *DiscommandStruct = new() var Discommand *DiscommandStruct = new()

View file

@ -1,171 +1,25 @@
package commands package commands
import ( import (
"strings" "fmt"
"git.wh64.net/muffin/goMuffin/configs"
"git.wh64.net/muffin/goMuffin/utils"
"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,
Name: "도움말", Name: "도움말",
Description: "기본적인 사용ㅂ법이에요.", 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"}, Aliases: []string{"도움", "명령어", "help"},
DetailedDescription: &DetailedDescription{ DetailedDescription: &DetailedDescription{
Usage: "머핀아 도움말 [명령어]", Usage: "머핀아 도움말 [명령어]",
Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"}, Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"},
}, },
Category: Generals,
} }
func getCommandsByCategory(d *DiscommandStruct, category Category) []string { func (c *Command) helpMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
commands := []string{} fmt.Println(c.Name)
for _, command := range d.Commands { s.ChannelMessageSend(m.ChannelID, "asdf")
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,7 +16,6 @@ var InformationCommand *Command = &Command{
DetailedDescription: &DetailedDescription{ DetailedDescription: &DetailedDescription{
Usage: "머핀아 정보", Usage: "머핀아 정보",
}, },
Category: Generals,
} }
func (c *Command) informationRun(s *discordgo.Session, m any) { func (c *Command) informationRun(s *discordgo.Session, m any) {
@ -66,10 +65,10 @@ func (c *Command) informationRun(s *discordgo.Session, m any) {
} }
} }
func (c *Command) informationMessageRun(ctx *MsgContext) { func (c *Command) informationMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
c.informationRun(ctx.Session, ctx.Msg) c.informationRun(s, m)
} }
func (c *Command) informationChatInputRun(ctx *InterContext) { func (c *Command) informationChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.informationRun(ctx.Session, ctx.Inter) c.informationRun(s, i)
} }

View file

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

View file

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

View file

@ -7,7 +7,7 @@ import (
"git.wh64.net/muffin/goMuffin/utils" "git.wh64.net/muffin/goMuffin/utils"
) )
const MUFFIN_VERSION = "0.0.0-gopher_canary.250330a" const MUFFIN_VERSION = "0.0.0-gopher_canary.250329c"
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0] var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]

View file

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

View file

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