feat: Add chat with slash command

This commit is contained in:
Siwoo Jeon 2025-06-02 22:47:13 +09:00
parent 7b0a35fbf6
commit 52f183e449
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
13 changed files with 78 additions and 10 deletions

View file

@ -2,22 +2,38 @@ package chatbot
import ( import (
"strings" "strings"
"time"
"git.wh64.net/muffin/goMuffin/configs" "git.wh64.net/muffin/goMuffin/configs"
"git.wh64.net/muffin/goMuffin/utils" "git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func ParseResult(content string, s *discordgo.Session, m *discordgo.MessageCreate) string { func ParseResult(content string, s *discordgo.Session, m any) string {
result := content result := content
userCreatedAt, _ := discordgo.SnowflakeTimestamp(m.Author.ID)
result = strings.ReplaceAll(result, "{user.name}", m.Author.Username) var user *discordgo.User
result = strings.ReplaceAll(result, "{user.mention}", m.Author.Mention()) var joinedAt *time.Time
result = strings.ReplaceAll(result, "{user.globalName}", m.Author.GlobalName)
result = strings.ReplaceAll(result, "{user.id}", m.Author.ID) switch m := m.(type) {
case *discordgo.MessageCreate:
case *utils.MessageCreate:
user = m.Author
joinedAt = &m.Member.JoinedAt
case *discordgo.IntegrationCreate:
case *utils.InteractionCreate:
user = m.Member.User
joinedAt = &m.Member.JoinedAt
}
userCreatedAt, _ := discordgo.SnowflakeTimestamp(user.ID)
result = strings.ReplaceAll(result, "{user.name}", user.Username)
result = strings.ReplaceAll(result, "{user.mention}", user.Mention())
result = strings.ReplaceAll(result, "{user.globalName}", user.GlobalName)
result = strings.ReplaceAll(result, "{user.id}", user.ID)
result = strings.ReplaceAll(result, "{user.createdAt}", utils.Time(&userCreatedAt, utils.RelativeTime)) result = strings.ReplaceAll(result, "{user.createdAt}", utils.Time(&userCreatedAt, utils.RelativeTime))
result = strings.ReplaceAll(result, "{user.joinedAt}", utils.Time(&m.Member.JoinedAt, utils.RelativeTime)) result = strings.ReplaceAll(result, "{user.joinedAt}", utils.Time(joinedAt, utils.RelativeTime))
result = strings.ReplaceAll(result, "{muffin.version}", configs.MUFFIN_VERSION) result = strings.ReplaceAll(result, "{muffin.version}", configs.MUFFIN_VERSION)
result = strings.ReplaceAll(result, "{muffin.updatedAt}", utils.Time(configs.UpdatedAt, utils.RelativeTime)) result = strings.ReplaceAll(result, "{muffin.updatedAt}", utils.Time(configs.UpdatedAt, utils.RelativeTime))

38
commands/chat.go Normal file
View file

@ -0,0 +1,38 @@
package commands
import (
"git.wh64.net/muffin/goMuffin/chatbot"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
)
var ChatCommand *Command = &Command{
ApplicationCommand: &discordgo.ApplicationCommand{
Name: "대화",
Description: "이 봇이랑 대화해요. (슬래시 커맨드 전용)",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "내용",
Description: "대화할 내용",
Required: true,
},
},
},
DetailedDescription: &DetailedDescription{
Usage: "/대화 (내용:대화할 내용)",
Examples: []string{"/대화 내용:안녕", "/대화 내용:냠냠"},
},
Category: Chatting,
RegisterApplicationCommand: true,
RegisterMessageCommand: false,
ChatInputRun: func(ctx *ChatInputContext) {
i := ctx.Inter
i.DeferReply(&discordgo.InteractionResponseData{})
result := chatbot.ParseResult(chatbot.ChatBot.GetResponse(i.Member.User.ID, i.Options["내용"].StringValue()), ctx.Inter.Session, i)
i.EditReply(&utils.InteractionEdit{
Content: &result,
})
},
}

View file

@ -43,6 +43,7 @@ var DataLengthCommand *Command = &Command{
}, },
Category: General, Category: General,
RegisterApplicationCommand: true, RegisterApplicationCommand: true,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
dataLengthRun(ctx.Msg.Session, ctx.Msg, ctx.Msg.Author.Username, ctx.Msg.Author.ID) dataLengthRun(ctx.Msg.Session, ctx.Msg, ctx.Msg.Author.Username, ctx.Msg.Author.ID)
}, },

View file

@ -31,6 +31,7 @@ var DeleteLearnedDataCommand *Command = &Command{
}, },
Category: Chatting, Category: Chatting,
RegisterApplicationCommand: true, RegisterApplicationCommand: true,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
command := strings.Join(*ctx.Args, " ") command := strings.Join(*ctx.Args, " ")
if command == "" { if command == "" {

View file

@ -29,6 +29,7 @@ type Command struct {
DetailedDescription *DetailedDescription DetailedDescription *DetailedDescription
Category Category Category Category
RegisterApplicationCommand bool RegisterApplicationCommand bool
RegisterMessageCommand bool
MessageRun messageRun MessageRun messageRun
ChatInputRun chatInputRun ChatInputRun chatInputRun
} }
@ -128,6 +129,10 @@ func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *disc
return return
} }
if !command.RegisterMessageCommand {
return
}
command.MessageRun(&MsgContext{&utils.MessageCreate{ command.MessageRun(&MsgContext{&utils.MessageCreate{
MessageCreate: m, MessageCreate: m,
Session: s, Session: s,
@ -136,7 +141,7 @@ func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *disc
} }
func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, i *discordgo.InteractionCreate) { func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, i *discordgo.InteractionCreate) {
if command, ok := d.Commands[name]; ok { if command, ok := d.Commands[name]; ok && command.RegisterApplicationCommand {
command.ChatInputRun(&ChatInputContext{&utils.InteractionCreate{ command.ChatInputRun(&ChatInputContext{&utils.InteractionCreate{
InteractionCreate: i, InteractionCreate: i,
Session: s, Session: s,

View file

@ -30,6 +30,7 @@ var HelpCommand *Command = &Command{
}, },
Category: General, Category: General,
RegisterApplicationCommand: true, RegisterApplicationCommand: true,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
helpRun(ctx.Msg.Session, ctx.Msg, strings.Join(*ctx.Args, " ")) helpRun(ctx.Msg.Session, ctx.Msg, strings.Join(*ctx.Args, " "))
}, },

View file

@ -18,6 +18,7 @@ var InformationCommand *Command = &Command{
}, },
Category: General, Category: General,
RegisterApplicationCommand: true, RegisterApplicationCommand: true,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
informationRun(ctx.Msg.Session, ctx.Msg) informationRun(ctx.Msg.Session, ctx.Msg)
}, },

View file

@ -58,6 +58,7 @@ var LearnCommand *Command = &Command{
}, },
Category: Chatting, Category: Chatting,
RegisterApplicationCommand: true, RegisterApplicationCommand: true,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
if len(*ctx.Args) < 2 { if len(*ctx.Args) < 2 {
utils.NewMessageSender(ctx.Msg). utils.NewMessageSender(ctx.Msg).

View file

@ -58,6 +58,7 @@ var LearnedDataListCommand *Command = &Command{
}, },
Category: Chatting, Category: Chatting,
RegisterApplicationCommand: true, RegisterApplicationCommand: true,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
var length int var length int

View file

@ -20,6 +20,7 @@ var ReloadPromptCommand *Command = &Command{
}, },
Category: DeveloperOnly, Category: DeveloperOnly,
RegisterApplicationCommand: false, RegisterApplicationCommand: false,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
err := chatbot.ChatBot.ReloadPrompt() err := chatbot.ChatBot.ReloadPrompt()
if err != nil { if err != nil {

View file

@ -19,6 +19,7 @@ var SwitchModeCommand *Command = &Command{
}, },
Category: DeveloperOnly, Category: DeveloperOnly,
RegisterApplicationCommand: false, RegisterApplicationCommand: false,
RegisterMessageCommand: true,
MessageRun: func(ctx *MsgContext) { MessageRun: func(ctx *MsgContext) {
chatbot.ChatBot.SwitchMode() chatbot.ChatBot.SwitchMode()

View file

@ -37,10 +37,10 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
args := argParser(content) args := argParser(content)
command := commands.Discommand.Aliases[args[0]] command := commands.Discommand.Aliases[args[0]]
if command == "" { if command == "" || command == "대화" {
s.ChannelTyping(m.ChannelID) s.ChannelTyping(m.ChannelID)
result := chatbot.ParseResult(chatbot.ChatBot.GetResponse(m.Author.ID, content), s, m) result := chatbot.ParseResult(chatbot.ChatBot.GetResponse(m.Author.ID, strings.TrimPrefix(content, "대화 ")), s, m)
utils.NewMessageSender(&utils.MessageCreate{ utils.NewMessageSender(&utils.MessageCreate{
MessageCreate: m, MessageCreate: m,
Session: s, Session: s,

View file

@ -31,6 +31,7 @@ func init() {
go commands.Discommand.LoadCommand(commands.DeleteLearnedDataCommand) go commands.Discommand.LoadCommand(commands.DeleteLearnedDataCommand)
go commands.Discommand.LoadCommand(commands.ReloadPromptCommand) go commands.Discommand.LoadCommand(commands.ReloadPromptCommand)
go commands.Discommand.LoadCommand(commands.SwitchModeCommand) go commands.Discommand.LoadCommand(commands.SwitchModeCommand)
go commands.Discommand.LoadCommand(commands.ChatCommand)
go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent) go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent)
go commands.Discommand.LoadComponent(components.PaginationEmbedComponent) go commands.Discommand.LoadComponent(components.PaginationEmbedComponent)