feat: Add learn command
This commit is contained in:
parent
87f16948d6
commit
e2c85c1ccb
8 changed files with 239 additions and 4 deletions
|
@ -1,6 +1,8 @@
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
// "fmt"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,6 +18,7 @@ type Command struct {
|
||||||
*discordgo.ApplicationCommand
|
*discordgo.ApplicationCommand
|
||||||
Aliases []string
|
Aliases []string
|
||||||
DetailedDescription *DetailedDescription
|
DetailedDescription *DetailedDescription
|
||||||
|
discommand *DiscommandStruct
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiscommandStruct struct {
|
type DiscommandStruct struct {
|
||||||
|
@ -35,17 +38,22 @@ func new() *DiscommandStruct {
|
||||||
|
|
||||||
go discommand.loadCommands(HelpCommand)
|
go discommand.loadCommands(HelpCommand)
|
||||||
go discommand.loadCommands(DataLengthCommand)
|
go discommand.loadCommands(DataLengthCommand)
|
||||||
|
go discommand.loadCommands(LearnCommand)
|
||||||
|
|
||||||
go discommand.addMessageRun(HelpCommand.Name, HelpCommand.helpMessageRun)
|
go discommand.addMessageRun(HelpCommand.Name, HelpCommand.helpMessageRun)
|
||||||
go discommand.addMessageRun(DataLengthCommand.Name, DataLengthCommand.dataLengthMessageRun)
|
go discommand.addMessageRun(DataLengthCommand.Name, DataLengthCommand.dataLengthMessageRun)
|
||||||
|
go discommand.addMessageRun(LearnCommand.Name, LearnCommand.learnMessageRun)
|
||||||
|
|
||||||
go discommand.addChatInputRun(DataLengthCommand.Name, DataLengthCommand.dataLenghChatInputRun)
|
go discommand.addChatInputRun(DataLengthCommand.Name, DataLengthCommand.dataLenghChatInputRun)
|
||||||
|
go discommand.addChatInputRun(LearnCommand.Name, LearnCommand.learnChatInputRun)
|
||||||
return &discommand
|
return &discommand
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DiscommandStruct) loadCommands(command *Command) {
|
func (d *DiscommandStruct) loadCommands(command *Command) {
|
||||||
d.Commands[command.Name] = command
|
d.Commands[command.Name] = command
|
||||||
d.Aliases[command.Name] = command.Name
|
d.Aliases[command.Name] = command.Name
|
||||||
|
// fmt.Println(command.Name)
|
||||||
|
command.discommand = d
|
||||||
|
|
||||||
for _, alias := range command.Aliases {
|
for _, alias := range command.Aliases {
|
||||||
d.Aliases[alias] = command.Name
|
d.Aliases[alias] = command.Name
|
||||||
|
|
215
commands/learn.go
Normal file
215
commands/learn.go
Normal file
|
@ -0,0 +1,215 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
|
var LearnCommand *Command = &Command{
|
||||||
|
ApplicationCommand: &discordgo.ApplicationCommand{
|
||||||
|
Type: discordgo.ChatApplicationCommand,
|
||||||
|
Name: "배워",
|
||||||
|
Description: "단어를 가르치는 명령ㅇ어에요.",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "단어",
|
||||||
|
Description: "등록할 단어를 입력해주세요.",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "대답",
|
||||||
|
Description: "해당 단어의 대답을 입력해주세요.",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Aliases: []string{"공부"},
|
||||||
|
DetailedDescription: &DetailedDescription{
|
||||||
|
Usage: "머핀아 배워 (등록할 단어) (대답)",
|
||||||
|
Examples: []string{"머핀아 배워 안녕 안녕!",
|
||||||
|
"머핀아 배워 \"야 죽을래?\" \"아니요 ㅠㅠㅠ\"",
|
||||||
|
"머핀아 배워 미간은_누구야? 이봇의_개발자요",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func addPrefix(arr []string) (newArr []string) {
|
||||||
|
for _, item := range arr {
|
||||||
|
newArr = append(newArr, "- "+item)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Command) learnRun(s *discordgo.Session, m any) {
|
||||||
|
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.Split(content, " ")[1]
|
||||||
|
result = 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"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Color: int(utils.EFail),
|
||||||
|
}, m.Reference())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
command = matches[0][1]
|
||||||
|
result = matches[1][1]
|
||||||
|
}
|
||||||
|
|
||||||
|
case *discordgo.InteractionCreate:
|
||||||
|
s.InteractionRespond(m.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
userId = m.Member.User.ID
|
||||||
|
|
||||||
|
optsMap := map[string]*discordgo.ApplicationCommandInteractionDataOption{}
|
||||||
|
for _, opt := range m.ApplicationCommandData().Options {
|
||||||
|
optsMap[opt.Name] = opt
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt, ok := optsMap["단어"]; ok {
|
||||||
|
command = opt.StringValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt, ok := optsMap["대답"]; ok {
|
||||||
|
result = opt.StringValue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, command := range c.discommand.Commands {
|
||||||
|
igCommands = append(igCommands, command.Name)
|
||||||
|
igCommands = append(igCommands, command.Aliases...)
|
||||||
|
}
|
||||||
|
|
||||||
|
ignores := []string{"미간", "Migan", "migan", "간미"}
|
||||||
|
ignores = append(ignores, igCommands...)
|
||||||
|
|
||||||
|
disallows := []string{
|
||||||
|
"@everyone",
|
||||||
|
"@here",
|
||||||
|
"<@" + configs.Config.Bot.OwnerId + ">"}
|
||||||
|
|
||||||
|
for _, ig := range ignores {
|
||||||
|
if strings.Contains(command, ig) {
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "❌ 오류",
|
||||||
|
Description: "해ㄷ당 단어는 배우기 껄끄ㄹ럽네요.",
|
||||||
|
Color: int(utils.EFail),
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m := m.(type) {
|
||||||
|
case *discordgo.MessageCreate:
|
||||||
|
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||||
|
case *discordgo.InteractionCreate:
|
||||||
|
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, di := range disallows {
|
||||||
|
if strings.Contains(result, di) {
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "❌ 오류",
|
||||||
|
Description: "해당 단ㅇ어의 대답으로 하기 좀 그렇ㄴ네요.",
|
||||||
|
Color: int(utils.EFail),
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m := m.(type) {
|
||||||
|
case *discordgo.MessageCreate:
|
||||||
|
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||||
|
case *discordgo.InteractionCreate:
|
||||||
|
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := databases.Learns.InsertOne(context.TODO(), databases.InsertLearn{
|
||||||
|
Command: command,
|
||||||
|
Result: result,
|
||||||
|
UserId: userId,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "❌ 오류",
|
||||||
|
Description: "단어를 배우는데 오류가 생겼어요.",
|
||||||
|
Color: int(utils.EFail),
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m := m.(type) {
|
||||||
|
case *discordgo.MessageCreate:
|
||||||
|
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||||
|
case *discordgo.InteractionCreate:
|
||||||
|
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "✅ 성공",
|
||||||
|
Description: hangul.GetJosa(command, hangul.EUL_REUL) + " 배웠어요.",
|
||||||
|
Color: int(utils.ESuccess),
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m := m.(type) {
|
||||||
|
case *discordgo.MessageCreate:
|
||||||
|
s.ChannelMessageSendEmbedReply(m.ChannelID, embed, m.Reference())
|
||||||
|
case *discordgo.InteractionCreate:
|
||||||
|
s.InteractionResponseEdit(m.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Embeds: &[]*discordgo.MessageEmbed{embed},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Command) learnMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
c.learnRun(s, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Command) learnChatInputRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
c.learnRun(s, i)
|
||||||
|
}
|
|
@ -8,11 +8,11 @@ import (
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var MUFFIN_VERSION = "0.0.0-gopher_canary.250326a"
|
|
||||||
|
|
||||||
type botConfig struct {
|
type botConfig struct {
|
||||||
Token string
|
Token string
|
||||||
Prefix string
|
Prefix string
|
||||||
|
OwnerId string
|
||||||
}
|
}
|
||||||
|
|
||||||
type trainConfig struct {
|
type trainConfig struct {
|
||||||
|
@ -41,6 +41,7 @@ func loadConfig() *MuffinConfig {
|
||||||
func setConfig(config *MuffinConfig) {
|
func setConfig(config *MuffinConfig) {
|
||||||
config.Bot.Prefix = os.Getenv("BOT_PREFIX")
|
config.Bot.Prefix = os.Getenv("BOT_PREFIX")
|
||||||
config.Bot.Token = os.Getenv("BOT_TOKEN")
|
config.Bot.Token = os.Getenv("BOT_TOKEN")
|
||||||
|
config.Bot.OwnerId = os.Getenv("BOT_OWNER_ID")
|
||||||
|
|
||||||
config.Train.UserID = os.Getenv("TRAIN_USER_ID")
|
config.Train.UserID = os.Getenv("TRAIN_USER_ID")
|
||||||
|
|
||||||
|
|
3
configs/version.go
Normal file
3
configs/version.go
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
package configs
|
||||||
|
|
||||||
|
var MUFFIN_VERSION = "0.0.0-gopher_canary.250328a"
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module git.wh64.net/muffin/goMuffin
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/LoperLee/golang-hangul-toolkit v1.1.0
|
||||||
github.com/bwmarrin/discordgo v0.28.1
|
github.com/bwmarrin/discordgo v0.28.1
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
go.mongodb.org/mongo-driver/v2 v2.1.0
|
go.mongodb.org/mongo-driver/v2 v2.1.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
||||||
|
github.com/LoperLee/golang-hangul-toolkit v1.1.0 h1:JEyLpLyA2hDQwWY9oCprHClnKIdkYVOSJzAat2uFX/A=
|
||||||
|
github.com/LoperLee/golang-hangul-toolkit v1.1.0/go.mod h1:CDbZ23/IL4v2ovWIOb7xDEiFcSc0pIIbbYTpg+gP+Sk=
|
||||||
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
|
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
|
||||||
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
|
|
@ -25,7 +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)
|
||||||
command := commands.Discommand.Aliases[content]
|
command := commands.Discommand.Aliases[strings.Split(content, " ")[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{
|
||||||
|
|
5
utils/regexp.go
Normal file
5
utils/regexp.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
var ExtractQuotedText *regexp.Regexp = regexp.MustCompile("[\"'`](.*?)[\"'`]")
|
Loading…
Reference in a new issue