2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../database';
|
2019-06-10 18:36:56 +00:00
|
|
|
import log from '../../utils/log';
|
2019-09-16 20:26:32 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
export default function() {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2019-06-17 13:57:07 +00:00
|
|
|
return new Promise(async(resolve) => {
|
|
|
|
try {
|
|
|
|
// RC 0.60.2
|
|
|
|
const result = await this.sdk.get('commands.list');
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (!result.success) {
|
2019-08-30 12:43:23 +00:00
|
|
|
console.log(result);
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
const { commands } = result;
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (commands && commands.length) {
|
2021-02-23 18:36:20 +00:00
|
|
|
await db.action(async() => {
|
2021-02-26 16:25:51 +00:00
|
|
|
const slashCommandsCollection = db.get('slash_commands');
|
2021-02-23 18:36:20 +00:00
|
|
|
const allSlashCommandsRecords = await slashCommandsCollection.query().fetch();
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
// filter slash commands
|
|
|
|
let slashCommandsToCreate = commands.filter(i1 => !allSlashCommandsRecords.find(i2 => i1.command === i2.id));
|
|
|
|
let slashCommandsToUpdate = allSlashCommandsRecords.filter(i1 => commands.find(i2 => i1.id === i2.command));
|
|
|
|
let slashCommandsToDelete = allSlashCommandsRecords
|
|
|
|
.filter(i1 => !slashCommandsToCreate.find(i2 => i2.command === i1.id) && !slashCommandsToUpdate.find(i2 => i2.id === i1.id));
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
// Create
|
|
|
|
slashCommandsToCreate = slashCommandsToCreate.map(command => slashCommandsCollection.prepareCreate(protectedFunction((s) => {
|
|
|
|
s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema);
|
|
|
|
Object.assign(s, command);
|
|
|
|
})));
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
// Update
|
|
|
|
slashCommandsToUpdate = slashCommandsToUpdate.map((command) => {
|
|
|
|
const newCommand = commands.find(s => s.command === command.id);
|
|
|
|
return command.prepareUpdate(protectedFunction((s) => {
|
|
|
|
Object.assign(s, newCommand);
|
|
|
|
}));
|
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
// Delete
|
|
|
|
slashCommandsToDelete = slashCommandsToDelete.map(command => command.prepareDestroyPermanently());
|
2020-02-14 13:24:35 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
const allRecords = [
|
|
|
|
...slashCommandsToCreate,
|
|
|
|
...slashCommandsToUpdate,
|
|
|
|
...slashCommandsToDelete
|
|
|
|
];
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
try {
|
|
|
|
await db.batch(...allRecords);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return allRecords.length;
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|