-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
447 lines (379 loc) · 11.2 KB
/
main.go
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* Proctected by MIT License: https://opensource.org/licenses/MIT
Author Telegram: @OneFreakDay
Date: 16/12/2017
Description:
A simple telegram bot to connect with a Block Explorer API cryptocoin
*/
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
//extern dependency
"github.com/yanzay/tbot"
)
var db Database
/* Obter dados do usuário */
func getData(user string) (*Dados, bool) {
fmt.Printf("$Procurando %s \n", user)
for i := 0; i < db.count; i++ {
if db.dados[i].charid == user {
return &db.dados[i], false
}
i++
}
fmt.Printf("$Sem sucesso! \n")
//alocar
if db.count == 0 {
db.dados = make([]Dados, 999)
}
//novo
db.dados[db.count] = Dados{"", "", 0.0, 0.0, 0.0, 0.0, 0.0, false, nil, 0, nil, 0}
return &db.dados[db.count], true
}
/*Obter anuncios de compra do usuário */
func getDataBuy(user string) (*Buy, bool) {
fmt.Printf("$Procurando Buys de %s \n", user)
for i := 0; i < db.count; i++ {
if db.dados[i].charid == user {
//buscar um local desativado
for x := 0; x < db.dados[i].buyc; x++ {
if db.dados[i].buys[x].end {
db.dados[i].buys[x].id = x
return &db.dados[i].buys[x], false
}
}
//novo
db.dados[i].buyc++
return &db.dados[i].buys[db.dados[i].buyc-1], true
}
i++
}
fmt.Printf("$Sem sucesso! \n")
return nil, true
}
/*Obter anuncios de venda do usuário */
func getDataSell(user string) (*Sell, bool) {
fmt.Printf("$Procurando Sells de %s \n", user)
for i := 0; i < db.count; i++ {
if db.dados[i].charid == user {
//algum local desativado?
for x := 0; x < db.dados[i].sellc; x++ {
if db.dados[i].sells[x].end {
return &db.dados[i].sells[x], false
}
}
//novo
db.dados[i].sellc++
return &db.dados[i].sells[db.dados[i].sellc-1], true
}
i++
}
fmt.Printf("$Sem sucesso! \n")
return nil, true
}
//adiciona usuário ou configura novos dados - /setup
func setuser(m *tbot.Message) {
//obtem dados do comando /setup
saldo := getbalance(m.Vars["carteira"])
tax := strtofloat(m.Vars["taxa"])
porcent := strtofloat(m.Vars["porc"])
//verifica se os dados foram recebidos do BE
if len(saldo) < 1 || strings.HasSuffix(saldo, "error") || strtofloat(saldo) < 5000 {
m.Reply("Setup: sem sucesso, acho que tem um inseto por aqui NEO!")
m.Reply("Output: " + saldo)
return
} else {
//se nao foi cadastrado, cadastra-lo
if db.count < MAX_DB {
//obter dados ou struct
user, nexiste := getData(m.From.UserName)
if nexiste {
m.Reply("Usuário novo? Bem vindo! Aguarde...")
time.Sleep(2000 * time.Millisecond)
m.Reply("$ Hackeando a NAS4!...")
time.Sleep(2000 * time.Millisecond)
m.Reply("hehe.. ")
}
//novo user
*user = Dados{
charid: m.From.UserName, //nome do usuário
charwallet: m.Vars["carteira"], //carteira do usuário
balance: strtofloat(saldo), //saldo
taxa: tax, //taxa
porcentagem: porcent, //porcentagem
last_rent: 0.0, //ultima renda verificada
rendimento: 0.0, //ultima renda verificada
isSet: true, //está configurado!
}
db.count++
m.Replyf("Hey [%s], o setup foi um sucesso e a NASA hackeada! Tente usar /me para iniciar o foguete.", m.From.FirstName)
return
} else {
m.Reply("Setup: sem sucesso :sob: ! - Usuários máximos cadastrados, tente mais tarde! :clock1:")
}
}
return
}
//comando /me - não requer argumentos, é preciso configra-lo
func mnrend(m *tbot.Message) {
//verificar se o usuário fez /setup
user, inativo := getData(m.From.UserName)
if inativo || !user.isSet {
m.Reply("Essse comando é automático.\nPortanto configure usando: \n/setup <carteira> <taxa> <porcentagem> !")
return
}
user.balance = strtofloat(getbalance(user.charwallet))
if user.balance >= 0 {
fmt.Printf("[%s] Carteira: %s %f\n", m.From.UserName, user.charwallet, user.balance)
user.last_rent = user.rendimento
//sem taxa e sem porcentagem
if user.taxa == 0 && user.porcentagem == 0 {
user.rendimento = user.balance - MN_BALANCE
//sem taxa e com porcentagem
} else if user.taxa == 0 {
user.rendimento = user.balance - MN_BALANCE
user.rendimento = (user.rendimento * user.porcentagem) / 100
//com taxa e com porcentagem
} else {
user.rendimento = user.balance - MN_BALANCE
user.rendimento = (user.rendimento * user.porcentagem) / 100
user.rendimento = user.rendimento - (user.rendimento * (user.taxa / 100))
}
} else {
m.Reply("falha ao obter saldo!")
return
}
m.Reply("Próximo recompensa: " + floattostr(user.rendimento) + "\nSem taxa: " + floattostr(user.rendimento+(user.rendimento*(user.taxa/100))))
}
func main() {
bot, err := tbot.NewServer(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Fatal(err)
}
db.count = 0
bot.HandleFunc("/sobre", sobre)
bot.HandleFunc("/start", sobre)
bot.HandleFunc("/wallet {carteira}", wallet)
//rendimento do masternode
bot.HandleFunc("/setup {carteira} {taxa} {porc}", setuser)
bot.HandleFunc("/me", mnrend)
//anunciar/finalizar/listar venda
bot.HandleFunc("/newsell {XNS} {MXT} {BRL} {contato}", newsell)
bot.HandleFunc("/endsell {id}", endsell)
bot.HandleFunc("/lsells", listsells)
//anunciar/finalizar/listar compra
bot.HandleFunc("/newbuy {MXT} {contato}", newbuy)
bot.HandleFunc("/endbuy {id}", endbuy)
bot.HandleFunc("/lbuys", listbuys)
bot.ListenAndServe()
}
func newsell(m *tbot.Message) {
user, nexiste := getData(m.From.UserName)
if nexiste {
m.Reply("Usuário cadastrado! Bem vindo!")
m.Reply("Tente não acomular sells!")
*user = Dados{
charid: m.From.UserName, //nome do usuário
sells: make([]Sell, MAX_DB_SELL),
buys: make([]Buy, MAX_DB_BUY),
}
db.count++
}
if user.sellc >= MAX_DB_SELL {
m.Replyf("Você excedeu %d pedidos.", MAX_DB_SELL)
return
}
//dados
xnsn := m.Vars["XNS"]
mxtf := m.Vars["MXT"]
brlf := m.Vars["BRL"]
contn := m.Vars["contato"]
//log
fmt.Printf("#BUY {%s} {%s} {%s}\n", m.From.UserName, mxtf, brlf, contn)
//fail?
if len(xnsn) < 0 || len(mxtf) < 0 || len(brlf) < 0 || len(contn) < 0 {
m.Reply("Use: /annsell <xns_n> <mxt_pedido> <brl_pedido> <contato> ")
return
} else {
//cadastro
bi, novo := getDataSell(m.From.UserName)
*bi = Sell{
xns: xnsn,
contato: contn,
mxt_price: strtofloat(mxtf),
brl_price: strtofloat(brlf),
end: false,
}
//output
m.Reply("--------------------")
m.Reply("▌▌▌▌EXTRATO▌▌▌▌")
if novo {
m.Replyf("▌ID: %d", user.sellc)
} else {
m.Replyf("▌ID: %d", bi.id)
}
m.Reply("▌de: " + bi.contato)
m.Reply("▌MN: XNs" + bi.xns)
m.Replyf("▌mxt_price: %f", bi.mxt_price)
m.Replyf("▌brl_price: %f", bi.brl_price)
m.Reply("--------------------")
m.Reply("Nao apague o extrato!")
m.Reply("--------------------")
}
}
func endsell(m *tbot.Message) {
user, nexiste := getData(m.From.UserName)
if nexiste {
m.Reply("Você não está cadastrado!")
return
}
if user.sellc <= 0 {
m.Reply("Você não efetuou nenhum anúncio de venda.")
return
}
//dados
bid, err := strconv.Atoi(m.Vars["id"])
//fail?
if err != nil {
m.Reply("Use: /endsell <id>")
return
} else {
//cadastro
user.sells[bid].end = true
//output
m.Reply("Feito!")
}
}
func newbuy(m *tbot.Message) {
user, inexiste := getData(m.From.UserName)
if inexiste {
m.Reply("Usuário cadastrado! Bem vindo!")
m.Reply("Tente não acomular buys!")
*user = Dados{
charid: m.From.UserName, //nome do usuário
sells: make([]Sell, MAX_DB_SELL),
buys: make([]Buy, MAX_DB_BUY),
}
db.count++
}
if user.buyc >= MAX_DB_BUY {
m.Replyf("Você excedeu %d pedidos.", MAX_DB_BUY)
return
}
//dados
mxtf := m.Vars["MXT"]
contn := m.Vars["contato"]
//log
fmt.Printf("#SELL {%s} {%s}\n", m.From.UserName, mxtf, contn)
//invalido
if len(mxtf) < 0 || len(contn) < 0 {
m.Reply("Use: /annbuy <mxt_pedido> <contato> ")
return
} else {
//cadastro
bi, novo := getDataBuy(m.From.UserName)
*bi = Buy{
contato: contn,
mxt_price: strtofloat(mxtf),
end: false,
}
//output
m.Reply("--------------------")
m.Reply("▌▌▌▌EXTRATO▌▌▌▌")
if novo {
m.Replyf("▌ID: %d", user.buyc)
} else {
m.Replyf("▌ID: %d", bi.id)
}
m.Reply("▌de: " + bi.contato)
m.Replyf("▌mxt_price: %f", bi.mxt_price)
m.Reply("--------------------")
m.Reply("Nao apague o extrato!")
m.Reply("--------------------")
}
}
func endbuy(m *tbot.Message) {
user, nexiste := getData(m.From.UserName)
if nexiste {
m.Reply("Você não está cadastrado!")
return
}
if user.buyc <= 0 {
m.Reply("Você não efetuou nenhum anuncio de compra.")
return
}
//dados
bid, err := strconv.Atoi(m.Vars["id"])
//fail?
if err != nil || bid < 0 || bid > MAX_DB_BUY {
m.Reply("Use: /endbuy <id>")
return
} else {
//cadastro
user.buys[bid].end = true
//output
m.Reply("Feito!")
}
}
//listar ordens de venda
func listsells(m *tbot.Message) {
m.Reply("ID - XNs - MXT pedido - BRL pedido - Contato ")
for i := 0; i < db.count; i++ {
fmt.Print(db.dados[i].sellc)
for x := 0; x < db.dados[i].sellc; x++ {
if !db.dados[i].sells[x].end {
m.Replyf("%d - %s - %f - %f - %s", x,
db.dados[i].sells[x].xns,
db.dados[i].sells[x].mxt_price,
db.dados[i].sells[x].brl_price,
db.dados[i].sells[x].contato)
}
}
}
}
//listar ordens de compra
func listbuys(m *tbot.Message) {
m.Reply("ID - lance de MXT - Contato ")
for i := 0; i < db.count; i++ {
for x := 0; x < db.dados[i].buyc; x++ {
if !db.dados[i].buys[x].end {
m.Replyf("%d - %f - %s", x,
db.dados[i].buys[x].mxt_price,
db.dados[i].buys[x].contato)
}
}
}
}
//about
func sobre(m *tbot.Message) {
m.Reply("MN BOT - 0.0.2 - @OneFreakDay")
m.Replyf("------------------------------")
m.Replyf("---- Comandos básicos -----")
m.Replyf("------------------------------")
m.Replyf("/wallet <carteira> - Obter saldo de uma carteira.")
m.Replyf("------------------------------------\n")
m.Replyf("/newsell <xns_n> <mxt_pedido> <brl_pedido> <contato> - Anunciar venda de XNs\n")
m.Replyf("/endsell <id> - Finalizar anuncio de venda de XNs\n")
m.Replyf("/mysells - Listar suas vendas XNs\n")
m.Replyf("------------------------------------\n")
m.Replyf("/newbuy <mxt_max> <contato> - Anunciar compra de XNs\n")
m.Replyf("/endbuy <id> - Finalizar anuncio de compra de XNs\n")
m.Replyf("/mybuys - Listar suas compras XNs\n")
m.Replyf("------------------------------------\n")
m.Replyf("/lbuys - Listar ofertas de compra\n")
m.Replyf("/lsells - Listar ofertas de venda\n")
m.Replyf("\n----------------------------------------------------------")
m.Replyf("---- Configuração para rendimento de masternode -----")
m.Replyf("----------------------------------------------------------")
m.Replyf("- 1º use:")
m.Replyf("/setup <carteira> <taxa> <porcentagem>\n Use para configurar dados do Masternode. \n- taxa = Taxa do Masternode \n- porcentagem = sua porcentagem do Masternode\n")
m.Replyf("- 2º agora você só precisa usar:")
m.Replyf("/me - Sua Próxima recompensa\n")
m.Replyf("*Seu nome de usuário é salvo apenas para poder identificar suas configurações.")
m.Replyf("*Dados são apagados quando o bot fica offline.")
m.Reply("Código: https://github.com/apollomatheus/mxtmnbot")
}