This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
89 lines (77 loc) · 2.62 KB
/
app.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
import { MongoClient, Collection } from 'mongodb';
import { Telegraf } from 'telegraf';
import fs from 'fs';
const botToken = process.env.BOT_TOKEN;
const ChatId = process.env.CHAT_ID;
const url = process.env.MONGO_URL!;
if (url == null) {
console.log("No DB URL provided");
process.exit(1);
}
let fileBuffer: string = fs.readFileSync('wordlist.txt').toString();
let bannedWord: string[] = fileBuffer.replace(/\r\n/g, '\n').split('\n');
let listUser: any[] = []
async function main(): Promise<void> {
console.log("Connecting to MongoDB");
try{
const client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
CleanDB(client);
} catch (err) {
console.error("Failed to connect to db ", err);
process.exit(1);
}
}
function CleanDB(client: MongoClient): void {
const col: Collection = client.db("UbotIndo").collection("GBANS");
col.find({}).toArray((err, data) => {
if (err) throw err;
if (data.length) {
for (let user of data) {
for (let word of bannedWord) {
if ((user.reason).search(escape(word)) != -1) {
col.deleteOne({ _id: user._id });
AppendUser(user, word, (result) => {
listUser.push(result);
});
break;
}
}
}
SendMessage(listUser);
console.log(`Done deleting ${listUser.length} user(s) from db`);
} else {
console.log("No data");
}
});
}
function AppendUser(user: any, match: string, callback: (result: any) => void): void {
let data = {
id: user._id.toString(),
name: user.name,
reason: user.reason,
matched: match
}
callback(data);
}
function SendMessage(deleted: any[]): void {
if (deleted.length && botToken && ChatId) {
const bot = new Telegraf(botToken);
let text: string = `#AUTOCLEAN\nDeleting ${deleted.length} gbanned user!\n`;
deleted.forEach((value) => {
let name = `\n× Name: <b>${value.name}</b> with id <b>${value.id}</b>\n`;
let reason = ` reason: <b>${value.reason}</b> (matching <b>${value.matched}</b>)\n`
text = text.concat(name, reason);
})
bot.telegram.sendMessage(ChatId, text, { parse_mode: "HTML" });
}
}
if (require.main === module) {
setTimeout(() => {
console.log("Exiting...");
process.exit(0);
}, 60000) // set manual timeout of 1 minutes
main();
}