Merge pull request '5.0.1' (#1) from develop into main

Reviewed-on: #1
This commit is contained in:
Siwoo Jeon 2025-05-05 03:21:09 +09:00
commit a95bf28efa
13 changed files with 76 additions and 48 deletions

View file

@ -1,10 +1,10 @@
FROM golang:1.24.1 FROM golang:1.24.2
RUN mkdir /app RUN mkdir /app
WORKDIR /app WORKDIR /app
COPY . . COPY . .
RUN go build -o build/goMuffin git.wh64.net/muffin/goMuffin RUN make
ENTRYPOINT [ "./build/goMuffin" ] ENTRYPOINT [ "./build/goMuffin" ]

33
Makefile Normal file
View file

@ -0,0 +1,33 @@
APP_NAME := goMuffin
BIN_DIR := build
EXT :=
ifeq ($(OS),Windows_NT)
EXT := .exe
endif
BIN := $(BIN_DIR)/$(APP_NAME)$(EXT)
PKG := git.wh64.net/muffin/goMuffin
.PHONY: all build run fmt vet deps
all: build
build:
@mkdir -p $(BIN_DIR)
@go build -o $(BIN) $(PKG)
run:
@go run $(PKG)
clean:
@rm -rf $(BIN_DIR)
deps:
@go mod tidy
fmt:
@go fmt $(PKG)
vet:
@go vet $(PKG)

View file

@ -42,7 +42,7 @@ var DataLengthCommand *Command = &Command{
DetailedDescription: &DetailedDescription{ DetailedDescription: &DetailedDescription{
Usage: fmt.Sprintf("%s학습데이터량", configs.Config.Bot.Prefix), Usage: fmt.Sprintf("%s학습데이터량", configs.Config.Bot.Prefix),
}, },
Category: Generals, Category: General,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
dataLengthRun(ctx.Session, ctx.Msg) dataLengthRun(ctx.Session, ctx.Msg)
}, },
@ -51,11 +51,11 @@ var DataLengthCommand *Command = &Command{
}, },
} }
func getLength(data dataType, coll *mongo.Collection, filter bson.D) { func getLength(dType dataType, coll *mongo.Collection, filter bson.D) {
defer dataLengthWg.Done() defer dataLengthWg.Done()
var err error var err error
var cur *mongo.Cursor var cur *mongo.Cursor
var datas []bson.M var data []bson.M
cur, err = coll.Find(context.TODO(), filter) cur, err = coll.Find(context.TODO(), filter)
if err != nil { if err != nil {
@ -64,8 +64,8 @@ func getLength(data dataType, coll *mongo.Collection, filter bson.D) {
defer cur.Close(context.TODO()) defer cur.Close(context.TODO())
cur.All(context.TODO(), &datas) cur.All(context.TODO(), &data)
dataLengthCh <- chStruct{name: data, length: len(datas)} dataLengthCh <- chStruct{name: dType, length: len(data)}
} }
func dataLengthRun(s *discordgo.Session, m any) { func dataLengthRun(s *discordgo.Session, m any) {

View file

@ -29,19 +29,18 @@ var DeleteLearnedDataCommand *Command = &Command{
Usage: fmt.Sprintf("%s삭제 (삭제할 단어)", configs.Config.Bot.Prefix), Usage: fmt.Sprintf("%s삭제 (삭제할 단어)", configs.Config.Bot.Prefix),
Examples: []string{fmt.Sprintf("%s삭제 머핀", configs.Config.Bot.Prefix)}, Examples: []string{fmt.Sprintf("%s삭제 머핀", configs.Config.Bot.Prefix)},
}, },
Category: Chattings, Category: Chatting,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
deleteLearnedDataRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args) deleteLearnedDataRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args)
}, },
ChatInputRun: func(ctx *ChatInputContext) { ChatInputRun: func(ctx *ChatInputContext) {
var args *[]string deleteLearnedDataRun(ctx.Command, ctx.Session, ctx.Inter, nil)
deleteLearnedDataRun(ctx.Command, ctx.Session, ctx.Inter, args)
}, },
} }
func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]string) { func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]string) {
var command, userId, description string var command, userId, description string
var datas []databases.Learn var data []databases.Learn
var options []discordgo.SelectMenuOption var options []discordgo.SelectMenuOption
switch m := m.(type) { switch m := m.(type) {
@ -94,9 +93,9 @@ func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]strin
return return
} }
cur.All(context.TODO(), &datas) cur.All(context.TODO(), &data)
if len(datas) < 1 { if len(data) < 1 {
embed := &discordgo.MessageEmbed{ embed := &discordgo.MessageEmbed{
Title: "❌ 오류", Title: "❌ 오류",
Description: "해당 하는 지식ㅇ을 찾을 수 없어요.", Description: "해당 하는 지식ㅇ을 찾을 수 없어요.",
@ -114,8 +113,8 @@ func deleteLearnedDataRun(c *Command, s *discordgo.Session, m any, args *[]strin
return return
} }
for i := range len(datas) { for i := range len(data) {
data := datas[i] data := data[i]
options = append(options, discordgo.SelectMenuOption{ options = append(options, discordgo.SelectMenuOption{
Label: fmt.Sprintf("%d번 지식", i+1), Label: fmt.Sprintf("%d번 지식", i+1),

View file

@ -59,8 +59,8 @@ type Component struct {
} }
const ( const (
Chattings Category = "채팅" Chatting Category = "채팅"
Generals Category = "일반" General Category = "일반"
) )
var commandMutex sync.Mutex var commandMutex sync.Mutex

View file

@ -28,13 +28,12 @@ var HelpCommand *Command = &Command{
Usage: fmt.Sprintf("%s도움말 [명령어]", configs.Config.Bot.Prefix), Usage: fmt.Sprintf("%s도움말 [명령어]", configs.Config.Bot.Prefix),
Examples: []string{fmt.Sprintf("%s도움말", configs.Config.Bot.Prefix), fmt.Sprintf("%s도움말 배워", configs.Config.Bot.Prefix)}, Examples: []string{fmt.Sprintf("%s도움말", configs.Config.Bot.Prefix), fmt.Sprintf("%s도움말 배워", configs.Config.Bot.Prefix)},
}, },
Category: Generals, Category: General,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
helpRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args) helpRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args)
}, },
ChatInputRun: func(ctx *ChatInputContext) { ChatInputRun: func(ctx *ChatInputContext) {
var args *[]string helpRun(ctx.Command, ctx.Session, ctx.Inter, nil)
helpRun(ctx.Command, ctx.Session, ctx.Inter, args)
}, },
} }
@ -76,8 +75,8 @@ func helpRun(c *Command, s *discordgo.Session, m any, args *[]string) {
embed.Description = utils.CodeBlock( embed.Description = utils.CodeBlock(
"md", "md",
fmt.Sprintf("# 일반\n%s\n\n# 채팅\n%s", fmt.Sprintf("# 일반\n%s\n\n# 채팅\n%s",
strings.Join(getCommandsByCategory(Discommand, Generals), "\n"), strings.Join(getCommandsByCategory(Discommand, General), "\n"),
strings.Join(getCommandsByCategory(Discommand, Chattings), "\n")), strings.Join(getCommandsByCategory(Discommand, Chatting), "\n")),
) )
switch m := m.(type) { switch m := m.(type) {
@ -122,7 +121,7 @@ func helpRun(c *Command, s *discordgo.Session, m any, args *[]string) {
if command.DetailedDescription.Examples != nil { if command.DetailedDescription.Examples != nil {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{ embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "예시", Name: "예시",
Value: utils.CodeBlock("md", strings.Join(addPrefix(c.DetailedDescription.Examples), "\n")), Value: utils.CodeBlock("md", strings.Join(addPrefix(command.DetailedDescription.Examples), "\n")),
}) })
} else { } else {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{ embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{

View file

@ -17,7 +17,7 @@ var InformationCommand *Command = &Command{
DetailedDescription: &DetailedDescription{ DetailedDescription: &DetailedDescription{
Usage: fmt.Sprintf("%s정보", configs.Config.Bot.Prefix), Usage: fmt.Sprintf("%s정보", configs.Config.Bot.Prefix),
}, },
Category: Generals, Category: General,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
informationRun(ctx.Session, ctx.Msg) informationRun(ctx.Session, ctx.Msg)
}, },

View file

@ -55,13 +55,12 @@ var LearnCommand *Command = &Command{
fmt.Sprintf("%s배워 \"나의 아이디를 알려줘\" \"너의 아이디는 {user.id}야.\"", configs.Config.Bot.Prefix), fmt.Sprintf("%s배워 \"나의 아이디를 알려줘\" \"너의 아이디는 {user.id}야.\"", configs.Config.Bot.Prefix),
}, },
}, },
Category: Chattings, Category: Chatting,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
learnRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args) learnRun(ctx.Command, ctx.Session, ctx.Msg, &ctx.Args)
}, },
ChatInputRun: func(ctx *ChatInputContext) { ChatInputRun: func(ctx *ChatInputContext) {
var args *[]string learnRun(ctx.Command, ctx.Session, ctx.Inter, nil)
learnRun(ctx.Command, ctx.Session, ctx.Inter, args)
}, },
} }

View file

@ -17,13 +17,13 @@ var LearnedDataListCommand *Command = &Command{
ApplicationCommand: &discordgo.ApplicationCommand{ ApplicationCommand: &discordgo.ApplicationCommand{
Type: discordgo.ChatApplicationCommand, Type: discordgo.ChatApplicationCommand,
Name: "리스트", Name: "리스트",
Description: "당신이 가ㄹ르쳐준 단어를 나열해요.", Description: "당신이 가ㄹ르쳐준 지식을 나열해요.",
}, },
Aliases: []string{"list", "목록", "지식목록"}, Aliases: []string{"list", "목록", "지식목록"},
DetailedDescription: &DetailedDescription{ DetailedDescription: &DetailedDescription{
Usage: fmt.Sprintf("%s리스트", configs.Config.Bot.Prefix), Usage: fmt.Sprintf("%s리스트", configs.Config.Bot.Prefix),
}, },
Category: Chattings, Category: Chatting,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
learnedDataListRun(ctx.Session, ctx.Msg) learnedDataListRun(ctx.Session, ctx.Msg)
}, },
@ -32,8 +32,8 @@ var LearnedDataListCommand *Command = &Command{
}, },
} }
func getDescriptions(datas *[]databases.Learn) (descriptions []string) { func getDescriptions(data *[]databases.Learn) (descriptions []string) {
for _, data := range *datas { for _, data := range *data {
descriptions = append(descriptions, fmt.Sprintf("- %s: %s", data.Command, data.Result)) descriptions = append(descriptions, fmt.Sprintf("- %s: %s", data.Command, data.Result))
} }
return return
@ -41,7 +41,8 @@ func getDescriptions(datas *[]databases.Learn) (descriptions []string) {
func learnedDataListRun(s *discordgo.Session, m any) { func learnedDataListRun(s *discordgo.Session, m any) {
var userId, globalName, avatarUrl string var userId, globalName, avatarUrl string
var datas []databases.Learn var data []databases.Learn
switch m := m.(type) { switch m := m.(type) {
case *discordgo.MessageCreate: case *discordgo.MessageCreate:
userId = m.Author.ID userId = m.Author.ID
@ -52,7 +53,7 @@ func learnedDataListRun(s *discordgo.Session, m any) {
userId = m.Member.User.ID userId = m.Member.User.ID
globalName = m.Member.User.GlobalName globalName = m.Member.User.GlobalName
avatarUrl = m.User.AvatarURL("512") avatarUrl = m.Member.User.AvatarURL("512")
} }
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}}) cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
@ -95,11 +96,11 @@ func learnedDataListRun(s *discordgo.Session, m any) {
defer cur.Close(context.TODO()) defer cur.Close(context.TODO())
cur.All(context.TODO(), &datas) cur.All(context.TODO(), &data)
embed := &discordgo.MessageEmbed{ embed := &discordgo.MessageEmbed{
Title: fmt.Sprintf("%s님이 알려주신 지식", globalName), Title: fmt.Sprintf("%s님이 알려주신 지식", globalName),
Description: utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n%s", len(datas), strings.Join(getDescriptions(&datas), "\n"))), Description: utils.CodeBlock("md", fmt.Sprintf("# 총 %d개에요.\n%s", len(data), strings.Join(getDescriptions(&data), "\n"))),
Color: utils.EmbedDefault, Color: utils.EmbedDefault,
Thumbnail: &discordgo.MessageEmbedThumbnail{ Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: avatarUrl, URL: avatarUrl,

View file

@ -7,7 +7,7 @@ import (
"git.wh64.net/muffin/goMuffin/utils" "git.wh64.net/muffin/goMuffin/utils"
) )
const MUFFIN_VERSION = "5.0.0-gopher_release.250413a" const MUFFIN_VERSION = "5.0.1-gopher_release.250505a"
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0] var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]

15
main.go
View file

@ -15,8 +15,7 @@ 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/handler" "git.wh64.net/muffin/goMuffin/handler"
dbmigrate "git.wh64.net/muffin/goMuffin/scripts/dbMigrate" "git.wh64.net/muffin/goMuffin/scripts"
deleteallcommands "git.wh64.net/muffin/goMuffin/scripts/deleteAllCommands"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -26,11 +25,11 @@ func main() {
if len(os.Args) > 1 { if len(os.Args) > 1 {
switch strings.ToLower(os.Args[1]) { switch strings.ToLower(os.Args[1]) {
case "dbmigrate": case "dbmigrate":
dbmigrate.DBMigrate() scripts.DBMigrate()
case "deleteallcommands": case "deleteallcommands":
deleteallcommands.DeleteAllCommands() scripts.DeleteAllCommands()
default: default:
panic(fmt.Errorf("[goMuffin] 명령어 인자에는 dbmigrate나 deleteallcommands만 올 수 있어요.")) log.Fatalln(fmt.Errorf("[goMuffin] 명령어 인자에는 dbmigrate나 deleteallcommands만 올 수 있어요"))
} }
return return
} }
@ -54,6 +53,7 @@ func main() {
go dg.AddHandler(handler.InteractionCreate) go dg.AddHandler(handler.InteractionCreate)
dg.Open() dg.Open()
defer dg.Close()
go func() { go func() {
for { for {
@ -77,10 +77,7 @@ func main() {
go dg.ApplicationCommandCreate(dg.State.User.ID, "", cmd.ApplicationCommand) go dg.ApplicationCommandCreate(dg.State.User.ID, "", cmd.ApplicationCommand)
} }
defer func() { defer databases.Client.Disconnect(context.TODO())
dg.Close()
databases.Client.Disconnect(context.TODO())
}()
log.Println("[goMuffin] 봇이 실행되고 있어요. 버전:", configs.MUFFIN_VERSION) log.Println("[goMuffin] 봇이 실행되고 있어요. 버전:", configs.MUFFIN_VERSION)
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)

View file

@ -1,4 +1,4 @@
package dbmigrate package scripts
import ( import (
"context" "context"

View file

@ -1,4 +1,4 @@
package deleteallcommands package scripts
import ( import (
"flag" "flag"
@ -25,7 +25,7 @@ func DeleteAllCommands() {
} }
if *id == "" { if *id == "" {
panic(fmt.Errorf("--id 플래그의 값이 필요해요.")) panic(fmt.Errorf("--id 플래그의 값이 필요해요"))
} }
c := http.Client{} c := http.Client{}