forked from Amatack/eCashDice_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
211 lines (163 loc) · 8.38 KB
/
bot.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
import { Telegraf} from 'telegraf'
import {message} from 'telegraf/filters'
import dbConnect from './database.js'
import {everySecond} from './everySecond.js'
import { hoursLeft } from './hoursLeft.js'
import {smtp} from './smtp.js'
import Release from './models/DiceRelease.js'
import Winner from './models/Winner.js'
import {token, idChat, idChannel, smtpPassword, emailAddress, threadId } from './configs/constants.js'
import UserAddress from './models/UserAddress.js'
if (token === undefined) {
throw new Error('BOT_TOKEN must be provided!')
}
const bot = new Telegraf(token)
// Represents 24 hours
let timeout = {
first: false,
second: false,
fourth: false,
third: false,
fifth: false,
sixth: false,
seventh: false,
eighth: false,
nineth: false,
tenth: false,
eleventh: false,
twelfth: false
}
let timeLeft = new String
setInterval(() => {
everySecond(timeout,idChat, bot, async (now, timeoutTwelfth)=>{
timeLeft = now
if(now === "00:00" && timeoutTwelfth === false){
let messageEmail = new String
const winners = await Winner.find()
winners.forEach(element => {if(element.address) (messageEmail = messageEmail + " " + element.address)})
await smtp(smtpPassword, messageEmail, emailAddress)
await Winner.deleteMany({})
await Release.deleteMany({})
await UserAddress.deleteMany({ address: { $exists: false } })
bot.telegram.sendMessage(idChannel, `#RESET \nNew chance to win`)
}
})
}, 1000)
bot.command('time', (ctx) => {
let split = hoursLeft(timeLeft)
ctx.reply(`In ` + split[0] + ":" + split[1] + " hours attempts reset to win")
})
bot.on(message("text"), async (ctx) =>{
const { from, chat } = ctx.message
if(chat.id !== Number(idChat))return
const word = "ecash:"
const withAddress = await UserAddress.find({ tgId: from.id })
//Suggestion to enter address
if(withAddress.length === 0 ){
ctx.replyWithHTML(`Welcome ${from.first_name}\n\nTo be able to receive rewards, please register your eCash address by using the /register command, followed by your address.\n\nExample:\n\n<code>/register eCash:address</code>`)
const userAddress = {
tgId: from.id
}
const newUserAddress = new UserAddress(userAddress)
await newUserAddress.save()
}
const winners = await Winner.find()
for(let i = 0; i < winners.length; i++){
if(winners[i].telegramId === from.id){
let split = ctx.message.text.split(" ")
split.forEach(async (element) => {
if(element.length === 48 && element.includes(word)){
await Winner.findOneAndUpdate(
{ telegramId: from.id }
,{ $set: { address: element } },
{ new: true })
}
});
}
}
})
bot.on(message("dice"), async (ctx) => {
const {dice, forward_from, from, message_thread_id, message_id, chat } = ctx.message
if(chat.id !== Number(idChat))return
// without: || forward_from. for tests
if(dice.emoji !== "🎲" || forward_from) return
if(Number(threadId) !== message_thread_id){
await ctx.deleteMessage(message_id)
return
}
let userReleasesInBd = 0
let sucessfulDiceNumbers = 0
let user;
//GET
const releases = await Release.find()
for(let i = 0; i < releases.length; i++){
//you get objects
user = releases[i]
if(user.telegramId === from.id){
userReleasesInBd++
//For each die in 1
if(user.value === 1){
sucessfulDiceNumbers++
}
if(userReleasesInBd === 3){
return
}
}
}
if(userReleasesInBd < 3 && from.is_bot === false){
const newRelease = new Release(
{
telegramId: from.id,
value: dice.value,
})
const userWinner = {
telegramId: from.id
}
const newWinner = new Winner(userWinner)
bot.telegram.sendMessage(idChannel, `#id${from.id} \nname: ${from.first_name} \nusername: @${from.username} \nvalue: ${dice.value} `)
try {
//POST
//if(dice.value === 6 && userReleasesInBd < 1){
//setTimeout(() => ctx.reply("🎲 Congratulations, you have rolled a Six (6) on your first roll. \n \n You didn't win the Jackpot (3x One) but you will be rewarded some #Grumpy😾 eTokens. \n \n 👉 Reply to this message with your eToken:address and we will send you some Grumpy (GRP). \n \n ℹ️ If you don't have an eCash wallet that support eTokens, you can create one at https://cashtab.com web-wallet. \n \n ⚠️Note: After setting up your new wallet, please take the time to go to the ⚙️Settings menu to write down and store your 12 Word Seed Phrase. It acts as your Backup to your funds in case of loss of device. Keep this 12 Word Backup Phrase Safe and do not disclose it to anyone."), 3000)
//await newWinner.save()
//}
if(userReleasesInBd < 1){
if(dice.value === 1 ){
await ctx.replyWithHTML(`${from.first_name} multiply x3 possible reward by paying 1 million Grumpy ($GRP) to this address:\n\n<code>ecash:qq5v4wmfhclzqur4wnt6phwxt2qpk6h9nyesy04fn0</code>`)
}else{
const withAddress = await UserAddress.find({ tgId: from.id })
//Suggestion to enter address
if(withAddress.length === 0 ){
ctx.replyWithHTML(`Welcome ${from.first_name}\n\nTo be able to receive rewards, please register your eCash address by using the /register command, followed by your address.\n\nExample:\n\n<code>/register ecash:address</code>`)
const userAddress = {
tgId: from.id
}
const newUserAddress = new UserAddress(userAddress)
await newUserAddress.save()
}
}
}
if(sucessfulDiceNumbers === 2){
const userAddress = await UserAddress.findOne({ tgId: from.id });
if(userAddress === null && dice.value === 1){
setTimeout( () => ctx.reply("🎉 Congratulations! \n \nYou have won the 🎲 Dice Game's reward!🏅 \n \nPlease reply to this message with your eCash (XEC) wallet address and admin @eKoush will reward you as soon as possible!"), 3000)
}else if(userAddress !== null && dice.value === 1){
setTimeout( () => ctx.reply("🎉 Congratulations! \n \nYou have won the 🎲 Dice Game's reward!🏅 \n \nYour address registered is\n"+userAddress.address+"\n\nAdmin @eKoush will reward you as soon as possible!"), 3000)
}else{
if(userAddress === null){
setTimeout(() => ctx.reply("🎲 Aww, almost! \n \nYou didn't win the Jackpot (3x One) but you will be rewarded some #Grumpy😾 eTokens, instead! \n \n👉 Please share your eCash address. Note that your wallet needs to support eTokens. We recommend creating a wallet on Cashtab.com. If you are not sure if your wallet supports eTokens, feel free to ask! \n \n⚠️Note: After setting up your new wallet, please take the time to go to the ⚙️Settings menu to write down and store your 12 Word Seed Phrase. It acts as your Backup to your funds in case of loss of device. Keep this 12 Word Backup Phrase Safe and do not disclose it to anyone."), 3000)
}else{
newWinner.address = userAddress.address
setTimeout(() => ctx.reply("🎲 Aww, almost! \n \nYou didn't win the Jackpot (3x One) but you will be rewarded some #Grumpy😾 eTokens, instead! \n \n👉 Your address registered is\n"+userAddress.address+"\n\nAdmin @eKoush will reward you as soon as possible!"), 3000)
}
await newWinner.save()
}
}
await newRelease.save()
} catch (error) {
console.log("Error saving to database")
}
}
})
dbConnect()
bot.launch()