-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (68 loc) · 1.98 KB
/
index.js
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
import User from './user';
import {
saveUser,
getUsers,
} from './db'
const TelegramBot = require('node-telegram-bot-api');
const options = {
polling: true
};
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, options);
const socialCreditPlus = 'CAADAQADAgADf3BGHAXMZNg3IivIFgQ';
const socialCreditMinus = 'CAADAQADAwADf3BGHENZiEtY50bNFgQ';
const selfCreditMessage = "-20 omdat je jezelf credit probeert te geven."
bot.on('message', async (msg) => {
console.log(msg.chat.id)
if(msg.chat.id == -379248839 || msg.chat.id == 848122263){
console.log('eee')
if (isReplyAndSticker(msg)) {
if (msg.sticker.file_id == socialCreditPlus) {
if(isGivingSelfCredit(msg)){
handleCredit(msg, -20, selfCreditMessage)
} else {
handleCredit(msg, 20);
}
} else if (msg.sticker.file_id == socialCreditMinus) {
handleCredit(msg, -20);
}
}
}
});
async function handleCredit(msg, amount, customMessage) {
const chatId = msg.chat.id;
const firstName = msg.reply_to_message.from.first_name;
const userId = msg.reply_to_message.from.id;
let user = await User.getOrCreateUser(userId, firstName);
user.addCredit(amount);
saveUser(user);
if(customMessage){
bot.sendMessage(chatId, customMessage)
}
bot.sendMessage(chatId, `Totaal voor ${firstName}: ${user.socialCreditScore}`)
}
function isGivingSelfCredit(msg){
if(msg.reply_to_message.from.id == msg.from.id){
return true;
}
return false;
}
function isReplyAndSticker(msg) {
if (msg.reply_to_message) {
if (msg.sticker) {
if (msg.sticker.file_id == socialCreditPlus || msg.sticker.file_id == socialCreditMinus) {
return true;
}
}
}
return false;
}
bot.onText(/\/ranking/, async (msg) => {
let users = await getUsers();
let i = 1;
let response = " ";
await users.forEach(user => {
response += `#${i} ${user.firstName}. SocialScore: ${user.socialCreditScore} \n`;
i++;
});
bot.sendMessage(msg.chat.id, response)
});