vegapokerbot/internal/handler/fsmwizard/remove_chat_member_state.go

57 lines
1.6 KiB
Go
Raw Normal View History

package fsmwizard
import (
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
)
const RemoveChatMemberStateID fsm.StateID = "remove_chat_member"
type RemoveChatMemberState struct {
State
}
func NewRemoveChatMemberState(app iface.App) fsm.IState[Wizard] {
return &RemoveChatMemberState{newBase(app)}
}
2024-05-14 14:44:41 +03:00
func (s *RemoveChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[Wizard]) error {
if pl.Data.MyChatMember == nil {
2024-05-13 19:46:55 +03:00
s.Move(mc, WaitingForMemberWebhookStateID, pl)
}
return nil
}
2024-05-14 14:44:41 +03:00
func (s *RemoveChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[Wizard]) {
if s.Payload.Chat == nil {
chat, err := s.App.DB().ForChat().ByTelegramID(s.Payload.Data.MyChatMember.Chat.ID)
if err != nil {
return
}
if chat != nil && chat.ID > 0 {
s.Payload.Chat = chat
}
}
2024-05-14 14:44:41 +03:00
if s.Payload.Chat != nil && s.Payload.Chat.ID > 0 {
user, _ := s.App.DB().ForUser().ByID(s.Payload.Chat.UserID)
if user != nil && user.ID > 0 && user.ChatID > 0 {
_, err := s.App.TG().SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(user.ChatID),
Text: s.Localizer(user.Language).Template(
"bot_was_removed_from_group", map[string]interface{}{"Name": pl.Data.MyChatMember.Chat.Title}),
ParseMode: telego.ModeMarkdown,
})
s.LogError(err)
}
2024-05-14 14:44:41 +03:00
s.LogError(s.App.DB().ForChat().Delete(s.Payload.Chat))
}
2024-05-13 19:46:55 +03:00
s.Move(mc, WaitingForMemberWebhookStateID, pl)
}
func (s *RemoveChatMemberState) ID() fsm.StateID {
return RemoveChatMemberStateID
}