vegapokerbot/internal/handler/fsmwizard/remove_chat_member_state.go
Neur0toxine 1a28ab584a
Some checks are pending
continuous-integration/drone/push Build is running
move fsm wizard to its own package, little refactor
2024-05-13 19:21:34 +03:00

52 lines
1.4 KiB
Go

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)}
}
func (s *RemoveChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[*Wizard]) error {
if pl.Data.MyChatMember == nil {
s.LogError(mc.Move(WaitingForMemberWebhookStateID, pl))
}
return nil
}
func (s *RemoveChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) {
cr := s.App.DB().ForChat()
chat, err := cr.ByTelegramID(pl.Data.MyChatMember.Chat.ID)
if err != nil {
s.LogError(err)
return
}
if chat != nil && chat.ID > 0 {
user, _ := s.App.DB().ForUser().ByID(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)
}
s.LogError(cr.Delete(chat))
}
}
func (s *RemoveChatMemberState) ID() fsm.StateID {
return RemoveChatMemberStateID
}