-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
255 lines (236 loc) · 7.89 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
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
const Discord = require("discord.js");
const { Key } = require("semux-js");
const botSettings = require("./config/config-bot.json");
const allowedCommands = require("./config/allowed-commands.json");
const { toHexString } = require("./utils.js");
const { scanNewBlock } = require("./alerts.js");
const { Users } = require("./models");
const getTop = require("./actions/getTop");
const getStats = require("./actions/getStats");
const getBalance = require("./actions/getBalance");
const doRain = require("./actions/doRain");
const doFaucet = require("./actions/doFaucet");
const doClaim = require("./actions/doClaim");
const sendCoins = require("./sendCoins");
const prefix = botSettings.prefix;
const bot = new Discord.Client({ disableEveryone: true });
bot.on("ready", () => {
console.log("Bot is connected.");
});
async function changeStats(senderId, recieverId, value) {
if (value.includes(",")) value = value.replace(/,/g, ".");
let amount = parseFloat(value);
let sender = await Users.findOne({ where: { discord_id: senderId } });
let reciever = await Users.findOne({ where: { discord_id: recieverId } });
await sender.update({
sent: sender.sent + amount,
});
await reciever.update({
received: reciever.received + amount,
});
}
bot.on("message", async (msg) => {
// replace double whitespaces with a single one
msg.content = msg.content.toString().replace(/ +/g, " ");
const args = msg.content.trim().split(" ");
const authorId = msg.author.id;
if (allowedCommands[args[0]]) {
console.log(
`[${new Date()}] ${msg.author.username}#${msg.author.discriminator}: ${
msg.content
}`
);
}
switch (msg.content.toLocaleLowerCase()) {
case `${prefix}claim`:
await doClaim(authorId, msg);
return;
case `${prefix}topdonators`:
return msg.channel.send(await getTop("sent"));
case `${prefix}toprecipients`:
return msg.channel.send(await getTop("received"));
case `${prefix}stats`:
await getStats(msg);
return;
case `${prefix}help`:
return msg.channel.send(
`SemuxBot commands:\n` +
`**${prefix}balance** - show your balance.\n` +
`**${prefix}tip** *<@username>* *<amount>* *<'comment'>*- send SEM to a Discord user.\n` +
`**${prefix}withdraw** *<address>* *<amount>* - withdraw SEM to your personal address.\n` +
`**${prefix}getAddress** - get your personal deposit/tips address.\n` +
`**${prefix}topDonators** - show the most active donators.\n` +
`**${prefix}topRecipients** - show the luckiest recipients.\n` +
`**${prefix}rain** *<amount>* - gives all online users a portion of sem.\n` +
`**${prefix}faucet** *<amount>* - donate sem to faucet address.\n` +
`**${prefix}claim** - claim 1 sem if faucet address has it. *(works once a day)*\n` +
`**${prefix}stats** - show current Semux network stats.`
);
}
// balance
if (
msg.content.startsWith(`${prefix}balance`) ||
msg.content.startsWith(`${prefix}bal`)
) {
await getBalance(msg, authorId);
return;
}
// tip to username
if (msg.content.startsWith(`${prefix}tip `)) {
let comment = "";
const amount = args[2];
const username = args[1];
if (args[3] && args[3].includes("'")) {
try {
comment = msg.content.trim().match(/'([^']+)'/)[1];
} catch (e) {
return msg.reply("Close quotes please");
}
}
let usernameId = username;
if (username.includes("@")) {
usernameId = username.substring(2, username.length - 1);
usernameId = usernameId.replace("!", "");
}
console.log(`Tipping to ${usernameId}`);
let userAddress = await Users.findOne({
where: { discord_id: usernameId },
});
if (!userAddress) {
const newUserName = bot.users.find((user) => user.id === usernameId);
if (!newUserName) {
console.log("Cannot find this user on the server. Aborting.");
return msg.reply("Cannot find this user on the server.");
}
console.log(
"Recipient doesn't have public address yet. Generating new key pair."
);
const key = Key.generateKeyPair();
const privateKey = toHexString(key.getEncodedPrivateKey());
const address = "0x" + key.toAddressHexString();
var newRegister = await Users.create({
username: newUserName.username,
discord_id: usernameId,
address: address,
private_key: privateKey,
});
userAddress = newRegister.address;
} else {
userAddress = userAddress.address;
}
let reciever = bot.users.find((user) => user.id === usernameId);
if (!reciever) return msg.reply("Cannot find this user on the server.");
try {
var trySend = await sendCoins(
authorId,
userAddress,
amount,
msg,
comment
);
} catch (e) {
// console.log(e)
}
if (trySend.error) return msg.reply(trySend.reason);
await changeStats(authorId, usernameId, amount);
try {
await reciever.send(
`You've received tips. TX: <https://semux.info/explorer/transaction/${trySend.hash}> \nSend me: \`/balance\` or \`/help\` to find more details`
);
} catch (e) {
console.error(e);
}
await msg.reply(
`Tip sent. TX: <https://semux.info/explorer/transaction/${trySend.hash}>`
);
}
// get donate address
if (
msg.content.toLowerCase().startsWith(`${prefix}getaddress`) ||
msg.content.toLowerCase().startsWith(`${prefix}address`)
) {
const user = await Users.findOne({ where: { discord_id: authorId } });
if (!user) {
const key = Key.generateKeyPair();
const privateKey = toHexString(key.getEncodedPrivateKey());
const address = "0x" + key.toAddressHexString();
if (address) {
let text = `This is your unique deposit address: **${address}**\n
You can deposit some SEM to this address and use your coins for tipping.\n
People will be tipping to this address too. Try to be helpful to the community ;)
`;
try {
await msg.author.send(text);
} catch (e) {
console.error(e);
msg.channel.send(text);
}
await Users.create({
username: msg.author.username,
discord_id: authorId,
address: address,
private_key: privateKey,
});
}
} else {
let text = `Your deposit address is: **${user.address}**`;
try {
await msg.author.send(text);
} catch (e) {
console.error(e);
msg.channel.send(text);
}
}
}
// withdraw
if (msg.content.startsWith(`${prefix}withdraw`)) {
const amount = args[2];
const toAddress = args[1];
let trySend;
try {
trySend = await sendCoins(authorId, toAddress, amount, msg);
} catch (e) {
// console.log(e)
}
let responseToWithdrawal =
"Your withdrawal request has been processed successfully.";
if (trySend.error) {
responseToWithdrawal = trySend.reason;
}
try {
await msg.author.send(responseToWithdrawal);
} catch (e) {
console.error(e);
}
}
// rain
if (msg.content.startsWith(`${prefix}rain`)) {
const amount = args[1];
await doRain(authorId, bot, amount, msg);
return;
}
// faucet
if (msg.content.startsWith(`${prefix}faucet`)) {
const amount = args[1];
await doFaucet(authorId, bot, amount, msg);
}
});
setInterval(async function () {
let result = await scanNewBlock();
if (result.error) {
return;
}
const channel = bot.channels.find((c) => c.name === "trading");
for (let tx of result.transfers) {
if (tx.type === "deposited") {
channel.send(
`**[whale alert]** ${tx.value} SEM ${tx.type} to ${tx.exchange} :inbox_tray:`
);
} else {
channel.send(
`**[whale alert]** ${tx.value} SEM ${tx.type} from ${tx.exchange} :outbox_tray:`
);
}
}
}, 5 * 1000);
bot.login(botSettings.token);