File size: 8,564 Bytes
891b1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71db04d
d64953c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
891b1b2
 
 
71db04d
 
891b1b2
 
71db04d
891b1b2
 
 
 
 
 
 
 
 
 
 
6e6402e
 
 
891b1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d492361
 
3fa0fdf
 
 
 
 
1c9c497
d492361
 
 
 
 
 
 
 
 
 
3fa0fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d492361
891b1b2
64c9818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
891b1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// (c) Jisin0

package plugins

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/Jisin0/Go-Filter-Bot/utils/config"
	"github.com/Jisin0/Go-Filter-Bot/utils/logger"
	"github.com/PaulSonOfLars/gotgbot/v2"
	"github.com/PaulSonOfLars/gotgbot/v2/ext"
)

var cbEditPattern *regexp.Regexp = regexp.MustCompile(`edit\((.+)\)`)

// AutoDelete var removed — now read dynamically from DB

func Start(bot *gotgbot.Bot, update *ext.Context) error {
	user := update.EffectiveMessage.From
	isNewUser := false
	if user != nil {
		isNewUser = DB.AddUserAndCheck(user.Id)
	}

	args := strings.Split(update.Message.Text, " ")
	if len(args) > 1 && strings.HasPrefix(args[1], "file_") {
		fileID := strings.TrimPrefix(args[1], "file_")
		SendFileByID(fileID, bot, update.Message)
		return nil
	}

	text := fmt.Sprintf(config.TEXT["START"], update.Message.From.FirstName, bot.FirstName)
	markup := gotgbot.InlineKeyboardMarkup{InlineKeyboard: config.BUTTONS["START"]}
	replyOpts := &gotgbot.ReplyParameters{AllowSendingWithoutReply: true}

	// Send with image if START_IMAGE is configured
	if config.StartImage != "" {
		imgFile := gotgbot.InputFileByID(config.StartImage)
		_, err := bot.SendPhoto(update.Message.Chat.Id, imgFile, &gotgbot.SendPhotoOpts{
			Caption:         text,
			ParseMode:       gotgbot.ParseModeHTML,
			ReplyMarkup:     markup,
			ReplyParameters: replyOpts,
		})
		if err != nil {
			// Fallback to text if photo fails
			fmt.Printf("start: photo send failed: %v\n", err)
			bot.SendMessage(update.Message.Chat.Id, text, &gotgbot.SendMessageOpts{
				ParseMode:       gotgbot.ParseModeHTML,
				ReplyMarkup:     markup,
				ReplyParameters: replyOpts,
			})
		}
	} else {
		_, err := bot.SendMessage(
			update.Message.Chat.Id,
			text,
			&gotgbot.SendMessageOpts{
				ParseMode:       gotgbot.ParseModeHTML,
				ReplyMarkup:     markup,
				ReplyParameters: replyOpts,
			})
		if err != nil {
			fmt.Printf("start: %v\n", err)
		}
	}

	// Notify log channel for new users
	if isNewUser && user != nil {
		go logNewUser(bot, user)
	}

	return nil
}

func Stats(bot *gotgbot.Bot, update *ext.Context) error {
	markup := gotgbot.InlineKeyboardMarkup{InlineKeyboard: [][]gotgbot.InlineKeyboardButton{{{Text: "Refresh 🔄", CallbackData: "stats"}}}}

	_, err := update.EffectiveMessage.Reply(bot, DB.Stats(), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML, ReplyMarkup: markup})
	if err != nil {
		fmt.Printf("stats: %v\n", err)
	}

	return nil
}

func GetID(bot *gotgbot.Bot, ctx *ext.Context) error {
	var (
		text   string
		update = ctx.Message
	)

	if update.ReplyToMessage != nil {
		text += fmt.Sprintf("\nReplied to user : <code>%v</code>", update.ReplyToMessage.From.Id)

		if f := update.ReplyToMessage.ForwardOrigin; f.GetDate() != 0 {
			switch m := f.MergeMessageOrigin(); {
			case m.Chat != nil:
				text += fmt.Sprintf("\nForwarded from : <code>%v</code>", m.Chat.Id)
			case m.SenderChat != nil:
				text += fmt.Sprintf("\nForwarded from : <code>%v</code>", m.SenderChat.Id)
			case m.SenderUser != nil:
				text += fmt.Sprintf("\nForwarded from : <code>%v</code>", m.SenderUser.Id)
			}
		}
	}

	text += fmt.Sprintf("\nUser id : <code>%v</code>", update.From.Id)

	if update.Chat.Type != gotgbot.ChatTypePrivate {
		text += fmt.Sprintf("\nChat id : <code>%v</code>", update.Chat.Id)
	}

	_, err := update.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML, ReplyParameters: &gotgbot.ReplyParameters{MessageId: update.MessageId}})
	if err != nil {
		fmt.Printf("getid: %v\n", err)
	}

	return nil
}

func CbStats(bot *gotgbot.Bot, update *ext.Context) error {
	cb := update.CallbackQuery
	chatID := cb.Message.GetChat().Id
	msgID := cb.Message.GetMessageId()
	
	statsMarkup := gotgbot.InlineKeyboardMarkup{InlineKeyboard: config.BUTTONS["STATS"]}
	
	var err error
	origMsg, isMsg := cb.Message.(*gotgbot.Message)
	if isMsg && len(origMsg.Photo) > 0 {
		_, _, err = origMsg.EditCaption(bot, &gotgbot.EditMessageCaptionOpts{
			Caption:     DB.Stats(),
			ParseMode:   gotgbot.ParseModeHTML,
			ReplyMarkup: statsMarkup,
		})
	} else {
		_, _, err = bot.EditMessageText(DB.Stats(), &gotgbot.EditMessageTextOpts{
			ChatId:      chatID,
			MessageId:   msgID,
			ParseMode:   gotgbot.ParseModeHTML,
			ReplyMarkup: statsMarkup,
		})
		if err != nil {
			// Fallback if EditMessageText fails (it might be a photo message but inaccessible)
			_, _, err = bot.EditMessageCaption(&gotgbot.EditMessageCaptionOpts{
				ChatId:      chatID,
				MessageId:   msgID,
				Caption:     DB.Stats(),
				ParseMode:   gotgbot.ParseModeHTML,
				ReplyMarkup: statsMarkup,
			})
		}
	}

	if err != nil {
		fmt.Printf("cbstats: %v\n", err)
		cb.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "⚠️ Could not refresh — please re-run /stats", ShowAlert: true}) //nolint:errcheck
		return nil
	}

	cb.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "🔄 Refreshed!"}) //nolint:errcheck
	return nil
}

func FilterHandler(b *gotgbot.Bot, ctx *ext.Context) error {
	// Silently ignore banned users
	if user := ctx.EffectiveUser; user != nil && user.Id != 0 {
		if DB.IsBanned(user.Id) {
			return nil
		}
	}

	// Force sub is handled inside Filter() — skip duplicate check here
	go Filter(b, ctx)  // VJ-style search with "Searching..." indicator
	go GFilter(b, ctx) // Manual group filters

	return nil
}

// Function to handle edit() callbacks from the Start, About and Help menus
func CbEdit(bot *gotgbot.Bot, update *ext.Context) error {
	key := cbEditPattern.FindStringSubmatch(update.CallbackQuery.Data)[1]

	markup, ok := config.BUTTONS[key]
	if !ok {
		markup = [][]gotgbot.InlineKeyboardButton{{{Text: "⤝ Bᴀᴄᴋ", CallbackData: "edit(HELP)"}}}
	}

	var text string
	if key == "START" {
		text = fmt.Sprintf(config.TEXT["START"], update.CallbackQuery.From.FirstName, bot.FirstName)
	} else {
		text = config.TEXT[key]
	}

	editMarkup := gotgbot.InlineKeyboardMarkup{InlineKeyboard: markup}

	// Try to get the full message object
	origMsg, isMsg := update.CallbackQuery.Message.(*gotgbot.Message)

	// If it's a photo message (sent with START_IMAGE), edit caption
	if isMsg && len(origMsg.Photo) > 0 {
		_, _, err := origMsg.EditCaption(bot, &gotgbot.EditMessageCaptionOpts{
			Caption:     text,
			ParseMode:   gotgbot.ParseModeHTML,
			ReplyMarkup: editMarkup,
		})
		if err != nil {
			fmt.Printf("cbedit(caption): %v\n", err)
		}
		return nil
	}

	// Standard text message edit
	if isMsg {
		_, _, err := origMsg.EditText(bot, text, &gotgbot.EditMessageTextOpts{
			ParseMode:          gotgbot.ParseModeHTML,
			LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
			ReplyMarkup:        editMarkup,
		})
		if err != nil {
			fmt.Printf("cbedit: %v\n", err)
		}
		return nil
	}

	// Fallback: use raw bot API with chat/message IDs
	chatID := update.CallbackQuery.Message.GetChat().Id
	msgID := update.CallbackQuery.Message.GetMessageId()
	_, _, err := bot.EditMessageText(text, &gotgbot.EditMessageTextOpts{
		ChatId:             chatID,
		MessageId:          msgID,
		ParseMode:          gotgbot.ParseModeHTML,
		LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
		ReplyMarkup:        editMarkup,
	})
	if err != nil {
		// If EditText fails (e.g. message has photo), try EditCaption
		_, _, capErr := bot.EditMessageCaption(&gotgbot.EditMessageCaptionOpts{
			ChatId:      chatID,
			MessageId:   msgID,
			Caption:     text,
			ParseMode:   gotgbot.ParseModeHTML,
			ReplyMarkup: editMarkup,
		})
		if capErr != nil {
			// Both failed — just update markup silently
			bot.EditMessageReplyMarkup(&gotgbot.EditMessageReplyMarkupOpts{ //nolint:errcheck
				ChatId:      chatID,
				MessageId:   msgID,
				ReplyMarkup: editMarkup,
			})
		}
	}
	return nil
}

func About(b *gotgbot.Bot, update *ext.Context) error {
	_, err := update.Message.Reply(b, config.TEXT["ABOUT"], &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML, ReplyMarkup: gotgbot.InlineKeyboardMarkup{InlineKeyboard: config.BUTTONS["ABOUT"]}})
	if err != nil {
		fmt.Printf("about: %v\n", err)
	}

	return nil
}

func Help(b *gotgbot.Bot, update *ext.Context) error {
	_, err := update.Message.Reply(b, config.TEXT["HELP"], &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML, ReplyMarkup: gotgbot.InlineKeyboardMarkup{InlineKeyboard: config.BUTTONS["HELP"]}})
	if err != nil {
		fmt.Printf("help: %v\n", err)
	}

	return nil
}

// logNewUser sends a notification to the log channel for a new user.
func logNewUser(bot *gotgbot.Bot, user *gotgbot.User) {
	logger.SendNewUser(bot, user)
}