code refactor

This commit is contained in:
Astra 2026-02-17 20:05:06 +00:00
parent 7275b93666
commit d157f9b2c9
6 changed files with 187 additions and 193 deletions

View file

@ -10,35 +10,23 @@ import (
// HandleJoinRequestResponse records the user's join reason and notifies admins.
func (bot *Bot) HandleJoinRequestResponse(user *ExtendedChatJoinRequest, update *api.Message) {
if user.JoinReason == "" {
user.JoinReason = utils.EscapeHTML(update.Text)
userString := utils.BuildUserString(&user.From)
keyboard := utils.NewApprovalKeyboard(user.From.ID)
utils.EditMessageWithKeyboard(bot.API, *bot.Config.AdminChatId, user.JoinRequestMessageID,
fmt.Sprintf(AdminJoinRequestMsg,
userString, user.From.ID, user.JoinReason), &keyboard)
utils.SendMessage(bot.API, update.From.ID, 0, "Thank you! Your request has been sent to the admins for review.")
} else {
if user.JoinReason != "" {
utils.SendMessage(bot.API, update.From.ID, 0, "Your request is already pending approval.")
}
}
// HandleJoinRequest initiates join approval flow by sending entry message and admin notification.
func (bot *Bot) HandleJoinRequest(request *api.ChatJoinRequest) {
// if chat is not in config, ignore
if *bot.Config.TargetChatId != request.Chat.ID {
m := api.NewMessage(*bot.Config.AdminChatId,
fmt.Sprintf("Received join request for chat %s (<code>%d</code>), but it's not in config, ignoring",
request.Chat.Title, request.Chat.ID))
leaveBtn := api.NewInlineKeyboardButtonData("Leave Chat", fmt.Sprintf("leave_%d", request.Chat.ID))
m.ReplyMarkup = api.NewInlineKeyboardMarkup([]api.InlineKeyboardButton{leaveBtn})
m.ParseMode = api.ModeHTML
bot.API.Send(m)
return
}
user.JoinReason = utils.EscapeHTML(update.Text)
userString := utils.BuildUserString(&user.From)
keyboard := utils.NewApprovalKeyboard(user.From.ID)
utils.EditMessageWithKeyboard(bot.API, *bot.Config.AdminChatId, user.JoinRequestMessageID,
fmt.Sprintf(AdminJoinRequestMsg, userString, user.From.ID, user.JoinReason), &keyboard)
utils.SendMessage(bot.API, update.From.ID, 0, "Thank you! Your request has been sent to the admins for review.")
}
// HandleJoinRequest initiates the join approval flow by sending the entry message and admin notification.
func (bot *Bot) HandleJoinRequest(request *api.ChatJoinRequest) {
utils.SendMessage(bot.API, request.From.ID, 0, bot.Config.EntryMessage)
userString := utils.BuildUserString(&request.From)
@ -49,6 +37,7 @@ func (bot *Bot) HandleJoinRequest(request *api.ChatJoinRequest) {
if topic := *bot.Config.AdminChatTopicId; topic != 0 {
m.MessageThreadID = topic
}
r, err := bot.API.Send(m)
if err != nil {
log.Printf("Failed to send join request to admin chat: %v", err)
@ -66,7 +55,5 @@ func (bot *Bot) HandleJoinRequest(request *api.ChatJoinRequest) {
func (bot *Bot) SendFailureMessage(user *ExtendedChatJoinRequest, query *api.CallbackQuery, userString string) {
utils.EditMessage(bot.API, *bot.Config.AdminChatId, user.JoinRequestMessageID,
fmt.Sprintf(AdminFailedMsg, userString, user.From.ID, utils.EscapeHTML(user.JoinReason), "User not found in requests."))
callback := api.NewCallback(query.ID, "Join request failed.")
bot.API.Request(callback)
bot.API.Request(api.NewCallback(query.ID, "Join request failed."))
}