-
Notifications
You must be signed in to change notification settings - Fork 8
/
commands.js
134 lines (98 loc) · 2.8 KB
/
commands.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
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
require('./models/db');
const User = require('./models/user.model');
const TransactionSchema = require('./models/transaction.schema');
checkUserRegistered = async (user) => {
try {
const doc = await User.findOne({
username: user.username
});
if (!doc) {
var user_obj = new User();
user_obj.name = user.name.toString();
user_obj.username = user.username.toString();
user_obj.chat_id = user.chat_id.toString();
user_obj.budget = 0;
user_obj.others =
user_obj.sharedAmounts = [];
user_obj.transactions = [];
user_obj.save();
return false;
}
return true;
} catch (e) {
console.log(`Error: ${e}`)
}
}
addUser = async (user, message) => {
let info = true;
try {
const name = message.text.toString();
const doc = await User.findOne({
username: user.username
});
if (doc.others.map((element) => {
return element.toLowerCase();
}).indexOf(name.toLowerCase()) > -1) {
info = false;
return info;
}
await User.findOneAndUpdate({
username: user.username
}, {
$push: {
others: name
}
});
}
catch (e) {
console.log(`Error: ${e}`)
}
return info;
}
addTransactions = async (user, message) => {
try {
var doc = await User.find({
username: user.username
});
}
catch (e) {
console.log(`Error: ${e}`)
}
}
viewUsers = async (user) => {
try {
const doc = await User.findOne({
username: user.username
});
if (!doc) {
reply = `You must register yourself with the bot first, try: '/start'`
return reply;
}
var reply = (doc.others.length !== 0) ? `These are the users you have added: ${doc.others}` : `You have not added any users yet.`;
} catch (e) {
console.log(`Error: ${e}`);
}
return reply;
}
viewTransactions = async (user) => {
try {
const doc = await User.findOne({
username: user.username
});
if (!doc) {
reply = `You must register yourself with the bot first, try: '/start'`
return reply;
}
var reply = (doc.transactions.length !== 0) ? `Your transactions are as follows: ${doc.transactions}`: `You have not made any transactions yet.`;
} catch (e) {
console.log(`Error: ${e}`);
}
return reply;
}
module.exports = {
checkUserRegistered: checkUserRegistered,
addUser: addUser,
addTransactions: addTransactions,
viewUsers: viewUsers,
viewTransactions: viewTransactions,
}