add ban button

This commit is contained in:
Astra 2026-02-17 20:17:58 +00:00
parent d157f9b2c9
commit 4f8d18b696
3 changed files with 35 additions and 2 deletions

View file

@ -10,7 +10,7 @@ import (
api "github.com/OvyFlash/telegram-bot-api"
)
// HandleCallbackQuery processes inline button callbacks (approve/decline/leave).
// HandleCallbackQuery processes inline button callbacks (approve/decline/ban/leave).
func (bot *Bot) HandleCallbackQuery(query *api.CallbackQuery) {
action, args, err := parseCallbackData(query.Data)
if err != nil {
@ -38,6 +38,8 @@ func (bot *Bot) HandleCallbackQuery(query *api.CallbackQuery) {
bot.handleApproveRequest(query, user, userString, adminUserString)
case "decline":
bot.handleDeclineRequest(query, user, userString, adminUserString)
case "ban":
bot.handleBanRequest(query, user, userString, adminUserString)
}
bot.DeletePendingUser(args)
@ -98,6 +100,32 @@ func (bot *Bot) handleDeclineRequest(query *api.CallbackQuery, user *ExtendedCha
bot.API.Request(api.NewCallback(query.ID, "Join request declined."))
}
// handleBanRequest bans a user from the chat for 24 hours.
func (bot *Bot) handleBanRequest(query *api.CallbackQuery, user *ExtendedChatJoinRequest, userString, adminUserString string) {
r := api.BanChatMemberConfig{
ChatMemberConfig: api.ChatMemberConfig{
ChatConfig: api.ChatConfig{ChatID: user.ChatJoinRequest.Chat.ID},
UserID: user.ChatJoinRequest.From.ID,
},
UntilDate: time.Now().Add(24 * time.Hour).Unix(),
}
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(AdminBannedMsg,
userString, user.From.ID, user.JoinReason, adminUserString,
time.Now().Format("2006-01-02 15:04:05"),
),
)
bot.API.Request(api.NewCallback(query.ID, "User banned."))
}
// HandleDeclineReason allows admins to provide a decline reason by replying to decline messages.
func (bot *Bot) HandleDeclineReason(update *api.Update) {
repliedMsg := update.Message.ReplyToMessage

View file

@ -11,6 +11,7 @@ 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"
AdminBannedMsg = "🚫 Join #request banned for %s [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Banned by</b>: %s\n<b>Banned at</b>: %s"
AdminFailedMsg = "⚠️ Join #request failed for %s [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Failure reason</b>: %s"
defaultReason = "(no reason provided, reply to this to set one, prepend with + to also send to user)"
)

View file

@ -38,7 +38,11 @@ func EscapeHTML(s string) string {
func NewApprovalKeyboard(userID int64) api.InlineKeyboardMarkup {
approveBtn := api.NewInlineKeyboardButtonData("Approve", fmt.Sprintf("approve_%d", userID))
declineBtn := api.NewInlineKeyboardButtonData("Decline", fmt.Sprintf("decline_%d", userID))
return api.NewInlineKeyboardMarkup([]api.InlineKeyboardButton{approveBtn, declineBtn})
banBtn := api.NewInlineKeyboardButtonData("Ban", fmt.Sprintf("ban_%d", userID))
return api.NewInlineKeyboardMarkup(
[]api.InlineKeyboardButton{approveBtn, declineBtn},
[]api.InlineKeyboardButton{banBtn},
)
}
func GetInfoFromMsg(msg string) (userId int64, username, joinReason, declinedBy, declinedAt string) {