feat: add component select chat
This commit is contained in:
parent
26c96e5637
commit
18a373f083
4 changed files with 74 additions and 1 deletions
61
components/selectChat.go
Normal file
61
components/selectChat.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package components
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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 SelectChatComponent *commands.Component = &commands.Component{
|
||||
Parse: func(ctx *commands.ComponentContext) bool {
|
||||
i := ctx.Inter
|
||||
customId := i.MessageComponentData().CustomID
|
||||
|
||||
if !strings.HasPrefix(customId, utils.SelectChat) {
|
||||
return false
|
||||
}
|
||||
|
||||
userId := utils.GetSelectChatUserId(customId)
|
||||
if i.User.ID != userId {
|
||||
i.Reply(&discordgo.InteractionResponseData{
|
||||
Flags: discordgo.MessageFlagsEphemeral | discordgo.MessageFlagsIsComponentsV2,
|
||||
Components: []discordgo.MessageComponent{
|
||||
utils.GetDeclineContainer(discordgo.TextDisplay{Content: "당신은 해당 권한이 없ㅇ어요."}),
|
||||
},
|
||||
})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
Run: func(ctx *commands.ComponentContext) error {
|
||||
i := ctx.Inter
|
||||
|
||||
err := i.DeferUpdate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, itemId := utils.GetSelectChatId(i.MessageComponentData().CustomID)
|
||||
_, err = databases.Database.Users.UpdateOne(context.TODO(), databases.User{UserId: i.User.ID}, bson.D{{
|
||||
Key: "$set",
|
||||
Value: databases.User{ChatId: id},
|
||||
}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
flags := discordgo.MessageFlagsIsComponentsV2
|
||||
return i.EditReply(&utils.InteractionEdit{
|
||||
Flags: &flags,
|
||||
Components: &[]discordgo.MessageComponent{
|
||||
utils.GetSuccessContainer(discordgo.TextDisplay{Content: fmt.Sprintf("%d번으로 채팅을 변경했어요.", itemId)}),
|
||||
},
|
||||
})
|
||||
},
|
||||
}
|
|
@ -7,7 +7,7 @@ import (
|
|||
"git.wh64.net/muffin/goMuffin/utils"
|
||||
)
|
||||
|
||||
const MUFFIN_VERSION = "6.0.0-madeleine_develop.250707a"
|
||||
const MUFFIN_VERSION = "6.0.0-madeleine_develop.250712a"
|
||||
|
||||
var updatedString string = utils.RegexpDecimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]
|
||||
|
||||
|
|
1
main.go
1
main.go
|
@ -41,6 +41,7 @@ func init() {
|
|||
go commands.Discommand.LoadComponent(components.PaginationEmbedComponent)
|
||||
go commands.Discommand.LoadComponent(components.RegisterComponent)
|
||||
go commands.Discommand.LoadComponent(components.DeregisterComponent)
|
||||
go commands.Discommand.LoadComponent(components.SelectChatComponent)
|
||||
|
||||
go commands.Discommand.LoadModal(modals.PaginationEmbedModal)
|
||||
}
|
||||
|
|
|
@ -123,3 +123,14 @@ func GetDeregisterUserId(customId string) string {
|
|||
func MakeSelectChat(id string, number int, userId string) string {
|
||||
return fmt.Sprintf("%sid=%s&no=%d&user_id=%s", DeleteLearnedData, id, number, userId)
|
||||
}
|
||||
|
||||
func GetSelectChatId(customId string) (id bson.ObjectID, itemId int) {
|
||||
id, _ = bson.ObjectIDFromHex(strings.ReplaceAll(RegexpDLDId.FindAllString(customId, 1)[0], "id=", ""))
|
||||
stringItemId := strings.ReplaceAll(RegexpDLDItemId.FindAllString(customId, 1)[0], "no=", "")
|
||||
itemId, _ = strconv.Atoi(stringItemId)
|
||||
return
|
||||
}
|
||||
|
||||
func GetSelectChatUserId(customId string) string {
|
||||
return strings.ReplaceAll(RegexpDLDUserId.FindAllString(customId, 1)[0], "user_id=", "")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue