diff --git a/commands/chat.go b/commands/chat.go index 7e2b0a2..d476356 100644 --- a/commands/chat.go +++ b/commands/chat.go @@ -28,7 +28,7 @@ var ChatCommand *Command = &Command{ Category: Chatting, RegisterApplicationCommand: true, RegisterMessageCommand: false, - Flags: CommandFlagsIsRegistered, + Flags: CommandFlagsIsRegistered | CommandFlagsIsBlocked, ChatInputRun: func(ctx *ChatInputContext) error { i := ctx.Inter i.DeferReply(&discordgo.InteractionResponseData{}) diff --git a/commands/dataLength.go b/commands/dataLength.go index 974cec8..b7941ae 100644 --- a/commands/dataLength.go +++ b/commands/dataLength.go @@ -44,7 +44,7 @@ var DataLengthCommand *Command = &Command{ Category: General, RegisterApplicationCommand: true, RegisterMessageCommand: true, - Flags: CommandFlagsIsRegistered, + Flags: CommandFlagsIsRegistered | CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { return dataLengthRun(ctx.Msg.Session, ctx.Msg, ctx.Msg.Author.Username, ctx.Msg.Author.ID) }, diff --git a/commands/deleteLearnedData.go b/commands/deleteLearnedData.go index 26fdff7..9fb2b5e 100644 --- a/commands/deleteLearnedData.go +++ b/commands/deleteLearnedData.go @@ -32,7 +32,7 @@ var DeleteLearnedDataCommand *Command = &Command{ Category: Chatting, RegisterApplicationCommand: true, RegisterMessageCommand: true, - Flags: CommandFlagsIsRegistered, + Flags: CommandFlagsIsRegistered | CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { command := strings.Join(*ctx.Args, " ") if command == "" { diff --git a/commands/deregister.go b/commands/deregister.go index 4c3424b..c9cd356 100644 --- a/commands/deregister.go +++ b/commands/deregister.go @@ -19,7 +19,7 @@ var DeregisterCommand *Command = &Command{ Category: General, RegisterMessageCommand: true, RegisterApplicationCommand: true, - Flags: CommandFlagsIsRegistered, + Flags: CommandFlagsIsRegistered | CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { return deregisterRun(ctx.Msg, ctx.Msg.Author.ID, ctx.Msg.Session.State.User.Username) }, diff --git a/commands/discommand.go b/commands/discommand.go index 0c6f874..f4f31be 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -84,6 +84,7 @@ const ( const ( CommandFlagsIsDeveloper CommandFlags = 1 << iota CommandFlagsIsRegistered + CommandFlagsIsBlocked ) var ( @@ -151,6 +152,17 @@ func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, msg *di return nil } + blocked, reason := databases.Database.IsUserBlocked(m.Author.ID) + if command.Flags&CommandFlagsIsBlocked != 0 && blocked { + user, _ := s.User(m.Author.ID) + utils.NewMessageSender(m). + AddComponents(utils.GetUserIsBlockedContainer(user.GlobalName, reason)). + SetComponentsV2(true). + SetReply(true). + Send() + return nil + } + return command.MessageRun(&MsgContext{m, &args, command}) } return nil @@ -186,6 +198,17 @@ func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, inter return nil } + blocked, reason := databases.Database.IsUserBlocked(i.User.ID) + if command.Flags&CommandFlagsIsBlocked != 0 && blocked { + user, _ := s.User(i.User.ID) + utils.NewMessageSender(i). + AddComponents(utils.GetUserIsBlockedContainer(user.GlobalName, reason)). + SetComponentsV2(true). + SetReply(true). + Send() + return nil + } + return command.ChatInputRun(&ChatInputContext{i, command}) } return nil diff --git a/commands/help.go b/commands/help.go index 075a3cb..d98da68 100644 --- a/commands/help.go +++ b/commands/help.go @@ -31,6 +31,7 @@ var HelpCommand *Command = &Command{ Category: General, RegisterApplicationCommand: true, RegisterMessageCommand: true, + Flags: CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { return helpRun(ctx.Msg.Session, ctx.Msg, strings.Join(*ctx.Args, " ")) }, diff --git a/commands/information.go b/commands/information.go index 9b2a972..ef46957 100644 --- a/commands/information.go +++ b/commands/information.go @@ -19,6 +19,7 @@ var InformationCommand *Command = &Command{ Category: General, RegisterApplicationCommand: true, RegisterMessageCommand: true, + Flags: CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { return informationRun(ctx.Msg.Session, ctx.Msg) }, diff --git a/commands/learn.go b/commands/learn.go index 06cc061..d9859fa 100644 --- a/commands/learn.go +++ b/commands/learn.go @@ -58,7 +58,7 @@ var LearnCommand *Command = &Command{ Category: Chatting, RegisterApplicationCommand: true, RegisterMessageCommand: true, - Flags: CommandFlagsIsRegistered, + Flags: CommandFlagsIsRegistered | CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { if len(*ctx.Args) < 2 { utils.NewMessageSender(ctx.Msg). diff --git a/commands/learnedDataList.go b/commands/learnedDataList.go index 60bf19a..e5f4dde 100644 --- a/commands/learnedDataList.go +++ b/commands/learnedDataList.go @@ -53,7 +53,7 @@ var LearnedDataListCommand *Command = &Command{ Category: Chatting, RegisterApplicationCommand: true, RegisterMessageCommand: true, - Flags: CommandFlagsIsRegistered, + Flags: CommandFlagsIsRegistered | CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { var length int diff --git a/commands/register.go b/commands/register.go index a1fd972..f0a424a 100644 --- a/commands/register.go +++ b/commands/register.go @@ -20,6 +20,7 @@ var RegisterCommand *Command = &Command{ Category: General, RegisterMessageCommand: true, RegisterApplicationCommand: true, + Flags: CommandFlagsIsBlocked, MessageRun: func(ctx *MsgContext) error { return registerRun(ctx.Msg, ctx.Msg.Author.ID, ctx.Msg.Session.State.User.Username) }, diff --git a/databases/User.go b/databases/User.go index aed020f..c5318dc 100644 --- a/databases/User.go +++ b/databases/User.go @@ -20,3 +20,9 @@ func (d *MuffinDatabase) IsUser(userId string) bool { d.Users.FindOne(context.TODO(), bson.D{{Key: "user_id", Value: userId}}).Decode(&user) return user != nil } + +func (d *MuffinDatabase) IsUserBlocked(userId string) (bool, string) { + var user User + d.Users.FindOne(context.TODO(), bson.D{{Key: "user_id", Value: userId}}).Decode(&user) + return user.Blocked, user.BlockedReason +} diff --git a/handler/messageCreate.go b/handler/messageCreate.go index 4401ded..6cf7d5d 100644 --- a/handler/messageCreate.go +++ b/handler/messageCreate.go @@ -49,6 +49,18 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { Send() return } + + blocked, reason := databases.Database.IsUserBlocked(m.Author.ID) + if blocked { + user, _ := s.User(m.Author.ID) + utils.NewMessageSender(m). + AddComponents(utils.GetUserIsBlockedContainer(user.GlobalName, reason)). + SetComponentsV2(true). + SetReply(true). + Send() + return + } + s.ChannelTyping(m.ChannelID) str, err := chatbot.ChatBot.GetResponse(m.Author, strings.TrimPrefix(content, "대화 ")) diff --git a/utils/embed.go b/utils/embed.go index 946466f..ecd3aea 100644 --- a/utils/embed.go +++ b/utils/embed.go @@ -56,3 +56,9 @@ func GetUserIsNotRegisteredErrContainer(prefix string) *discordgo.Container { Content: fmt.Sprintf("해당 기능은 등록된 사용자만 쓸 수 있어요. `%s가입`으로 가입해주새요.", prefix), }) } + +func GetUserIsBlockedContainer(globalName, reason string) *discordgo.Container { + return GetDeclineContainer(discordgo.TextDisplay{ + Content: fmt.Sprintf("- %s님은 서비스에서 차단되었어요.\n> 사유: %s", globalName, reason), + }) +}