-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbLoader.js
63 lines (49 loc) · 1.33 KB
/
dbLoader.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
/**
* Call this file like:
* `node dbLoader.js ./test.config.json ./yag-export.json`
* Where the first argument is the config file for your Leo,
* and the second is data exported from YAGPDB.xyz
*/
import config from "./config.cjs";
import * as Database from "./database.js";
import seq from 'sequelize';
import fs from "fs/promises";
import { User } from "discord.js";
const { Sequelize } = seq;
const sequelize = new Sequelize({
dialect: "sqlite",
storage: config.database
});
async function main() {
Database.Reputation.init(sequelize);
Database.User.init(sequelize);
await Database.Reputation.sync({ alter: true });
await Database.User.sync({ alter: true });
const data = JSON.parse(await fs.readFile(process.argv[3]));
await insertData(data);
}
async function insertData(data) {
const toInsert = data.map(d => ({
user: d.receiver_id,
delta: d.amount,
reason: "Imported from YAGPDB.xyz",
giverId: d.sender_id,
createdAt: d.created_at
}));
const users = data
.filter(d => d.receiver_username)
.map(d => {
const [tag, name, disc] = d.receiver_username.match(/(.+)#([0-9]+)/);
return {
id: d.receiver_id,
name: name,
discriminator: disc
}
});
await Database.Reputation.bulkCreate(toInsert);
for (let user of users) {
await Database.User.upsert(user);
}
console.log(users);
}
main();