feat: Add args
This commit is contained in:
parent
695b22c3d5
commit
d6a47dbfd0
7 changed files with 81 additions and 54 deletions
|
@ -37,6 +37,7 @@ 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)
|
||||||
|
|
||||||
|
@ -172,10 +173,10 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m any) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) dataLengthMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (c *Command) dataLengthMessageRun(ctx *MsgContext) {
|
||||||
c.dataLengthRun(s, m)
|
c.dataLengthRun(ctx.Session, ctx.Msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) dataLenghChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func (c *Command) dataLenghChatInputRun(ctx *InterContext) {
|
||||||
c.dataLengthRun(s, i)
|
c.dataLengthRun(ctx.Session, ctx.Inter)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,10 @@ import (
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type messageRun func(s *discordgo.Session, m *discordgo.MessageCreate)
|
type messageRun func(ctx *MsgContext)
|
||||||
type chatInputRun func(s *discordgo.Session, m *discordgo.InteractionCreate)
|
type chatInputRun func(s *InterContext)
|
||||||
|
|
||||||
|
type Category string
|
||||||
|
|
||||||
type DetailedDescription struct {
|
type DetailedDescription struct {
|
||||||
Usage string
|
Usage string
|
||||||
|
@ -19,6 +21,7 @@ type Command struct {
|
||||||
Aliases []string
|
Aliases []string
|
||||||
DetailedDescription *DetailedDescription
|
DetailedDescription *DetailedDescription
|
||||||
discommand *DiscommandStruct
|
discommand *DiscommandStruct
|
||||||
|
Category Category
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiscommandStruct struct {
|
type DiscommandStruct struct {
|
||||||
|
@ -28,6 +31,22 @@ 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{},
|
||||||
|
@ -73,13 +92,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) {
|
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) {
|
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()
|
var Discommand *DiscommandStruct = new()
|
||||||
|
|
|
@ -16,6 +16,7 @@ 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) {
|
||||||
|
@ -65,10 +66,10 @@ func (c *Command) informationRun(s *discordgo.Session, m any) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) informationMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (c *Command) informationMessageRun(ctx *MsgContext) {
|
||||||
c.informationRun(s, m)
|
c.informationRun(ctx.Session, ctx.Msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) informationChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func (c *Command) informationChatInputRun(ctx *InterContext) {
|
||||||
c.informationRun(s, i)
|
c.informationRun(ctx.Session, ctx.Inter)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,8 @@ 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/bwmarrin/discordgo"
|
|
||||||
|
|
||||||
"github.com/LoperLee/golang-hangul-toolkit/hangul"
|
"github.com/LoperLee/golang-hangul-toolkit/hangul"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var LearnCommand *Command = &Command{
|
var LearnCommand *Command = &Command{
|
||||||
|
@ -42,6 +41,7 @@ var LearnCommand *Command = &Command{
|
||||||
"머핀아 배워 미간은_누구야? 이봇의_개발자요",
|
"머핀아 배워 미간은_누구야? 이봇의_개발자요",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Category: Chattings,
|
||||||
}
|
}
|
||||||
|
|
||||||
func addPrefix(arr []string) (newArr []string) {
|
func addPrefix(arr []string) (newArr []string) {
|
||||||
|
@ -51,44 +51,36 @@ func addPrefix(arr []string) (newArr []string) {
|
||||||
return
|
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
|
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(matches) < 2 {
|
if len(*args) < 2 {
|
||||||
content := strings.TrimPrefix(m.Content, configs.Config.Bot.Prefix)
|
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
|
||||||
command = strings.ReplaceAll(strings.Split(content, " ")[1], "_", "")
|
Title: "❌ 오류",
|
||||||
result = strings.ReplaceAll(strings.Split(content, " ")[2], "_", "")
|
Description: "올바르지 않ㅇ은 용법이에요.",
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
if command == "" || result == "" {
|
{
|
||||||
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
|
Name: "사용법",
|
||||||
Title: "❌ 오류",
|
Value: utils.InlineCode(c.DetailedDescription.Usage),
|
||||||
Description: "올바르지 않ㅇ은 용법이에요.",
|
Inline: true,
|
||||||
Fields: []*discordgo.MessageEmbedField{
|
|
||||||
{
|
|
||||||
Name: "사용법",
|
|
||||||
Value: utils.InlineCode(c.DetailedDescription.Usage),
|
|
||||||
Inline: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "예시",
|
|
||||||
Value: strings.Join(addPrefix(c.DetailedDescription.Examples), "\n"),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Color: int(utils.EFail),
|
{
|
||||||
}, m.Reference())
|
Name: "예시",
|
||||||
return
|
Value: strings.Join(addPrefix(c.DetailedDescription.Examples), "\n"),
|
||||||
}
|
},
|
||||||
} else {
|
},
|
||||||
command = matches[0][1]
|
Color: int(utils.EFail),
|
||||||
result = matches[1][1]
|
}, m.Reference())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
@ -206,10 +198,11 @@ func (c *Command) learnRun(s *discordgo.Session, m any) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) learnMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (c *Command) learnMessageRun(ctx *MsgContext) {
|
||||||
c.learnRun(s, m)
|
c.learnRun(ctx.Session, ctx.Msg, &ctx.Args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) learnChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func (c *Command) learnChatInputRun(ctx *InterContext) {
|
||||||
c.learnRun(s, i)
|
var args *[]string
|
||||||
|
c.learnRun(ctx.Session, ctx.Inter, args)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ 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) {
|
||||||
|
@ -114,10 +115,10 @@ func (c *Command) learnedDataListRun(s *discordgo.Session, m any) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) learnedDataListMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (c *Command) learnedDataListMessageRun(ctx *MsgContext) {
|
||||||
c.learnedDataListRun(s, m)
|
c.learnedDataListRun(ctx.Session, ctx.Msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) learnedDataListChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func (c *Command) learnedDataListChatInputRun(ctx *InterContext) {
|
||||||
c.learnedDataListRun(s, i)
|
c.learnedDataListRun(ctx.Session, ctx.Inter)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,17 @@ 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
|
||||||
|
@ -25,7 +36,8 @@ 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)
|
||||||
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 m.Author.ID == config.Train.UserID {
|
||||||
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
if _, err := databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||||
|
@ -104,7 +116,7 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.Discommand.MessageRun(command, s, m)
|
commands.Discommand.MessageRun(command, s, m, args[1:])
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,5 +2,5 @@ package utils
|
||||||
|
|
||||||
import "regexp"
|
import "regexp"
|
||||||
|
|
||||||
var ExtractQuotedText *regexp.Regexp = regexp.MustCompile("[\"'`](.*?)[\"'`]")
|
var FlexibleStringParser *regexp.Regexp = regexp.MustCompile("[^\\s\"'「」«»]+|\"([^\"]*)\"|'([^']*)'|「([^」]*)」|«([^»]*)»")
|
||||||
var Decimals *regexp.Regexp = regexp.MustCompile(`\d+`)
|
var Decimals *regexp.Regexp = regexp.MustCompile(`\d+`)
|
||||||
|
|
Loading…
Reference in a new issue