File size: 9,334 Bytes
891b1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d492361
 
 
891b1b2
 
 
 
 
 
 
 
d492361
 
 
 
 
891b1b2
 
 
d492361
 
891b1b2
 
 
 
 
 
 
 
d492361
 
 
 
 
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
284
285
286
287
288
289
290
291
292
293
// Dynamic bot settings panel β€” toggle all settings from within the bot.
// No more .env edits needed! All changes are saved to MongoDB and cached in memory.

package plugins

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"

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

var cbSettingsRegex = regexp.MustCompile(`settings\((.+)\)`)

func boolEmoji(v bool) string {
	if v {
		return "βœ…"
	}
	return "❌"
}

// buildSettingsMarkup generates the inline keyboard for the settings panel.
func buildSettingsMarkup(s database.BotSettings) gotgbot.InlineKeyboardMarkup {
	return gotgbot.InlineKeyboardMarkup{
		InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
			{
				{Text: fmt.Sprintf("%s Multi Filter", boolEmoji(s.MultiFilter)), CallbackData: "settings(toggle_multi_filter)"},
			},
			{
				{Text: fmt.Sprintf("%s Search as Button", boolEmoji(s.SearchAsButton)), CallbackData: "settings(toggle_search_button)"},
			},
			{
				{Text: fmt.Sprintf("⏱ Auto Delete: %dm", s.AutoDelete), CallbackData: "settings(auto_delete)"},
			},
			{
				{Text: "πŸ”— Shortener URL", CallbackData: "settings(shortener_url)"},
				{Text: "πŸ”‘ Shortener API", CallbackData: "settings(shortener_api)"},
			},
			{
				{Text: fmt.Sprintf("πŸ“’ Force-Sub (%d ch)", len(s.ForceSubChats)), CallbackData: "settings(fsub_info)"},
				{Text: fmt.Sprintf("πŸ“ Index (%d ch)", len(s.IndexChannels)), CallbackData: "settings(index_info)"},
			},
			{
				{Text: fmt.Sprintf("πŸ‘‘ Admins (%d dynamic)", len(s.DynamicAdmins)), CallbackData: "settings(admin_info)"},
			},
			{
				{Text: "πŸ”„ Refresh", CallbackData: "settings(refresh)"},
				{Text: "βœ– Close", CallbackData: "close"},
			},
		},
	}
}

// buildSettingsText generates the main settings panel text.
func buildSettingsText(s database.BotSettings) string {
	shortURL := s.ShortenerURL
	if shortURL == "" {
		shortURL = "<i>Not Set</i>"
	}
	shortAPI := "β€’β€’β€’β€’β€’β€’"
	if s.ShortenerAPI == "" {
		shortAPI = "<i>Not Set</i>"
	}

	fsubList := "<i>None</i>"
	if len(s.ForceSubChats) > 0 {
		parts := make([]string, len(s.ForceSubChats))
		for i, c := range s.ForceSubChats {
			parts[i] = fmt.Sprintf("<code>%d</code>", c)
		}
		fsubList = strings.Join(parts, ", ")
	}

	indexList := "<i>None</i>"
	if len(s.IndexChannels) > 0 {
		parts := make([]string, len(s.IndexChannels))
		for i, c := range s.IndexChannels {
			parts[i] = fmt.Sprintf("<code>%d</code>", c)
		}
		indexList = strings.Join(parts, ", ")
	}

	return fmt.Sprintf(`<b>βš™οΈ Bᴏᴛ SᴇᴛᴛΙͺΙ΄Ι’s Pα΄€Ι΄α΄‡ΚŸ</b>

<b>Multi Filter :</b> %s
<b>Search Mode :</b> %s
<b>Auto Delete :</b> %d minutes
<b>Shortener URL :</b> %s
<b>Shortener API :</b> %s
<b>Force-Sub :</b> %s
<b>Auto-Index :</b> %s
<b>Dynamic Admins :</b> %d

<i>Tap the buttons below to change settings.</i>
<i>Changes take effect immediately β€” no restart needed!</i>`,
		boolEmoji(s.MultiFilter),
		func() string {
			if s.SearchAsButton {
				return "Button"
			}
			return "Hyperlink"
		}(),
		s.AutoDelete,
		shortURL,
		shortAPI,
		fsubList,
		indexList,
		len(s.DynamicAdmins),
	)
}

// Settings handles the /settings command.
//
//nolint:errcheck
func Settings(bot *gotgbot.Bot, ctx *ext.Context) error {
	if !utils.IsAdmin(ctx.EffectiveUser.Id) {
		ctx.EffectiveMessage.Reply(bot, "β›” Only bot admins can use this command!", &gotgbot.SendMessageOpts{})
		return nil
	}

	s := database.GetBotSettings()
	_, err := ctx.Message.Reply(bot, buildSettingsText(s), &gotgbot.SendMessageOpts{
		ParseMode:   gotgbot.ParseModeHTML,
		ReplyMarkup: buildSettingsMarkup(s),
	})
	if err != nil {
		fmt.Printf("settings: %v\n", err)
	}
	return nil
}

// CbSettings handles all settings callbacks.
//
//nolint:errcheck
func CbSettings(bot *gotgbot.Bot, ctx *ext.Context) error {
	update := ctx.CallbackQuery

	if !utils.IsAdmin(update.From.Id) {
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "β›” Only bot admins can change settings!", ShowAlert: true})
		return nil
	}

	match := cbSettingsRegex.FindStringSubmatch(update.Data)
	if len(match) < 2 {
		return nil
	}

	action := match[1]
	s := database.GetBotSettings()

	switch action {
	case "toggle_multi_filter":
		DB.SetBotSetting("multi_filter", !s.MultiFilter)
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{
			Text: fmt.Sprintf("Multi Filter: %s", boolEmoji(!s.MultiFilter)),
		})

	case "toggle_search_button":
		DB.SetBotSetting("search_as_button", !s.SearchAsButton)
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{
			Text: fmt.Sprintf("Search as Button: %s", boolEmoji(!s.SearchAsButton)),
		})

	case "auto_delete":
		// Cycle through: 0 β†’ 5 β†’ 10 β†’ 30 β†’ 60 β†’ 0
		var next int64
		switch {
		case s.AutoDelete == 0:
			next = 5
		case s.AutoDelete < 10:
			next = 10
		case s.AutoDelete < 30:
			next = 30
		case s.AutoDelete < 60:
			next = 60
		default:
			next = 0
		}
		DB.SetBotSetting("auto_delete", next)
		if next == 0 {
			update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "Auto Delete: OFF"})
		} else {
			update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: fmt.Sprintf("Auto Delete: %d minutes", next)})
		}

	case "shortener_url":
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{})
		// Ask in PM β€” works from both group and PM contexts
		pmChat := &gotgbot.Chat{Id: update.From.Id, Type: gotgbot.ChatTypePrivate}
		m := utils.Ask(bot, "πŸ“ Send the new <b>Shortener URL</b>\n\nExample: <code>fpshortner.in</code>\n\nSend <code>off</code> to disable.", pmChat, &update.From)
		if m == nil {
			return nil
		}
		val := strings.TrimSpace(m.Text)
		if strings.ToLower(val) == "off" {
			val = ""
		}
		DB.SetBotSetting("shortener_url", val)
		if val == "" {
			bot.SendMessage(update.From.Id, "βœ… Shortener URL disabled.", &gotgbot.SendMessageOpts{})
		} else {
			bot.SendMessage(update.From.Id, fmt.Sprintf("βœ… Shortener URL set to: <code>%s</code>", val), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
		}

	case "shortener_api":
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{})
		pmChat := &gotgbot.Chat{Id: update.From.Id, Type: gotgbot.ChatTypePrivate}
		m := utils.Ask(bot, "πŸ“ Send the new <b>Shortener API Key</b>\n\nSend <code>off</code> to disable.", pmChat, &update.From)
		if m == nil {
			return nil
		}
		val := strings.TrimSpace(m.Text)
		if strings.ToLower(val) == "off" {
			val = ""
		}
		DB.SetBotSetting("shortener_api", val)
		if val == "" {
			bot.SendMessage(update.From.Id, "βœ… Shortener API disabled.", &gotgbot.SendMessageOpts{})
		} else {
			bot.SendMessage(update.From.Id, "βœ… Shortener API Key updated!", &gotgbot.SendMessageOpts{})
		}

	case "fsub_info":
		text := "Use <code>/addfsub &lt;channel_id&gt;</code> to add\nUse <code>/delfsub &lt;channel_id&gt;</code> to remove\nUse <code>/fsub</code> to list"
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "Manage with /addfsub, /delfsub, /fsub", ShowAlert: true})
		msg, ok := update.Message.(*gotgbot.Message)
		if !ok {
			return nil
		}
		bot.SendMessage(msg.Chat.Id, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
		return nil


	case "index_info":
		text := "Use <code>/addindex &lt;channel_id&gt;</code> to add\nUse <code>/delindex &lt;channel_id&gt;</code> to remove\nUse <code>/bulkindex &lt;channel_id&gt; [from] [to]</code> for bulk indexing"
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "Manage with /addindex, /delindex, /bulkindex", ShowAlert: true})
		msg2, ok2 := update.Message.(*gotgbot.Message)
		if !ok2 {
			return nil
		}
		bot.SendMessage(msg2.Chat.Id, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
		return nil

	case "admin_info":
		text := "Use <code>/addadmin &lt;user_id&gt;</code> to add a dynamic admin\nUse <code>/deladmin &lt;user_id&gt;</code> to remove\nUse <code>/admins</code> to list all admins"
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "Manage with /addadmin, /deladmin, /admins", ShowAlert: true})
		msg3, ok3 := update.Message.(*gotgbot.Message)
		if !ok3 {
			return nil
		}
		bot.SendMessage(msg3.Chat.Id, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
		return nil

	case "refresh":
		DB.RefreshBotSettings()
		update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: "♻️ Settings refreshed!"})

	default:
		// Handle set_autodelete_<value> from potential custom input
		if strings.HasPrefix(action, "set_autodelete_") {
			valStr := strings.TrimPrefix(action, "set_autodelete_")
			val, err := strconv.ParseInt(valStr, 10, 64)
			if err == nil {
				DB.SetBotSetting("auto_delete", val)
				update.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{Text: fmt.Sprintf("Auto Delete: %d min", val)})
			}
		}
	}

	// Refresh the settings panel with updated values
	newS := database.GetBotSettings()
	update.Message.EditText(bot, buildSettingsText(newS), &gotgbot.EditMessageTextOpts{
		ParseMode:   gotgbot.ParseModeHTML,
		ReplyMarkup: buildSettingsMarkup(newS),
	})

	return nil
}

// CbClose handles the close button on settings and other panels.
//
//nolint:errcheck
func CbClose(bot *gotgbot.Bot, ctx *ext.Context) error {
	ctx.CallbackQuery.Message.Delete(bot, nil)
	ctx.CallbackQuery.Answer(bot, &gotgbot.AnswerCallbackQueryOpts{})
	return nil
}