File size: 7,815 Bytes
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
// Admin commands: ban, unban, banned list, force-sub channel management.

package plugins

import (
	"fmt"
	"strconv"
	"strings"

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

// ── Ban / Unban ───────────────────────────────────────────────────────────────

// BanUser handles the /ban command.
//
//nolint:errcheck
func BanUser(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
	}

	msg := ctx.Message
	var (
		targetID int64
		reason   = "No reason provided"
		err      error
	)

	// /ban reply: get user from replied message
	if msg.ReplyToMessage != nil && msg.ReplyToMessage.From != nil {
		targetID = msg.ReplyToMessage.From.Id
		args := strings.SplitN(msg.Text, " ", 2)
		if len(args) >= 2 {
			reason = args[1]
		}
	} else {
		// /ban <user_id> [reason]
		args := strings.SplitN(msg.Text, " ", 3)
		if len(args) < 2 {
			msg.Reply(bot, "Usage: <code>/ban &lt;user_id&gt; [reason]</code> or reply to a message", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
			return nil
		}
		targetID, err = strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64)
		if err != nil {
			msg.Reply(bot, "❌ Invalid user ID.", &gotgbot.SendMessageOpts{})
			return nil
		}
		if len(args) == 3 {
			reason = args[2]
		}
	}

	if utils.IsAdmin(targetID) {
		msg.Reply(bot, "β›” You cannot ban another bot admin!", &gotgbot.SendMessageOpts{})
		return nil
	}

	if err := DB.BanUser(targetID, reason); err != nil {
		msg.Reply(bot, fmt.Sprintf("❌ Failed to ban: %v", err), &gotgbot.SendMessageOpts{})
		return nil
	}

	msg.Reply(bot,
		fmt.Sprintf("βœ… <b>User Banned</b>\n\n<b>ID:</b> <code>%d</code>\n<b>Reason:</b> %s", targetID, reason),
		&gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML},
	)
	return nil
}

// UnbanUser handles the /unban command.
//
//nolint:errcheck
func UnbanUser(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
	}

	msg := ctx.Message
	var (
		targetID int64
		err      error
	)

	if msg.ReplyToMessage != nil && msg.ReplyToMessage.From != nil {
		targetID = msg.ReplyToMessage.From.Id
	} else {
		args := strings.SplitN(msg.Text, " ", 2)
		if len(args) < 2 {
			msg.Reply(bot, "Usage: <code>/unban &lt;user_id&gt;</code> or reply to a message", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
			return nil
		}
		targetID, err = strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64)
		if err != nil {
			msg.Reply(bot, "❌ Invalid user ID.", &gotgbot.SendMessageOpts{})
			return nil
		}
	}

	if err := DB.UnbanUser(targetID); err != nil {
		msg.Reply(bot, fmt.Sprintf("❌ Failed to unban: %v", err), &gotgbot.SendMessageOpts{})
		return nil
	}

	msg.Reply(bot,
		fmt.Sprintf("βœ… User <code>%d</code> has been unbanned.", targetID),
		&gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML},
	)
	return nil
}

// BannedList handles the /banned command – lists all banned users.
//
//nolint:errcheck
func BannedList(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
	}

	users, err := DB.GetBannedUsers()
	if err != nil {
		ctx.EffectiveMessage.Reply(bot, fmt.Sprintf("❌ Error: %v", err), &gotgbot.SendMessageOpts{})
		return nil
	}

	if len(users) == 0 {
		ctx.EffectiveMessage.Reply(bot, "βœ… No banned users.", &gotgbot.SendMessageOpts{})
		return nil
	}

	text := "<b>🚫 Banned Users:</b>\n\n"
	for i, u := range users {
		text += fmt.Sprintf("%d. <code>%d</code> β€” %s\n", i+1, u.ID, u.Reason)
	}

	ctx.EffectiveMessage.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
	return nil
}

// ── Force-Sub Management ──────────────────────────────────────────────────────

// AddFSub handles /addfsub <channel_id> – adds a channel to force-sub list.
//
//nolint:errcheck
func AddFSub(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
	}

	args := strings.SplitN(ctx.Message.Text, " ", 2)
	if len(args) < 2 {
		ctx.Message.Reply(bot, "Usage: <code>/addfsub &lt;channel_id&gt;</code>", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
		return nil
	}

	chanID, err := strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64)
	if err != nil {
		ctx.Message.Reply(bot, "❌ Invalid channel ID. Use the numeric ID (e.g. -1001234567890).", &gotgbot.SendMessageOpts{})
		return nil
	}

	// Verify bot is in the channel
	_, err = bot.GetChat(chanID, nil)
	if err != nil {
		ctx.Message.Reply(bot, fmt.Sprintf("❌ Cannot access channel <code>%d</code>. Make sure I'm an admin there.", chanID), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
		return nil
	}

	if err := DB.AddForceSubChannel(chanID); err != nil {
		ctx.Message.Reply(bot, fmt.Sprintf("❌ Failed to add: %v", err), &gotgbot.SendMessageOpts{})
		return nil
	}

	ctx.Message.Reply(bot,
		fmt.Sprintf("βœ… Channel <code>%d</code> added to force-sub list.", chanID),
		&gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML},
	)
	return nil
}

// DelFSub handles /delfsub <channel_id> – removes a channel from force-sub list.
//
//nolint:errcheck
func DelFSub(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
	}

	args := strings.SplitN(ctx.Message.Text, " ", 2)
	if len(args) < 2 {
		ctx.Message.Reply(bot, "Usage: <code>/delfsub &lt;channel_id&gt;</code>", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
		return nil
	}

	chanID, err := strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64)
	if err != nil {
		ctx.Message.Reply(bot, "❌ Invalid channel ID.", &gotgbot.SendMessageOpts{})
		return nil
	}

	if err := DB.RemoveForceSubChannel(chanID); err != nil {
		ctx.Message.Reply(bot, fmt.Sprintf("❌ Failed to remove: %v", err), &gotgbot.SendMessageOpts{})
		return nil
	}

	ctx.Message.Reply(bot,
		fmt.Sprintf("βœ… Channel <code>%d</code> removed from force-sub list.", chanID),
		&gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML},
	)
	return nil
}

// ListFSub handles /fsub – lists all force-sub channels.
//
//nolint:errcheck
func ListFSub(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
	}

	channels := DB.GetForceSubChannels()
	if len(channels) == 0 {
		ctx.EffectiveMessage.Reply(bot, "ℹ️ No force-sub channels configured.", &gotgbot.SendMessageOpts{})
		return nil
	}

	text := "<b>πŸ“’ Force-Sub Channels:</b>\n\n"
	for i, chanID := range channels {
		name := fmt.Sprintf("%d", chanID)
		chat, err := bot.GetChat(chanID, nil)
		if err == nil {
			name = fmt.Sprintf("%s (<code>%d</code>)", chat.Title, chanID)
		}
		text += fmt.Sprintf("%d. %s\n", i+1, name)
	}

	text += "\n<i>Use /addfsub and /delfsub to manage.</i>"
	ctx.EffectiveMessage.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
	return nil
}