-
Notifications
You must be signed in to change notification settings - Fork 7
/
migrate.js
81 lines (65 loc) · 2.57 KB
/
migrate.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
import { rmSync, copyFileSync, renameSync } from 'fs';
import sqlite3 from 'sqlite3';
import * as sqlite from 'sqlite';
rmSync('settings_new.sqlite3', { force: true });
const dbOld = await sqlite.open({
filename: new URL('settings.sqlite3', import.meta.url).pathname,
driver: sqlite3.Database
});
const dbNew = await sqlite.open({
filename: new URL('settings_new.sqlite3', import.meta.url).pathname,
driver: sqlite3.Database
});
await dbNew.run('CREATE TABLE settings (guild INTEGER PRIMARY KEY, settings TEXT)');
const oldSettings = await dbOld.all('SELECT CAST(guild as TEXT) as guild, settings FROM settings');
await Promise.all(oldSettings.map(async s => {
const newSettings = await migrate(s.guild, JSON.parse(s.settings));
await dbNew.run('INSERT INTO settings (guild, settings) VALUES(?, ?)', s.guild, JSON.stringify(newSettings));
}));
copyFileSync('settings.sqlite3', 'settings_old.sqlite3');
rmSync('settings.sqlite3');
renameSync('settings_new.sqlite3', 'settings.sqlite3');
async function migrate(guild, oldSettings) {
const newSettings = {};
if (oldSettings.prefix) {
newSettings.prefix = oldSettings.prefix;
}
if (oldSettings.mutedRole) {
newSettings.mutedRole = oldSettings.mutedRole;
}
if (oldSettings.guildMemberAdd?.message) {
const { content, ...embed } = JSON.parse(oldSettings.guildMemberAdd.message);
newSettings.welcome = {
content,
embed,
channel: oldSettings.guildMemberAdd.channel,
automute: oldSettings.guildMemberAdd.automute ?? false,
};
}
if (oldSettings.guildMemberRemove?.message) {
const { content, ...embed } = JSON.parse(oldSettings.guildMemberRemove.message);
newSettings.welcome = {
content,
embed,
channel: oldSettings.guildMemberRemove.channel,
};
}
newSettings.log = {
guildMemberAdd: oldSettings?.guildMemberAdd?.log ?? null,
guildMemberRemove: oldSettings?.guildMemberRemove?.log ?? null,
guildMemberMuted: oldSettings?.guildMemberMuted?.log ?? null,
guildMemberUnmuted: oldSettings?.guildMemberUnmuted?.log ?? null,
guildMemberUpdate: oldSettings?.guildMemberUpdate?.log ?? null,
modAction: oldSettings?.guildMemberMuted?.log ?? null,
messageUpdate: oldSettings?.messageUpdate?.log ?? null,
messageDelete: oldSettings?.messageDelete?.log ?? null,
userUpdate: oldSettings?.userUpdate?.log ?? null,
};
if (oldSettings.suggest) {
newSettings.suggest = oldSettings.suggest;
}
if (oldSettings.complain) {
newSettings.complain = oldSettings.complain;
}
return newSettings;
}