forked from MyrTheMoth/Guard-Bot-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noinfokick.ts
306 lines (290 loc) · 14 KB
/
noinfokick.ts
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
294
295
296
297
298
299
300
301
302
303
304
305
306
import type { ExtendedContext } from "../typings/context";
import type { UserProfilePhotos, Chat } from "telegraf/typings/telegram-types";
import Telegraf = require("telegraf");
import HtmlUtils = require("../utils/html");
import TgUtils = require("../utils/tg");
import Log = require("../utils/log");
import UserStore = require('../stores/user');
import Files = require('fs');
let settings = {
active: true, // If True, module is active and will kick profiles depending on the criteria you setup.
kickCooldown: 300, // Time before a kicked user can attempt to join the chat again in 5 minutes (300 seconds) by default.
checkUsername: true, // If True, checks if the new member has an username, if they don't, they'll fail this check.
checkPicture: true, // If True, checks if the new member has a profile picture, if they don't, they'll fail this check.
checkBio: true, // If True, checks if the new member has a bio description, if they don't, they'll fail this check.
feedback: true, // If True, it will post a message detailing why the user was removed.
tolerance: 2 // Value from 1 to 3, determines how many checks the new member must fail to be kicked.
}
const { Composer: C } = Telegraf;
const { html } = HtmlUtils;
const { link } = TgUtils;
const { logError } = Log;
const { getAdmins } = UserStore;
const fs = Files;
type seenOnce = {
user: number;
chat: String;
};
const userSeen = (u: number, c: String): seenOnce => {
const s = {
user: u,
chat: c
};
return s;
}
const kickedOnce: seenOnce[] = [];
const adminList: number[] = [];
const settingsFile = "./plugins/noinfokick.json";
// If a settings file exists, read it and update our settings with it.
fs.access(settingsFile, fs.F_OK, (err) => {
if (err) {
logError("[noinfokick] " + err.message);
return;
}
fs.readFile(settingsFile, "utf-8", (err, data) => {
if (err) {
logError("[noinfokick] " + err.message);
return;
}
settings = JSON.parse(data.toString());
});
});
// Update the settings file with our current settings object.
function updateSettings() {
const data = JSON.stringify(settings);
fs.writeFile(settingsFile, data, (err) => {
if (err) {
logError("[noinfokick] " + err.message);
return;
}
});
}
export = C.mount("message", async (ctx: ExtendedContext, next) => {
// Populate the list of Admins
if (adminList.length < 1) {
const admins = await getAdmins();
//logError(admins);
for (let i = 0; i < admins.length; i++) {
adminList.push(admins[i].id);
}
//logError(adminList);
}
//Plugin Commands
if (ctx.message?.entities?.[0].type === "bot_command") {
const text = ctx.message?.text;
const match = text.match(/^\/([^\s]+)\s?(.+)?/);
let args = [];
let command = "";
if (match !== null) {
if (match[1]) {
command = match[1];
}
if (match[2]) {
args = match[2].split(' ');
}
}
// Command arguments (/noinfokick).
if (command === "noinfokick") {
if (adminList.indexOf(ctx.from?.id) >= 0) {
if (args !== null) {
if (args[0] === "on") { // Enable No Info Kick (/noinfokick on).
settings.active = true;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick is now On.`
);
} else if (args[0] === "off") { // Disable No Info Kick (/noinfokick off).
settings.active = false;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick is now Off.`
);
} else if (args[0] === "cooldown") { // Change kick cooldown. (/noinfokick cooldown <integer bigger or equal to 300>).
if (parseInt(args[1]) >= 300) {
settings.kickCooldown = parseInt(args[1]);
updateSettings();
ctx.replyWithHTML(
html`No Info Kick cooldown kick time is now ${settings.kickCooldown}.`
);
} else if (parseInt(args[1]) < 300) {
ctx.replyWithHTML(
html`No Info Kick cooldown kick time cannot be lower than 300 seconds (5 minutes).`
);
} else {
ctx.replyWithHTML(
html`No Info Kick cooldown kick time value is invalid.`
);
}
} else if (args[0] === "username") { // Change No Info Kick username (/noinfokick username <true or false>).
if (args[1] == "true") {
settings.checkUsername = true;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick username checking is enabled.`
);
} else if (args[1] == "false") {
settings.checkUsername = false;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick username checking is disabled.`
);
}
} else if (args[0] === "picture") { // Change No Info Kick picture (/noinfokick picture <true or false>).
if (args[1] == "true") {
settings.checkPicture = true;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick picture checking is enabled.`
);
} else if (args[1] == "false") {
settings.checkPicture = false;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick picture checking is disabled.`
);
}
} else if (args[0] === "bio") { // Change No Info Kick bio (/noinfokick bio <true or false>).
if (args[1] == "true") {
settings.checkBio = true;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick bio checking is enabled.`
);
} else if (args[1] == "false") {
settings.checkBio = false;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick bio checking is disabled.`
);
}
} else if (args[0] === "feedback") { // Change No Info Kick feedback (/noinfokick feedback <true or false>).
if (args[1] == "true") {
settings.feedback = true;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick feedback is enabled.`
);
} else if (args[1] == "false") {
settings.feedback = false;
updateSettings();
ctx.replyWithHTML(
html`No Info Kick feedback is disabled.`
);
}
} else if (args[0] === "tolerance") { // Change No Info Kick tolerance (/noinfokick tolerance <1, 2 or 3>).
if (parseInt(args[1]) >= 1 && parseInt(args[1]) <= 3) {
settings.tolerance = parseInt(args[1]);
updateSettings();
ctx.replyWithHTML(
html`No Info Kick tolerance is now ${settings.tolerance}.`
);
} else {
ctx.replyWithHTML(
html`No Info Kick tolerance can only be 1, 2 or 3.`
);
}
} else if (args[0] === "settings") { // Print plugin settings (/noinfokick settings).
ctx.replyWithHTML(
html`No Info Kick settings:
<code>
active: ${settings.active}
kickCooldown: ${settings.kickCooldown}
checkUsername: ${settings.checkUsername}
checkPicture: ${settings.checkPicture}
checkBio: ${settings.checkBio}
feedback: ${settings.feedback}
tolerance: ${settings.tolerance}
</code>`
);
} else { // Invalid arguments, show No Info Kick commands.
ctx.replyWithHTML(
html`No Info Kick usage:
<code>/noinfokick argument value</code>\n
No Info Kick arguments:\n
<code>on</code> - Enables No Info Kick.
<code>off</code> - Disables No Info Kick.
<code>cooldown</code> - Changes the kick cooldown, in seconds, cannot be lower than 300 seconds (5 minutes).
<code>username</code> - Switches username checking on and off, boolean, only accepts true or false.
<code>picture</code> - Switches picture checking on and off, boolean, only accepts true or false.
<code>bio</code> - Switches bio checking on and off, boolean, only accepts true or false.
<code>feedback</code> - Switches feedback messages on and off, boolean, only accepts true or false.
<code>tolerance</code> - Changes the tolerance for failure, a number from 1 to 3.
<code>settings</code> - Shows the current settings for No Info Kick.`
);
}
}
}
ctx.deleteMessage(ctx.message?.message_id);
}
}
const members = ctx.message?.new_chat_members?.filter(
(x) => x.username !== ctx.me
);
if (!members || members.length === 0) {
return next();
}
return Promise.all(
members.map(async (x) => {
let username = false;
let picture = false;
let bio = false;
let kickMessage = "";
let profilePictures: UserProfilePhotos;
let userBio: Chat;
let fails = 0;
let seen = userSeen(x.id, String(ctx.chat?.id));
let neverSeen = true;
let seenIndex = -1;
for (var i = 0; i < kickedOnce.length; i++) {
if (kickedOnce[i].user === x.id && kickedOnce[i].chat === String(ctx.chat?.id)) {
neverSeen = false;
seenIndex = i;
}
}
//logError("[noinfokick] New Member: [" + x.id + "] (" + x.username + ") {" + x.first_name + " " + x.last_name + "}\n");
if (settings.checkUsername) {
//logError("[noinfokick] Username: " + x.username + "\n");
if (x.username == null) {
username = true;
kickMessage = kickMessage + "No Username \n";
fails = fails + 1;
}
}
if (settings.checkPicture) {
profilePictures = await ctx.telegram.getUserProfilePhotos(x.id);
//logError("[noinfokick] Profile Picture Count: " + profilePictures.total_count + "\n");
if (profilePictures.total_count === 0) {
picture = true;
kickMessage = kickMessage + "No Profile Picture \n";
fails = fails + 1;
}
}
if (settings.checkBio) {
userBio = await ctx.telegram.getChat(x.id);
//logError("[noinfokick] Bio: " + userBio.bio + "\n");
if (userBio.bio == null) {
bio = true;
kickMessage = kickMessage + "No Bio \n";
fails = fails + 1;
}
}
if ((settings.active) && (fails >= settings.tolerance) && neverSeen && (!x.is_bot)) {
if (settings.feedback) {
ctx.replyWithHTML(html`User ${link(x)} has been kicked under suspicion of being an userbot. \n\n
If they aren't an userbot, they may attempt to rejoin in 5 minutes.`);
// ctx.replyWithHTML(
// html`User ${link(x)} has been kicked as suspicious for: \n
// <code>${kickMessage}</code>`
// );
}
ctx.kickChatMember(x.id, Math.floor((Date.now() / 1000) + settings.kickCooldown));
kickedOnce.push(seen);
} else {
if (!neverSeen) {
kickedOnce.splice(seenIndex, 1);
}
return next();
}
})
).catch((err) => logError("[noinfokick] " + err.message));
});