-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
130 lines (87 loc) · 3.73 KB
/
index.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
import TimeNotification from './functions/timeNotification'
require('dotenv').config()
const http = require('http');
const mongoose = require('mongoose')
const TelegramBot = require('node-telegram-bot-api');
import { BotCommand } from './commandAPI'
import * as botConfig from './botConfig'
import * as nicknameCRUD from './MongoDB/CRUD/CustomNicknamesCRUD'
const token = process.env.TOKEN;
const bot = new TelegramBot(token, { polling: true });
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
let commands: BotCommand[] = [];
loadCommands(`${__dirname}/functions/commands`);
server.listen(process.env.PORT || 5000, () => {
console.log(`Server running`);
bot.sendMessage(process.env.CHAT_ID, 'Updated to version 4.2.0\nYou can see the changes in https://github.com/demo-hub/PinkGlobal420TelegramBot/blob/master/Changelog.md\n\nIf you have any doubt, contribution or suggestion please feel free to hit me up in Twitter or Telegram @suembra or create an issue in Github https://github.com/demo-hub/PinkGlobal420TelegramBot.\nThank you José Pinto (https://twitter.com/JoseAoQuadrado) and Luis Azevedo (https://github.com/LuisAzeved) for your contributions!\nIf you want to help please go on Github and star and/or watch the project or follow me and José Pinto on Twitter @suembra and @JoseAoQuadrado')
new TimeNotification().timeNotification(bot);
bot.on("message", (msg) => {
var checkMention = new RegExp('^@');
var checkCommand = new RegExp('^/')
if (checkMention.test(msg.text)) {
handleMentions(msg);
} else if (checkCommand.test(msg.text)) {
handleCommand(msg);
} else {
handleUserMessages(msg);
}
})
//commands.mentions(bot)
//commands.next(bot)
//commands.ping(bot)
});
async function handleMentions(msg: any) {
var checkBotMention = new RegExp(/(.?)@PinkGlobal420Bot(.?)/);
const connector = mongoose.connect(botConfig.connectionString, {
useNewUrlParser: true
});
let customNick = await connector.then(async () => {
return nicknameCRUD.GetUserByID(msg.from.id);
});
if (checkBotMention.test(msg.text)) {
if (customNick == null) {
if (!msg.from.username) {
customNick = msg.from.first_name;
} else {
customNick = "@" + msg.from.username;
}
}
var mention = `[${customNick}](tg://user?id=${msg.from.id})`;
bot.sendMessage(msg.chat.id, `Hi ${mention}\nFazeçe irmão`, {
parse_mode: "MarkdownV2"
});
// bot.sendMessage(msg.chat.id, "Fazeçe irmão");
}
}
function handleUserMessages(msg: any) {
//This is a temporary solution
var checkDepression = new RegExp(/(.?)(deprimido|depressão|depressao|deprimir|deprimo)(.?)/);
if (checkDepression.test(msg.text)) {
bot.sendMessage(msg.chat.id, "Eu tenho a solução para a tua depressão. Deposita o teu salário na minha congragação.");
}
}
async function handleCommand(msg: any) {
let command = msg.text.split(" ")[0].replace('/', "");
let args = msg.text.split(" ").slice(1);
for (const commandType of commands) {
try {
if (!commandType.isCommand(command)) {
continue;
}
await commandType.runCommand(args, bot, msg);
} catch (exception) {
console.log(exception);
}
}
}
function loadCommands(commandsPath: string) {
//If command list doesn't exist or is empty
if (!botConfig.config.commands || (botConfig.config.commands as BotCommand[]).length === 0) { return; }
for (const command of botConfig.config.commands as BotCommand[]) {
commands.push(command);
}
}