legacyMigrator: possible fix for last commit

master
Dragory 2018-02-19 00:49:44 +02:00
parent a76a675f4c
commit 8a1b8802b3
1 changed files with 36 additions and 30 deletions

View File

@ -69,44 +69,50 @@ async function shouldMigrate() {
async function migrateOpenThreads() { async function migrateOpenThreads() {
const bot = new Eris.Client(config.token); const bot = new Eris.Client(config.token);
await bot.connect();
const oldThreads = await jsonDb.get('threads', []); return new Promise(resolve => {
const promises = oldThreads.map(async oldThread => { bot.on('ready', async () => {
const existingOpenThread = await knex('threads') const oldThreads = await jsonDb.get('threads', []);
.where('channel_id', oldThread.channelId)
.first();
if (existingOpenThread) return; const promises = oldThreads.map(async oldThread => {
const existingOpenThread = await knex('threads')
.where('channel_id', oldThread.channelId)
.first();
const threadMessages = await bot.getChannel(oldThread.channelId).getMessages(1000); if (existingOpenThread) return;
const log = threadMessages.reverse().map(msg => {
const date = moment.utc(msg.timestamp, 'x').format('YYYY-MM-DD HH:mm:ss');
return `[${date}] ${msg.author.username}#${msg.author.discriminator}: ${msg.content}`;
}).join('\n') + '\n';
const newThread = { const threadMessages = await bot.getChannel(oldThread.channelId).getMessages(1000);
status: THREAD_STATUS.OPEN, const log = threadMessages.reverse().map(msg => {
user_id: oldThread.userId, const date = moment.utc(msg.timestamp, 'x').format('YYYY-MM-DD HH:mm:ss');
user_name: oldThread.username, return `[${date}] ${msg.author.username}#${msg.author.discriminator}: ${msg.content}`;
channel_id: oldThread.channelId, }).join('\n') + '\n';
is_legacy: 1
};
const threadId = await threads.createThreadInDB(newThread); const newThread = {
status: THREAD_STATUS.OPEN,
user_id: oldThread.userId,
user_name: oldThread.username,
channel_id: oldThread.channelId,
is_legacy: 1
};
await trx('thread_messages').insert({ const threadId = await threads.createThreadInDB(newThread);
thread_id: threadId,
message_type: THREAD_MESSAGE_TYPE.LEGACY, await trx('thread_messages').insert({
user_id: oldThread.userId, thread_id: threadId,
user_name: '', message_type: THREAD_MESSAGE_TYPE.LEGACY,
body: log, user_id: oldThread.userId,
is_anonymous: 0, user_name: '',
created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss') body: log,
is_anonymous: 0,
created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss')
});
});
resolve(Promise.all(promises));
}); });
});
return Promise.all(promises); bot.connect();
});
} }
async function migrateLogs() { async function migrateLogs() {