code refactor
This commit is contained in:
parent
7275b93666
commit
d157f9b2c9
6 changed files with 187 additions and 193 deletions
|
|
@ -12,37 +12,21 @@ import (
|
|||
|
||||
// HandleCallbackQuery processes inline button callbacks (approve/decline/leave).
|
||||
func (bot *Bot) HandleCallbackQuery(query *api.CallbackQuery) {
|
||||
data := strings.Join(strings.Split(query.Data, "_"), " ")
|
||||
var action string
|
||||
var args int64
|
||||
_, err := fmt.Sscanf(data, "%s %d", &action, &args)
|
||||
action, args, err := parseCallbackData(query.Data)
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse callback data: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if action == "leave" {
|
||||
utils.LeaveChatRequest(bot.API, []int64{args})
|
||||
utils.EditMessage(bot.API, query.Message.Chat.ID, query.Message.MessageID,
|
||||
fmt.Sprintf("We have left chat <i>%d</i>", args))
|
||||
callback := api.NewCallback(query.ID, "")
|
||||
bot.API.Request(callback)
|
||||
bot.handleLeaveAction(query, args)
|
||||
return
|
||||
}
|
||||
|
||||
user := bot.GetPendingUser(args)
|
||||
if user == nil {
|
||||
log.Printf("No pending request for user ID %d", args)
|
||||
msg := api.NewMessage(query.Message.Chat.ID, "Unable to find user, bot may have restarted")
|
||||
msg.ReplyParameters = api.ReplyParameters{MessageID: query.Message.MessageID, ChatID: query.Message.Chat.ID}
|
||||
r, _ := bot.API.Send(msg)
|
||||
|
||||
edit := api.NewEditMessageText(query.Message.Chat.ID, r.ReplyToMessage.MessageID, r.ReplyToMessage.Text)
|
||||
edit.Entities = r.ReplyToMessage.Entities
|
||||
bot.API.Send(edit)
|
||||
|
||||
callback := api.NewCallback(query.ID, "")
|
||||
bot.API.Request(callback)
|
||||
bot.handleMissingUser(query)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -56,98 +40,83 @@ func (bot *Bot) HandleCallbackQuery(query *api.CallbackQuery) {
|
|||
bot.handleDeclineRequest(query, user, userString, adminUserString)
|
||||
}
|
||||
|
||||
if bot.Config.DeleteRequestAfterDecision {
|
||||
deleteTimer := time.NewTimer(10 * time.Second)
|
||||
go func() {
|
||||
defer deleteTimer.Stop()
|
||||
<-deleteTimer.C
|
||||
del := api.NewDeleteMessage(query.Message.Chat.ID, query.Message.MessageID)
|
||||
bot.API.Send(del)
|
||||
}()
|
||||
}
|
||||
|
||||
bot.DeletePendingUser(args)
|
||||
|
||||
if bot.Config.DeleteRequestAfterDecision {
|
||||
go bot.scheduleMessageDeletion(query.Message.Chat.ID, query.Message.MessageID, 10*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// handleApproveRequest approves a join request and sends approval callback.
|
||||
// handleApproveRequest approves a join request and sends an approval callback.
|
||||
func (bot *Bot) handleApproveRequest(query *api.CallbackQuery, user *ExtendedChatJoinRequest, userString, adminUserString string) {
|
||||
r := api.ApproveChatJoinRequestConfig{
|
||||
ChatConfig: api.ChatConfig{
|
||||
ChatID: user.ChatJoinRequest.Chat.ID,
|
||||
},
|
||||
UserID: user.ChatJoinRequest.From.ID,
|
||||
ChatConfig: api.ChatConfig{ChatID: user.ChatJoinRequest.Chat.ID},
|
||||
UserID: user.ChatJoinRequest.From.ID,
|
||||
}
|
||||
_, e := bot.API.Request(r)
|
||||
if e != nil {
|
||||
log.Println(e.Error())
|
||||
edit := api.NewEditMessageText(query.Message.Chat.ID, query.Message.MessageID, query.Message.Text)
|
||||
edit.Entities = query.Message.Entities
|
||||
bot.API.Send(edit)
|
||||
|
||||
if _, err := bot.API.Request(r); err != nil {
|
||||
log.Println(err)
|
||||
bot.restoreMessage(query)
|
||||
return
|
||||
}
|
||||
|
||||
utils.EditMessage(bot.API, query.Message.Chat.ID, query.Message.MessageID,
|
||||
fmt.Sprintf(AdminApprovedMsg,
|
||||
userString, user.From.ID, user.JoinReason, adminUserString,
|
||||
time.Now().Format("2006-01-02 15:04:05")))
|
||||
time.Now().Format("2006-01-02 15:04:05"),
|
||||
),
|
||||
)
|
||||
|
||||
if bot.Config.SendApprovalMessage {
|
||||
utils.SendMessage(bot.API, user.From.ID, 0, bot.Config.ApprovalMessage)
|
||||
}
|
||||
|
||||
callback := api.NewCallback(query.ID, "Join request approved.")
|
||||
bot.API.Request(callback)
|
||||
bot.API.Request(api.NewCallback(query.ID, "Join request approved."))
|
||||
}
|
||||
|
||||
// handleDeclineRequest declines a join request and sends decline callback.
|
||||
// handleDeclineRequest declines a join request and sends a decline callback.
|
||||
func (bot *Bot) handleDeclineRequest(query *api.CallbackQuery, user *ExtendedChatJoinRequest, userString, adminUserString string) {
|
||||
r := api.DeclineChatJoinRequest{
|
||||
ChatConfig: api.ChatConfig{
|
||||
ChatID: user.ChatJoinRequest.Chat.ID,
|
||||
},
|
||||
UserID: user.ChatJoinRequest.From.ID,
|
||||
ChatConfig: api.ChatConfig{ChatID: user.ChatJoinRequest.Chat.ID},
|
||||
UserID: user.ChatJoinRequest.From.ID,
|
||||
}
|
||||
_, e := bot.API.Request(r)
|
||||
if e != nil {
|
||||
log.Println(e.Error())
|
||||
edit := api.NewEditMessageText(query.Message.Chat.ID, query.Message.MessageID, query.Message.Text)
|
||||
edit.Entities = query.Message.Entities
|
||||
bot.API.Send(edit)
|
||||
|
||||
if _, err := bot.API.Request(r); err != nil {
|
||||
log.Println(err)
|
||||
bot.restoreMessage(query)
|
||||
return
|
||||
}
|
||||
|
||||
utils.EditMessage(bot.API, query.Message.Chat.ID, query.Message.MessageID,
|
||||
fmt.Sprintf(AdminDeclinedMsg, userString, user.From.ID, user.JoinReason, adminUserString,
|
||||
fmt.Sprintf(AdminDeclinedMsg,
|
||||
userString, user.From.ID, user.JoinReason, adminUserString,
|
||||
time.Now().Format("2006-01-02 15:04:05"),
|
||||
"(no reason provided, reply to this to set one, prepend with + to also send to user)"),
|
||||
defaultReason,
|
||||
),
|
||||
)
|
||||
|
||||
callback := api.NewCallback(query.ID, "Join request declined.")
|
||||
bot.API.Request(callback)
|
||||
bot.API.Request(api.NewCallback(query.ID, "Join request declined."))
|
||||
}
|
||||
|
||||
// HandleDeclineReason allows admins to provide a decline reason by replying to decline messages.
|
||||
func (bot *Bot) HandleDeclineReason(update *api.Update) {
|
||||
repliedMsg := update.Message.ReplyToMessage
|
||||
if !strings.Contains(repliedMsg.Text,
|
||||
"(no reason provided, reply to this to set one, prepend with + to also send to user)") {
|
||||
if !strings.Contains(repliedMsg.Text, defaultReason) {
|
||||
return
|
||||
}
|
||||
|
||||
lines := strings.Split(repliedMsg.Text, "\n")
|
||||
userString := utils.BuildUserString(update.Message.From)
|
||||
|
||||
if strings.TrimPrefix(lines[3], "Declined by: ") != userString {
|
||||
return
|
||||
}
|
||||
|
||||
reason := utils.EscapeHTML(update.Message.Text)
|
||||
userID, username, joinReason, declinedBy, declinedAt := utils.GetInfoFromMsg(repliedMsg.Text)
|
||||
entities, _ := utils.FilterEntitiesByTypeWithContext(repliedMsg, "italic")
|
||||
if len(entities) >= 1 {
|
||||
if entities, _ := utils.FilterEntitiesByTypeWithContext(repliedMsg, "italic"); len(entities) >= 1 {
|
||||
username = fmt.Sprintf("<i>%s</i>", entities[0].Text)
|
||||
}
|
||||
|
||||
reason := utils.EscapeHTML(update.Message.Text)
|
||||
if strings.HasPrefix(update.Message.Text, "+") {
|
||||
reason = utils.EscapeHTML(update.Message.Text[1:])
|
||||
utils.SendMessage(bot.API, userID, 0,
|
||||
|
|
@ -157,3 +126,49 @@ func (bot *Bot) HandleDeclineReason(update *api.Update) {
|
|||
utils.EditMessage(bot.API, update.Message.Chat.ID, repliedMsg.MessageID,
|
||||
fmt.Sprintf(AdminDeclinedMsg, username, userID, joinReason, declinedBy, declinedAt, reason))
|
||||
}
|
||||
|
||||
// parseCallbackData parses the action and user ID from a callback query's data string.
|
||||
func parseCallbackData(data string) (action string, userID int64, err error) {
|
||||
normalized := strings.Join(strings.Split(data, "_"), " ")
|
||||
_, err = fmt.Sscanf(normalized, "%s %d", &action, &userID)
|
||||
return
|
||||
}
|
||||
|
||||
// handleLeaveAction handles the "leave" callback by leaving the specified chat.
|
||||
func (bot *Bot) handleLeaveAction(query *api.CallbackQuery, chatID int64) {
|
||||
utils.LeaveChatRequest(bot.API, []int64{chatID})
|
||||
utils.EditMessage(bot.API, query.Message.Chat.ID, query.Message.MessageID,
|
||||
fmt.Sprintf("We have left chat <i>%d</i>", chatID))
|
||||
bot.API.Request(api.NewCallback(query.ID, ""))
|
||||
}
|
||||
|
||||
// handleMissingUser notifies the admin that the user's pending request could not be found.
|
||||
func (bot *Bot) handleMissingUser(query *api.CallbackQuery) {
|
||||
msg := api.NewMessage(query.Message.Chat.ID, "Unable to find user, bot may have restarted")
|
||||
msg.ReplyParameters = api.ReplyParameters{
|
||||
MessageID: query.Message.MessageID,
|
||||
ChatID: query.Message.Chat.ID,
|
||||
}
|
||||
r, _ := bot.API.Send(msg)
|
||||
|
||||
edit := api.NewEditMessageText(query.Message.Chat.ID, r.ReplyToMessage.MessageID, r.ReplyToMessage.Text)
|
||||
edit.Entities = r.ReplyToMessage.Entities
|
||||
bot.API.Send(edit)
|
||||
|
||||
bot.API.Request(api.NewCallback(query.ID, ""))
|
||||
}
|
||||
|
||||
// restoreMessage restores a message to its original state after a failed API request.
|
||||
func (bot *Bot) restoreMessage(query *api.CallbackQuery) {
|
||||
edit := api.NewEditMessageText(query.Message.Chat.ID, query.Message.MessageID, query.Message.Text)
|
||||
edit.Entities = query.Message.Entities
|
||||
bot.API.Send(edit)
|
||||
}
|
||||
|
||||
// scheduleMessageDeletion deletes a message after a given delay.
|
||||
func (bot *Bot) scheduleMessageDeletion(chatID int64, messageID int, delay time.Duration) {
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
<-timer.C
|
||||
bot.API.Send(api.NewDeleteMessage(chatID, messageID))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue