diff --git a/chatbot/chatbot.go b/chatbot/chatbot.go index 5605500..e4e0474 100644 --- a/chatbot/chatbot.go +++ b/chatbot/chatbot.go @@ -34,7 +34,7 @@ func New(s *discordgo.Session) { } ChatBot = &Chatbot{ - Mode: ChatbotDefault, + Mode: ChatbotAI, Gemini: gemini, s: s, } @@ -54,6 +54,27 @@ func (c *Chatbot) SetMode(mode ChatbotMode) *Chatbot { return c } +func (c *Chatbot) SwitchMode() *Chatbot { + switch c.Mode { + case ChatbotAI: + c.SetMode(ChatbotMuffin) + case ChatbotMuffin: + c.SetMode(ChatbotAI) + } + return c +} + +func (c *Chatbot) ModeString() string { + switch c.Mode { + case ChatbotAI: + return "AI모드" + case ChatbotMuffin: + return "머핀 모드" + default: + return "알 수 없음" + } +} + func (c *Chatbot) ReloadPrompt() error { bin, err := os.ReadFile(configs.Config.Chatbot.Gemini.PromptPath) if err != nil { @@ -120,7 +141,7 @@ func getDefaultResponse(s *discordgo.Session, question string) string { func getAIResponse(question string) string { result, err := ChatBot.Gemini.Models.GenerateContent(context.TODO(), configs.Config.Chatbot.Gemini.Model, genai.Text(question), ChatBot.config) if err != nil { - ChatBot.Mode = ChatbotDefault + ChatBot.Mode = ChatbotMuffin fmt.Println(err) return "AI에 문제가 생겼ㅇ어요." } @@ -129,7 +150,7 @@ func getAIResponse(question string) string { func (c *Chatbot) GetResponse(question string) string { switch c.Mode { - case ChatbotDefault: + case ChatbotMuffin: return getDefaultResponse(c.s, question) default: return getAIResponse(question) diff --git a/chatbot/enum.go b/chatbot/enum.go index 9eb7319..a99968a 100644 --- a/chatbot/enum.go +++ b/chatbot/enum.go @@ -4,5 +4,5 @@ type ChatbotMode int const ( ChatbotAI ChatbotMode = iota - ChatbotDefault + ChatbotMuffin ) diff --git a/commands/discommand.go b/commands/discommand.go index 107c6eb..b1f28f8 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -3,6 +3,7 @@ package commands import ( "sync" + "git.wh64.net/muffin/goMuffin/configs" "git.wh64.net/muffin/goMuffin/utils" "github.com/bwmarrin/discordgo" ) @@ -118,6 +119,15 @@ func (d *DiscommandStruct) LoadModal(m *Modal) { func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) { if command, ok := d.Commands[name]; ok { + if command.Category == DeveloperOnly && m.Author.ID != configs.Config.Bot.OwnerId { + utils.NewMessageSender(&utils.MessageCreate{MessageCreate: m, Session: s}). + AddComponents(utils.GetErrorContainer(discordgo.TextDisplay{Content: "해당 명령어는 개발자만 사용 가능해요."})). + SetComponentsV2(true). + SetReply(true). + Send() + return + } + command.MessageRun(&MsgContext{&utils.MessageCreate{ MessageCreate: m, Session: s, diff --git a/commands/switchMode.go b/commands/switchMode.go new file mode 100644 index 0000000..5ee2ad3 --- /dev/null +++ b/commands/switchMode.go @@ -0,0 +1,27 @@ +package commands + +import ( + "fmt" + + "git.wh64.net/muffin/goMuffin/chatbot" + "git.wh64.net/muffin/goMuffin/utils" + "github.com/bwmarrin/discordgo" +) + +var SwitchModeCommand *Command = &Command{ + ApplicationCommand: &discordgo.ApplicationCommand{ + Name: "모드전환", + Description: "머핀봇의 대답을 변경합니다.", + }, + Category: DeveloperOnly, + RegisterApplicationCommand: false, + MessageRun: func(ctx *MsgContext) { + chatbot.ChatBot.SwitchMode() + + utils.NewMessageSender(ctx.Msg). + AddComponents(utils.GetSuccessContainer(discordgo.TextDisplay{Content: fmt.Sprintf("모드를 %s로 바꾸었어요.", chatbot.ChatBot.ModeString())})). + SetComponentsV2(true). + SetReply(true). + Send() + }, +} diff --git a/main.go b/main.go index ec068e8..c6bff8d 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "context" - "flag" + "fmt" "log" "os" "os/signal" @@ -16,7 +16,10 @@ import ( "git.wh64.net/muffin/goMuffin/databases" "git.wh64.net/muffin/goMuffin/handler" "git.wh64.net/muffin/goMuffin/modals" + "git.wh64.net/muffin/goMuffin/scripts" "github.com/bwmarrin/discordgo" + "github.com/devproje/commando" + "github.com/devproje/commando/types" ) func init() { @@ -27,6 +30,7 @@ func init() { go commands.Discommand.LoadCommand(commands.InformationCommand) go commands.Discommand.LoadCommand(commands.DeleteLearnedDataCommand) go commands.Discommand.LoadCommand(commands.ReloadPromptCommand) + go commands.Discommand.LoadCommand(commands.SwitchModeCommand) go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent) go commands.Discommand.LoadComponent(components.PaginationEmbedComponent) @@ -35,52 +39,48 @@ func init() { } func main() { - // command := commando.NewCommando(os.Args[1:]) + command := commando.NewCommando(os.Args[1:]) config := configs.Config - // if len(os.Args) > 1 { - // command.Root("delete-all-commands", "봇의 모든 슬래시 커맨드를 삭제합니다.", scripts.DeleteAllCommands, - // types.OptionData{ - // Name: "id", - // Desc: "봇의 디스코드 아이디", - // Type: types.STRING, - // }, - // types.OptionData{ - // Name: "isYes", - // Short: []string{"y"}, - // Type: types.BOOLEAN, - // }, - // ) + if len(os.Args) > 1 { + command.Root("delete-all-commands", "봇의 모든 슬래시 커맨드를 삭제합니다.", scripts.DeleteAllCommands, + types.OptionData{ + Name: "id", + Desc: "봇의 디스코드 아이디", + Type: types.STRING, + }, + types.OptionData{ + Name: "isYes", + Short: []string{"y"}, + Type: types.BOOLEAN, + }, + ) - // command.Root("export", "머핀봇의 데이터를 추출합니다.", scripts.ExportData, - // types.OptionData{ - // Name: "type", - // Desc: "파일형식을 지정합니다. (json, jsonl, finetune)", - // Type: types.STRING, - // }, - // types.OptionData{ - // Name: "export-path", - // Desc: "데이터를 저장할 위치를 지정합니다.", - // Type: types.STRING, - // }, - // types.OptionData{ - // Name: "refined", - // Desc: "머핀 데이터를 있는 그대로 추출할 지, 가려내서 추출할 지를 지정합니다.", - // Type: types.BOOLEAN, - // }, - // ) + command.Root("export", "머핀봇의 데이터를 추출합니다.", scripts.ExportData, + types.OptionData{ + Name: "type", + Desc: "파일형식을 지정합니다. (json, jsonl, finetune)", + Type: types.STRING, + }, + types.OptionData{ + Name: "export-path", + Desc: "데이터를 저장할 위치를 지정합니다.", + Type: types.STRING, + }, + types.OptionData{ + Name: "refined", + Desc: "머핀 데이터를 있는 그대로 추출할 지, 가려내서 추출할 지를 지정합니다.", + Type: types.BOOLEAN, + }, + ) - // err := command.Execute() - // if err != nil { - // _, _ = fmt.Fprintln(os.Stderr, err) - // os.Exit(1) - // } - // return - // } - - isAI := flag.Bool("ai", false, "시작할 때 AI모드로 설정합니다.") - - flag.Parse() + err := command.Execute() + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return + } dg, _ := discordgo.New("Bot " + config.Bot.Token) @@ -95,10 +95,6 @@ func main() { chatbot.New(dg) - if *isAI { - chatbot.ChatBot.SetMode(chatbot.ChatbotAI) - } - defer dg.Close() // 봇의 상태메세지 변경