Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Mar 12, 2021
1 parent e93ad31 commit ea5fdfd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
67 changes: 67 additions & 0 deletions update/0.1.1.js
Original file line number Diff line number Diff line change
@@ -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<Boolean>}
*/
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;
}

0 comments on commit ea5fdfd

Please sign in to comment.