Code refactor

This commit is contained in:
Astra 2026-02-16 16:49:20 +00:00
parent 5e37545782
commit d2f7297d92
7 changed files with 534 additions and 330 deletions

60
handlers/handlers.go Normal file
View file

@ -0,0 +1,60 @@
package handlers
import (
"sync"
config "git.zio.sh/astra/telegram-approval-join/config"
api "github.com/OvyFlash/telegram-bot-api"
)
const (
AdminJoinRequestMsg = "New join #request from %s [<code>%d</code>]\n\n<b>Join reason</b>: %s"
AdminApprovedMsg = "✅ Join #request approved for %s [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Approved by</b>: %s\n<b>Approved at</b>: %s"
AdminDeclinedMsg = "❌ Join #request declined for %s [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Declined by</b>: %s\n<b>Declined at</b>: %s\n<b>Declined reason</b>: %s"
AdminFailedMsg = "⚠️ Join #request failed for %s [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Failure reason</b>: %s"
BotApprovalEnabled = "Join approval bot enabled, to get started, add the bot to the target group and make it an admin with invite permissions, then use <code>/targetchat &lt;chat_id&gt;</code> to add the group to the config.\n\nTo set the entry message that users will receive when they request to join, use <code>/setentrymessage &lt;entry_message&gt;</code> in the admin chat. You can use HTML formatting in the entry message."
BotAddedToGroup = "Hello! I help out with join approvals.\n\nTo get started, make sure this bot is added to the admin group where it will send join approvals to be accepted or declined, then type /request when you have done that."
BotRequestMsg = "You have requested this chat to be the admin chat, please wait for it to be activated."
)
// Types shared by handler files
type ExtendedChatJoinRequest struct {
*api.ChatJoinRequest
JoinReason string
JoinRequestMessageID int
}
type Bot struct {
API *api.BotAPI
Config config.Config
mu sync.RWMutex
WaitingForApproval map[int64]*ExtendedChatJoinRequest
}
// GetPendingUser retrieves a pending user request (read-safe).
func (bot *Bot) GetPendingUser(userID int64) *ExtendedChatJoinRequest {
bot.mu.RLock()
defer bot.mu.RUnlock()
user := bot.WaitingForApproval[userID]
if user == nil {
return nil
}
return user
}
// SetPendingUser stores a pending user request (write-safe).
func (bot *Bot) SetPendingUser(userID int64, user *ExtendedChatJoinRequest) {
bot.mu.Lock()
defer bot.mu.Unlock()
if _, ok := bot.WaitingForApproval[userID]; !ok {
bot.WaitingForApproval = make(map[int64]*ExtendedChatJoinRequest)
}
bot.WaitingForApproval[userID] = user
}
// DeletePendingUser removes a pending user request (write-safe).
func (bot *Bot) DeletePendingUser(userID int64) {
bot.mu.Lock()
defer bot.mu.Unlock()
delete(bot.WaitingForApproval, userID)
}