-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhelper.js
322 lines (289 loc) · 10.9 KB
/
helper.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
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
const config = require('./config');
const javalon = require('javalon');
const steem = require('steem')
const hive = require('@hiveio/hive-js')
steem.api.setOptions({ url: 'https://api.steemit.com/', useAppbaseApi: true});
hive.api.setOptions({ url: 'https://api.openhive.network/', useAppbaseApi: true});
console.log(steem.api.options.url)
console.log(hive.api.options.url)
let database = require('mysql').createConnection(config.database);
database.connect((err) => {
if (err) throw err;
console.log("Database connection etablished!");
database.query("set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';", () => {
console.log("Initialized Database")
})
});
database.addMessage = async (id, author, permlink) => {
return new Promise((resolve, reject) => {
let sql = "INSERT INTO message (discord_id, author, permlink, posted) VALUES (?,?,?,?)";
database.query(sql, [id, author, permlink, (new Date()).toISOString().slice(0, 19).replace('T', ' ')], (err, result) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(true);
}
})
})
};
database.getMessageSummary = async(days) => {
return new Promise((resolve, reject) => {
let sql = "select Count(id) as count, posted from message m WHERE m.posted > CURDATE() - INTERVAL ? DAY GROUP BY Day(m.posted);";
database.query(sql,[days], (err, result) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(result.reverse());
}
})
})
};
database.getMessagesToVote = async () => {
return new Promise((resolve, reject) => {
let sql = "SELECT * FROM message WHERE voted = 0";
database.query(sql, (err, result) => {
if (err) {
reject(err);
console.log(err);
} else {
resolve(result);
}
})
})
};
database.getMessage = async (author, permlink) => {
return new Promise((resolve, reject) => {
let sql = "SELECT * FROM message where author = ? and permlink = ?";
database.query(sql, [author, permlink], (err, result) => {
if (err) {
reject(err);
console.log(err);
} else {
if (result.length != 1)
resolve(null);
else
resolve(result[0]);
}
})
})
};
database.getMessages = async () => {
return new Promise((resolve, reject) => {
let sql = "SELECT * FROM message";
database.query(sql, (err, result) => {
if (err) {
reject(err);
console.log(err);
} else {
resolve(result);
}
})
})
};
database.countMessages = async () => {
return new Promise((resolve, reject) => {
let sql = "SELECT Count(id) as count FROM message";
database.query(sql, (err, result) => {
if (err) {
reject(err);
console.log(err);
} else {
resolve(result);
}
})
})
};
database.countCurators = async () => {
return new Promise((resolve, reject) => {
let sql = "SELECT Count(DISTINCT(discord_id)) as count FROM message";
database.query(sql, (err, result) => {
if (err) {
reject(err);
console.log(err);
} else {
resolve(result);
}
})
})
};
database.existMessage = async (author, permlink) => {
let message = await database.getMessage(author, permlink);
return new Promise((resolve, reject) => {
if (!message) resolve(false);
else resolve(true)
})
};
database.updateReactions = async (id, reactions) => {
return new Promise((resolve, reject) => {
let sql = "UPDATE message SET ? WHERE voted = 0 and discord_id = " + id;
database.query(sql, reactions, (err, result) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(true);
}
})
})
};
database.feedBackExist = async (author, permlink) => {
return new Promise((resolve, reject) => {
let sql = "SELECT * from feedback where author = ? and permlink = ?";
database.query(sql, [author, permlink], (err, result) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(result);
}
})
});
};
database.addFeedback = async (from, msg, author, permlink) => {
let sql = "INSERT INTO feedback (discord,message,author,permlink) VALUES (?,?,?,?)";
return new Promise((resolve, reject) => {
database.query(sql, [from, msg, author, permlink], (err, result) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(true);
}
})
});
};
function calculateVote(post) {
console.log(post)
if (post.one_hundred >= 3)
return 10000;
let weight = 0;
// add up all the weights
for (let i = 0; i < post.game_die; i++)
weight += 100 * (1 + Math.floor(Math.random() * 6));
for (let i = 0; i < post.heart; i++)
weight += 1500;
for (let i = 0; i < post.up; i++)
weight += 500;
for (let i = 0; i < post.unicorn; i++)
weight += 2500;
for (let i = 0; i < post.down; i++)
weight -= 500;
// if there is a disagrement, no vote
if (weight > 0 && post.down > 0)
return 0;
if (weight > 10000)
weight = 10000;
return weight
}
function countReaction(message) {
let reactions = {};
for (const key in config.discord.curation.curation_emojis)
reactions[key] =
message.reactions.get(config.discord.curation.curation_emojis[key]) ?
message.reactions.get(config.discord.curation.curation_emojis[key]).count
: 0;
return reactions;
}
function generatePermlink() {
let permlink = ""
let possible = "abcdefghijklmnopqrstuvwxyz0123456789"
for (let i = 0; i < 8; i++) {
permlink += possible.charAt(Math.floor(Math.random() * possible.length))
}
return permlink
}
module.exports = {
DTubeLink: (str) => {
let words = str.split(' ');
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (word.startsWith('https://d.tube'))
return word
}
},
calculateVote,
countReaction,
generatePermlink,
getMinutesSincePost: (posted) => {
let diff = (new Date()).getTime() - posted.getTime();
return (diff / 60000);
},
vote: async (message, client) => {
return new Promise((resolve, reject) => {
client
.guilds
.get(config.discord.curation.guild)
.channels
.get(config.discord.curation.channel)
.fetchMessage(message.discord_id).then(post => {
database.updateReactions(post.id, countReaction(post)).then(async () => {
let weight = calculateVote(message);
if (weight === 0) {
reject('Weight=0')
} else {
console.log('voting', message.author + '/' + message.permlink, weight);
// voting on avalon
var newTx = {
type: javalon.TransactionType.VOTE,
data: {
author: message.author,
link: message.permlink,
vt: weight*config.avalon.vtMultiplier,
tag: ''
}
}
newTx = javalon.sign(config.avalon.wif, config.avalon.account, newTx)
javalon.sendRawTransaction(newTx, function(err, res) {
if (!err) {
let sql = "UPDATE message SET voted = 1, vote_weight = ? WHERE author = ? and permlink = ?";
database.query(sql, [weight, message.author, message.permlink], (err, result) => {
console.log("Voted with " + (weight / 100) + "% for @" + message.author + '/' + message.permlink);
resolve({});
})
}
})
javalon.getContent(message.author, message.permlink, function(err, res) {
if (res.json && res.json.refs) {
for (let i = 0; i < res.json.refs.length; i++) {
var ref = res.json.refs[i].split('/')
if (ref[0] == 'steem') {
// voting on steem !
steem.broadcast.vote(
config.steem.wif,
config.steem.account, // Voter
ref[1], // Author
ref[2], // Permlink
weight,
(err, result_bc) => {
if (err) {
reject(err);
}
}
);
hive.broadcast.vote(
config.steem.wif,
config.steem.account, // Voter
ref[1], // Author
ref[2], // Permlink
weight,
(err, result_bc) => {
if (err) {
reject(err);
}
}
);
// making sure its only spending one vote :)
break;
}
}
}
})
}
})
});
})
},
database
};