diff --git a/commands/deregister.go b/commands/deregister.go new file mode 100644 index 0000000..79677bb --- /dev/null +++ b/commands/deregister.go @@ -0,0 +1,55 @@ +package commands + +import ( + "fmt" + + "git.wh64.net/muffin/goMuffin/configs" + "git.wh64.net/muffin/goMuffin/utils" + "github.com/bwmarrin/discordgo" +) + +var DeregisterCommand *Command = &Command{ + ApplicationCommand: &discordgo.ApplicationCommand{ + Name: "탈퇴", + Description: "이 봇에서 탈퇴해요.", + }, + DetailedDescription: &DetailedDescription{ + Usage: fmt.Sprintf("%s탈퇴", configs.Config.Bot.Prefix), + }, + Category: General, + RegisterMessageCommand: true, + RegisterApplicationCommand: true, + Flags: CommandFlagsIsRegistered, + MessageRun: func(ctx *MsgContext) { + deregisterRun(ctx.Msg, ctx.Msg.Author.ID, ctx.Msg.Session.State.User.Username) + }, +} + +func deregisterRun(m any, userId, botName string) { + utils.NewMessageSender(m). + AddComponents(discordgo.Container{ + Components: []discordgo.MessageComponent{ + discordgo.TextDisplay{ + Content: fmt.Sprintf("### %s 탈퇴\n- 정말로 해당 서비스에서 탈퇴하시겠어요?\n> 주의: **모든 데이터는 삭제되어요.**", botName), + }, + discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + discordgo.Button{ + CustomID: utils.MakeDeregisterAgree(userId), + Label: "탈퇴", + Style: discordgo.SuccessButton, + }, + discordgo.Button{ + CustomID: utils.MakeDeregisterDisagree(userId), + Label: "취소", + Style: discordgo.DangerButton, + }, + }, + }, + }, + }). + SetComponentsV2(true). + SetEphemeral(true). + SetReply(true). + Send() +} diff --git a/components/deregister.go b/components/deregister.go new file mode 100644 index 0000000..07224bd --- /dev/null +++ b/components/deregister.go @@ -0,0 +1,77 @@ +package components + +import ( + "context" + "log" + "strings" + + "git.wh64.net/muffin/goMuffin/commands" + "git.wh64.net/muffin/goMuffin/databases" + "git.wh64.net/muffin/goMuffin/utils" + "github.com/bwmarrin/discordgo" + "go.mongodb.org/mongo-driver/v2/bson" +) + +var DeregisterComponent *commands.Component = &commands.Component{ + Parse: func(ctx *commands.ComponentContext) bool { + customId := ctx.Inter.MessageComponentData().CustomID + if !strings.HasPrefix(customId, utils.DeregisterAgree) && !strings.HasPrefix(customId, utils.DeregisterDisagree) { + return false + } + + if ctx.Inter.User.ID != utils.GetDeregisterUserId(customId) { + return false + } + return true + }, + Run: func(ctx *commands.ComponentContext) { + ctx.Inter.DeferUpdate() + customId := ctx.Inter.MessageComponentData().CustomID + flags := discordgo.MessageFlagsIsComponentsV2 + + switch { + case strings.HasPrefix(customId, utils.DeregisterAgree): + filter := bson.D{{Key: "user_id", Value: ctx.Inter.User.ID}} + _, err := databases.Database.Users.DeleteOne(context.TODO(), filter) + if err != nil { + log.Println(err) + // 나중에 에러처리 바꿔야지 :( + return + } + + _, err = databases.Database.Learns.DeleteMany(context.TODO(), filter) + if err != nil { + log.Println(err) + // 나중에 에러처리 바꿔야지 :( + return + } + + _, err = databases.Database.Memory.DeleteMany(context.TODO(), filter) + if err != nil { + log.Println(err) + // 나중에 에러처리 바꿔야지 :( + return + } + + ctx.Inter.EditReply(&utils.InteractionEdit{ + Flags: &flags, + Components: &[]discordgo.MessageComponent{ + utils.GetSuccessContainer(discordgo.TextDisplay{ + Content: "탈퇴를 했어요.", + }), + }, + }) + return + case strings.HasPrefix(customId, utils.DeregisterDisagree): + ctx.Inter.EditReply(&utils.InteractionEdit{ + Flags: &flags, + Components: &[]discordgo.MessageComponent{ + utils.GetDeclineContainer(discordgo.TextDisplay{ + Content: "탈퇴를 거부했어요.", + }), + }, + }) + return + } + }, +} diff --git a/components/register.go b/components/register.go index 4298be5..c4a2757 100644 --- a/components/register.go +++ b/components/register.go @@ -20,7 +20,7 @@ var RegisterComponent *commands.Component = &commands.Component{ return false } - if ctx.Inter.User.ID != utils.GetServicesUserId(customId) { + if ctx.Inter.User.ID != utils.GetServiceUserId(customId) { return false } return true diff --git a/configs/version.go b/configs/version.go index 2493fd2..6e4b0e8 100644 --- a/configs/version.go +++ b/configs/version.go @@ -7,7 +7,7 @@ import ( "git.wh64.net/muffin/goMuffin/utils" ) -const MUFFIN_VERSION = "0.0.0-madeleine_canary.250606a" +const MUFFIN_VERSION = "0.0.0-madeleine_canary.250620a" var updatedString string = utils.RegexpDecimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0] diff --git a/main.go b/main.go index 73b8bfd..54afffc 100644 --- a/main.go +++ b/main.go @@ -33,10 +33,12 @@ func init() { go commands.Discommand.LoadCommand(commands.SwitchModeCommand) go commands.Discommand.LoadCommand(commands.ChatCommand) go commands.Discommand.LoadCommand(commands.RegisterCommand) + go commands.Discommand.LoadCommand(commands.DeregisterCommand) go commands.Discommand.LoadComponent(components.DeleteLearnedDataComponent) go commands.Discommand.LoadComponent(components.PaginationEmbedComponent) go commands.Discommand.LoadComponent(components.RegisterComponent) + go commands.Discommand.LoadComponent(components.DeregisterComponent) go commands.Discommand.LoadModal(modals.PaginationEmbedModal) } diff --git a/utils/customIds.go b/utils/customIds.go index b135a54..3e1f894 100644 --- a/utils/customIds.go +++ b/utils/customIds.go @@ -17,8 +17,11 @@ const ( PaginationEmbedModal = "#muffin-pages/modal$" PaginationEmbedSetPage = "#muffin-pages/modal/set$" - ServiceAgree = "#muffin/serviceAgree@" - ServiceDisagree = "#muffin/serviceDisagree@" + ServiceAgree = "#muffin/service/agree@" + ServiceDisagree = "#muffin/service/disagree@" + + DeregisterAgree = "#muffin/deregister/agree@" + DeregisterDisagree = "#muffin/deregister/disagree@" ) func MakeDeleteLearnedData(id string, number int, userId string) string { @@ -85,7 +88,7 @@ func MakeServiceDisagree(userId string) string { return fmt.Sprintf("%s%s", ServiceDisagree, userId) } -func GetServicesUserId(customId string) string { +func GetServiceUserId(customId string) string { switch { case strings.HasPrefix(customId, ServiceAgree): return customId[len(ServiceAgree):] @@ -95,3 +98,22 @@ func GetServicesUserId(customId string) string { return customId } } + +func MakeDeregisterAgree(userId string) string { + return fmt.Sprintf("%s%s", DeregisterAgree, userId) +} + +func MakeDeregisterDisagree(userId string) string { + return fmt.Sprintf("%s%s", DeregisterDisagree, userId) +} + +func GetDeregisterUserId(customId string) string { + switch { + case strings.HasPrefix(customId, DeregisterAgree): + return customId[len(DeregisterAgree):] + case strings.HasPrefix(customId, DeregisterDisagree): + return customId[len(DeregisterDisagree):] + default: + return customId + } +}