pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 1 | import fs from 'fs'; |
| 2 | import { MongoClient } from 'mongodb'; |
| 3 | |
| 4 | const mongoClient = new MongoClient('mongodb://127.0.0.1:27017/local'); |
| 5 | await mongoClient.connect() |
| 6 | const database = mongoClient.db("Nucleus"); |
| 7 | const collection = database.collection("migrationTesting"); |
| 8 | |
| 9 | // Loop through all files in the oldData folder |
| 10 | const files = fs.readdirSync('./oldData'); |
| 11 | let x = 0 |
| 12 | for (const file of files) { |
| 13 | console.log(`┌ Processing file ${x} of ${files.length - 1} | ${file}`); |
| 14 | // Read the file as a json |
| 15 | let data |
| 16 | try { |
| 17 | data = JSON.parse(fs.readFileSync(`./oldData/${file}`)); |
| 18 | } catch { |
| 19 | console.log(`└ Error reading file ${file}`); |
| 20 | x++ |
| 21 | continue; |
| 22 | } |
| 23 | // Check if data version is 3 |
| 24 | if (data.version !== 3) { |
| 25 | console.log(`├ Version was too old on ${file}`); |
| 26 | console.log(`└ Skipping file`); |
| 27 | x++; |
| 28 | continue |
| 29 | } |
| 30 | // Convert to the new format |
| 31 | const newData = { |
| 32 | "id": data.guild_info.id.toString(), |
| 33 | "version": 1, |
| 34 | "singleEventNotifications": { |
| 35 | "statsChannelDeleted": false |
| 36 | }, |
| 37 | "filters": { |
| 38 | "images": { |
| 39 | "NSFW": !data.images.nsfw, |
| 40 | "size": data.images.toosmall |
| 41 | }, |
| 42 | "wordFilter": { |
| 43 | "enabled": true, |
| 44 | "words": { |
| 45 | "strict": data.wordfilter.strict, |
| 46 | "loose": data.wordfilter.soft |
| 47 | }, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 48 | }, |
| 49 | "invite": { |
| 50 | "enabled": data.invite ? data.invite.enabled : false, |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 51 | "channels": data.invite ? data.invite.whitelist.channels.map(channel => channel.toString()) : [], |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 52 | }, |
| 53 | "pings": { |
| 54 | "mass": 5, |
| 55 | "everyone": true, |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 56 | "roles": true |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 57 | } |
| 58 | }, |
| 59 | "welcome": { |
| 60 | "enabled": data.welcome ? (data.welcome.message.text !== null) : false, |
| 61 | "verificationRequired": { |
| 62 | "message": null, |
| 63 | "role": null, |
| 64 | }, |
| 65 | "welcomeRole": data.welcome ? (data.welcome.role !== null ? data.welcome.role.toString() : null) : null, |
| 66 | "channel": data.welcome ? (data.welcome.message.text !== null ? data.welcome.message.channel.toString() : null) : null, |
| 67 | "message": data.welcome ? (data.welcome.message.text) : null |
| 68 | }, |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 69 | "stats": {}, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 70 | "logging": { |
| 71 | "logs": { |
| 72 | "enabled": true, |
| 73 | "channel": data.log_info.log_channel ? data.log_info.log_channel.toString() : null, |
| 74 | "toLog": "3fffff" |
| 75 | }, |
| 76 | "staff": { |
| 77 | "channel": data.log_info.staff ? data.log_info.staff.toString() : null, |
| 78 | } |
| 79 | }, |
| 80 | "verify": { |
| 81 | "enabled": data.verify_role !== null, |
| 82 | "role": data.verify_role ? data.verify_role.toString() : null, |
| 83 | }, |
| 84 | "tickets": { |
| 85 | "enabled": data.modmail ? (data.modmail.cat !== null) : null, |
| 86 | "category": data.modmail ? (data.modmail.cat !== null ? data.modmail.cat.toString() : null) : null, |
| 87 | "types": "3f", |
| 88 | "customTypes": null, |
| 89 | "supportRole": data.modmail ? (data.modmail.mention !== null ? data.modmail.mention.toString() : null) : null, |
| 90 | "maxTickets": data.modmail ? (data.modmail.max) : 5 |
| 91 | }, |
| 92 | "moderation": { |
| 93 | "mute": { |
| 94 | "timeout": true, |
| 95 | "role": null, |
| 96 | "text": null, |
| 97 | "link": null |
| 98 | }, |
| 99 | "kick": { |
| 100 | "text": null, |
| 101 | "link": null |
| 102 | }, |
| 103 | "ban": { |
| 104 | "text": null, |
| 105 | "link": null |
| 106 | }, |
| 107 | "softban": { |
| 108 | "text": null, |
| 109 | "link": null |
| 110 | }, |
| 111 | "warn": { |
| 112 | "text": null, |
| 113 | "link": null |
| 114 | }, |
| 115 | "role": { |
| 116 | "role": null |
| 117 | } |
| 118 | }, |
| 119 | "tracks": [], |
| 120 | "roleMenu": [], |
| 121 | "tags": data.tags |
| 122 | } |
| 123 | // Insert the new data into the database |
| 124 | await collection.updateOne({ id: data.guild_info.id.toString() }, { $set: newData }, { upsert: true }); |
| 125 | // Delete the old file |
| 126 | fs.unlinkSync(`./oldData/${file}`); |
| 127 | console.log(`└ Successfully migrated file ${file}`); |
| 128 | x++; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | // console.log((await collection.findOne({ id: "your mother" }))); |