-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
225 lines (183 loc) · 6.46 KB
/
main.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
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
import { createClient } from '@supabase/supabase-js';
import TelegramBot from 'node-telegram-bot-api';
//supabase
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
const supabase = createClient(supabaseUrl, supabaseKey);
//Bot declaration
const botToken = process.env.BOT_TOKEN;
const bot = new TelegramBot(botToken, { polling: true });
//saldo command
bot.onText(/\/saldo/, async (msg) => {
const chatId = msg.chat.id;
const telegramId = msg.from.id;
const name = msg.from.username || msg.from.first_name;
// DB query
const { data, error } = await supabase
.from('users')
.select('euricoins')
.eq('telegram_id', telegramId);
if (error || !data || data.length === 0) {
bot.sendMessage(chatId, 'No se encontraron tus euricoins. ¿Estás registrado?');
return;
}
const euricoins = data[0].euricoins;
bot.sendMessage(chatId, ` ${name}, tienes ${euricoins} euricoins.`);
});
//sumar command
bot.onText(/\/sumar (\d+) @(.+)/, async (msg, match) => {
const adminId = msg.from.id;
const euricoins = parseInt(match[1]);
const username = match[2];
//admin check
const { data: adminData, error: adminError } = await supabase
.from('users')
.select('is_admin')
.eq('telegram_id', adminId);
if (adminError || !adminData || !adminData[0].is_admin) {
bot.sendMessage(msg.chat.id, 'Quien cojones eres???');
return;
}
//DB query
const { data: usuario, error: errorUsuario } = await supabase
.from('users')
.select('id, telegram_id, euricoins')
.eq('name', username);
if (errorUsuario || !usuario || usuario.length === 0) {
bot.sendMessage(msg.chat.id, 'No se encontró al usuario.');
return;
}
const usuarioId = usuario[0].id;
const nuevoPuntaje = usuario[0].euricoins + euricoins;
const { error: errorUpdate } = await supabase
.from('users')
.update({ euricoins: nuevoPuntaje })
.eq('id', usuarioId);
if (errorUpdate) {
bot.sendMessage(msg.chat.id, 'Hubo un error actualizando los euricoins.');
return;
}
//transaction log
await supabase
.from('transactions')
.insert([{ user_id: usuarioId, cantidad: euricoins, administrador_id: adminId }]);
bot.sendMessage(msg.chat.id, `@${msg.from.username} ha añadido ${euricoins} euricoins a @${username}.`);
});
//restar commadn
// Restar command
bot.onText(/\/restar (\d+) @(.+)/, async (msg, match) => {
const adminId = msg.from.id;
const euricoins = parseInt(match[1]);
const username = match[2];
// Admin check
const { data: adminData, error: adminError } = await supabase
.from('users')
.select('is_admin')
.eq('telegram_id', adminId);
if (adminError || !adminData || !adminData[0].is_admin) {
bot.sendMessage(msg.chat.id, 'Quien cojones eres???');
return;
}
// DB query
const { data: usuario, error: errorUsuario } = await supabase
.from('users')
.select('id, telegram_id, euricoins')
.eq('name', username);
if (errorUsuario || !usuario || usuario.length === 0) {
bot.sendMessage(msg.chat.id, 'No se encontró al usuario');
return;
}
const usuarioId = usuario[0].id;
const saldo = usuario[0].euricoins; // Cambiado de `data` a `usuario`
const nuevoPuntaje = saldo - euricoins; // Usa `saldo` aquí
// Check saldo
if (nuevoPuntaje < 0) {
bot.sendMessage(msg.chat.id, `@${username} es pobre y no puedes quitarle ${euricoins} euricoins porque tiene ${saldo} euricoins`);
return;
}
const { error: errorUpdate } = await supabase
.from('users')
.update({ euricoins: nuevoPuntaje })
.eq('id', usuarioId);
if (errorUpdate) {
bot.sendMessage(msg.chat.id, 'Hubo un error actualizando los euricoins');
return;
}
// Transaction log
await supabase
.from('transactions')
.insert([{ user_id: usuarioId, cantidad: -euricoins, administrador_id: adminId }]);
bot.sendMessage(msg.chat.id, `@${msg.from.username} ha expropiado ${euricoins} euricoins a @${username}`);
});
//new user command
bot.onText(/\/registrar/, async (msg) => {
const chatId = msg.chat.id;
const telegramId = msg.from.id;
const name = msg.from.username || msg.from.first_name;
// Verificar si el usuario ya está registrado
const { data: existingUser, error: userError } = await supabase
.from('users')
.select('id')
.eq('telegram_id', telegramId);
if (userError) {
console.error('Error al verificar registro:', userError); // Log del error
bot.sendMessage(chatId, 'Hubo un error verificando tu registro.');
return;
}
if (existingUser && existingUser.length > 0) {
bot.sendMessage(chatId, 'Ya estás registrado en el sistema.');
return;
}
// Registrar al nuevo usuario
const { error: insertError } = await supabase
.from('users')
.insert([{
telegram_id: telegramId,
name: name,
euricoins: 0, // Iniciar con 0 euricoins
is_admin: false // No es administrador por defecto
}]);
if (insertError) {
console.error('Error al registrarse:', insertError); // Log del error
bot.sendMessage(chatId, 'Hubo un error al registrarte.');
} else {
bot.sendMessage(chatId, `¡Bienvenido, ${name}! Te has registrado correctamente.`);
}
});
//new admin command
bot.onText(/\/new_admin @(.+)/, async (msg, match) => {
const adminId = msg.from.id;
const username = match[1];
//admin check
const { data: adminData, error: adminError } = await supabase
.from('users')
.select('is_admin')
.eq('telegram_id', adminId);
if (adminError || !adminData || !adminData[0].is_admin) {
bot.sendMessage(msg.chat.id, 'Quien cojones eres???');
return;
}
//DB query
const { data: usuarioData, error: usuarioError } = await supabase
.from('users')
.select('id, is_admin')
.eq('name', username);
if (usuarioError || !usuarioData || usuarioData.length === 0) {
bot.sendMessage(msg.chat.id, 'No se encontró al usuario.');
return;
}
//Already admin
if (usuarioData[0].is_admin) {
bot.sendMessage(msg.chat.id, `@${username} ya es administrador.`);
return;
}
//admin update: is_admin = true
const { error: updateError } = await supabase
.from('users')
.update({ is_admin: true })
.eq('id', usuarioData[0].id);
if (updateError) {
bot.sendMessage(msg.chat.id, 'Hubo un erroral añadir un nuevo administrador.');
return;
}
});