From ea5fdfda5e5c81c8871e50082b15d5490857cef8 Mon Sep 17 00:00:00 2001 From: Julian Date: Fri, 12 Mar 2021 17:09:48 +0100 Subject: [PATCH] v0.1.1 --- package.json | 2 +- update/0.1.1.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 update/0.1.1.js diff --git a/package.json b/package.json index 07f010ccd..4248ff963 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "modbot", - "version": "0.1.0", + "version": "0.1.1", "description": "Discord Bot for the Aternos Discord server", "main": "index.js", "scripts": { diff --git a/update/0.1.1.js b/update/0.1.1.js new file mode 100644 index 000000000..2829b8874 --- /dev/null +++ b/update/0.1.1.js @@ -0,0 +1,67 @@ +const Database = require('../src/Database'); +const config = require('../config.json'); +const Discord = require('discord.js'); +const database = new Database(config.db); +const client = new Discord.Client(); + +async function update() { + console.log('Starting update to v0.1.1'); + + console.log('Updating tables...'); + await database.query('ALTER TABLE `channels` ADD COLUMN IF NOT EXISTS `guildid` VARCHAR(20)'); + console.log('Done!') + console.log('Updating entries...') + const channelIDs = await database.queryAll('SELECT id FROM channels WHERE guildid IS null'); + if (channelIDs.length === 0) { + console.log('No entries to update!'); + process.exit(0); + } + + console.log('Logging into Discord....'); + await client.login(config.auth_token); + console.log('Done!'); + + let updates = []; + for (const channelID of channelIDs) { + updates.push(updateGuildID(channelID.id)); + } + updates = await Promise.all(updates); + + const removed = updates.reduce(((previousValue, currentValue) => { + if (currentValue) + return previousValue; + else + return previousValue + 1; + }), 0); + + console.log(`Updated ${updates.length} database entries, removed ${removed === true ? 0 : removed} faulty entries!`); + process.exit(0); +} + +update().catch(e => { + console.error(e); + process.exit(1); +}); + +/** + * @param {Snowflake} channelID + * @return {Promise} + */ +async function updateGuildID(channelID) { + try { + /** @type {module:"discord.js".GuildChannel} */ + const channel = await client.channels.fetch(/** @type {Snowflake} */ channelID); + await database.query('UPDATE channels SET guildid = ? WHERE id = ?',[channel.guild.id, channelID]); + } + catch (e) { + if (e.code === 10003) { + await database.query('DELETE FROM channels WHERE id = ?',[channelID]); + return false; + } + if (e.code === 50001) { + return false; + } + throw e; + } + return true; +}