-
Notifications
You must be signed in to change notification settings - Fork 5
/
bot.201805291006
178 lines (137 loc) · 5.7 KB
/
bot.201805291006
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
const discord = require('discord.js');
const auth = require('./auth.json');
const logger = require('winston');
const snek = require('snekfetch');
const helpText = `
Commercium links:
Whitepaper:
<http://commercium.net/Commercium_White_Paper.pdf>
Block Explorers:
<https://explorer.commercium.net/>
<http://explorer.commercium.in:3001/>
<http://explorer.chattymining.com/>
Wallets:
<https://wallet.commercium.net/> (Web wallet)
<https://github.com/CommerciumBlockchain/Commercium/releases> (Client wallet)
The cmmbot commands are:
\`?pools\` (Pools list)
\`?stats\` (Price, Nethash Etc)
\`?cmm hashrate\` (estimate mining profit) example: \`?cmm 3000\`
`;
const poolList = [
{name: "VPOOL", url: "https://vpool.io", pool: "commercium"},
{name: "cmmpool.eu", url: "http://cmmpool.eu", pool: "commercium"},
{name: "DME", url: "http://pool.digitalminingexperts.com", pool: "commerciumpool"},
{name: "Dragon Pool", url: "https://cmm.dragonpool.xyz", pool: "commercium"},
{name: "COINBLOCKERS", url: "https://cmm.coinblockers.com", pool: "commercium"},
{name: "Miningspeed", url: "https://commercium.miningspeed.com", pool: "commercium"}
];
const statsUrl = "http://cmmpool.eu/api/stats";
const waitText = "Please wait 1 minute, I am recharging.";
let item = '';
const talkedRecently = new Set();
const bot = new discord.Client();
bot.login(auth.token);
bot.on('ready', evt => {
bot.user.setActivity('Type ?help');
});
bot.on('message', async message => {
if (message.content.substring(0, 1) === '?') {
let args = message.content.substring(1).split(' ');
let cmd = args[0];
let dm = true;
let validChannel = ['botchat', 'bot-command', 'bot-test', 'mining'];
if (validChannel.includes(message.channel.name)) {
dm = false;
}
args = args.splice(1);
switch (cmd) {
case 'help':
sendMessage(dm, message, helpText);
break;
case 'pools':
item = message.author.id + "_pools";
if (isCooldown(talkedRecently, item)) {
sendMessage(dm, message, waitText, true);
break;
}
setCooldown(talkedRecently, item);
let text1 = "Pool list may take several seconds to get stats!";
let poolText = "";
sendMessage(dm, message, text1);
for (let i = 0; i < poolList.length; i++) {
poolText += poolList[i].name + " : <" + poolList[i].url + ">";
try {
let poolStats = await snek.get(poolList[i].url + "/api/stats");
let statsJson = poolStats.body;
let poolHashStr = statsJson.pools[poolList[i].pool].hashrateString
poolText += " (" + poolHashStr + ") ";
poolText += "\n";
}
catch (e) {
poolText += " (Cannot retrieve data from pool API) \n";
console.log(e)
}
}
sendMessage(dm, message, poolText);
break;
case 'stats':
item = message.author.id + "_stats";
if (isCooldown(talkedRecently, item)) {
sendMessage(dm, message, waitText, true);
break;
}
setCooldown(talkedRecently, item);
let stats = await snek.get(statsUrl);
let statsJson = stats.body;
let networkStats = statsJson.pools.commercium.poolStats;
let statsText = "Difficulty: " + networkStats.networkDiff;
statsText += " \nNethash: " + networkStats.networkSolsString;
statsText += " \nBlock Height: " + networkStats.networkBlocks;
statsText += " \nExchange rate: (coming soon)";
sendMessage(dm, message, statsText);
break;
case 'cmm':
item = message.author.id + "_cmm";
if (isCooldown(talkedRecently, item)) {
sendMessage(dm, message, waitText, true);
break;
}
let hr = args[0];
if (isNaN(hr) || hr > 999999999 || hr <= 0) {
break;
}
setCooldown(talkedRecently, item);
const r1 = await snek.get('http://explorer.chattymining.com/api/getnetworkhashps');
let nethash = r1.body;
const r2 = await snek.get('https://explorer.commercium.net/api/status?q=getDifficulty');
let diff = r2.body.difficulty;
//coinPerDay = hr / nethash * 2880 * 32;
let coinPerDay = (32 / diff) * ((hr * 86400) / Math.pow(2, 32));
coinPerDay = Number(coinPerDay).toFixed(8);
let miningMessage = "```Estimated with " + hr + " sol/s you would get: " + coinPerDay + " CMM per day with current nethash: " + Number(nethash / 1000 / 1000).toFixed(2) + " MSol/s (Diff: " + Number(diff).toFixed(8) + ")```";
sendMessage(dm, message, miningMessage);
break;
}
}
});
function sendMessage(isDM, source, message, isMention) {
if (isDM) {
source.author.send(message);
}
else {
if (isMention) {
message = message + " " + source.author;
}
source.channel.send(message);
}
}
function isCooldown(waitingPool, item) {
return waitingPool.has(item);
}
function setCooldown(waitingPool, item) {
waitingPool.add(item);
setTimeout(() => {
waitingPool.delete(item);
}, 60000);
}