Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

Commit

Permalink
fix: Settings delete core event (#477)
Browse files Browse the repository at this point in the history
* fix: Added core event handler for settingsDelete

* docs: Updated IncludedEvents

* docs: Correct changelog PR number

* fix: Don't check this.client.shard in both core settings event handlers

* misc: Add IS support for both core settings events handlers

* misc: Much better and faster shard check for both settings events
  • Loading branch information
kyranet authored and bdistin committed Nov 7, 2018
1 parent 8256f57 commit 5fbb249
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ NOTE: For the contributors, you add new entries to this document following this

### Added

- [[#477](https://github.com/dirigeants/klasa/pull/477)] Added core `KlasaClient#settingsDelete` event handler to destroy instances in other shards. (kyranet)
- [[#475](https://github.com/dirigeants/klasa/pull/475)] Added `KlasaClient#settingsSync` event. (kyranet)
- [[#471](https://github.com/dirigeants/klasa/pull/471)] Added `Gateway#create` and `Gateway#acquire`. (kyranet)
- [[#471](https://github.com/dirigeants/klasa/pull/471)] Added `SETTING_GATEWAY_CHOOSE_KEY` and `SETTING_GATEWAY_UNCONFIGURABLE_FOLDER` i18n keys into en-US. (kyranet)
Expand Down
10 changes: 9 additions & 1 deletion guides/Included Pieces/IncludedEvents.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ Replies with the reason why the command was inhibited.

[events/commandInhibited.js](https://github.com/dirigeants/klasa/blob/master/src/events/commandInhibited.js)

## settingsDelete

Deletes settings in all shards, if the bot is sharded.

**Source:**

[events/coreSettingsDelete.js](https://github.com/dirigeants/klasa/blob/master/src/events/coreSettingsDelete.js)

## settingsUpdate

Synchronises the user settings between all shards, if the bot is sharded.
Synchronises settings between all shards, if the bot is sharded.

**Source:**

Expand Down
28 changes: 28 additions & 0 deletions src/events/coreSettingsDelete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { Event } = require('klasa');
const gateways = ['users', 'clientStorage'];

module.exports = class extends Event {

constructor(...args) {
super(...args, { event: 'settingsDelete' });
}

run(settings) {
if (gateways.includes(settings.gateway.name)) {
this.client.shard.broadcastEval(`
if (String(this.shard.id) === '${this.client.shard.id}') return;
const entry = this.gateways.get('${settings.gateway.name}').get('${settings.id}');
if (entry && entry.existenceStatus) {
this.emit('settingsDelete', settings);
entry.init(entry, entry.schema);
entry.existenceStatus = false;
}
`);
}
}

init() {
if (!this.client.shard) this.disable();
}

};
15 changes: 7 additions & 8 deletions src/events/coreSettingsUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ module.exports = class extends Event {
}

run(settings) {
if (this.client.shard && gateways.includes(settings.gateway.name)) {
if (gateways.includes(settings.gateway.name)) {
this.client.shard.broadcastEval(`
if (this.shard.id !== ${this.client.shard.id}) {
const entry = this.gateways.get('${settings.gateway.name}').get('${settings.id}');
if (entry) {
entry._patch(${JSON.stringify(settings)});
entry.existenceStatus = true;
this.emit('settingsSync', settings);
}
if (String(this.shard.id) === '${this.client.shard.id}') return;
const entry = this.gateways.get('${settings.gateway.name}').get('${settings.id}');
if (entry) {
entry._patch(${JSON.stringify(settings)});
entry.existenceStatus = true;
this.emit('settingsSync', settings);
}
`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/settings/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Settings extends SettingsFolder {
if (this.existenceStatus) {
await this.gateway.provider.delete(this.gateway.name, this.id);
this.gateway.client.emit('settingsDelete', this);
this.init();
this.init(this, this.schema);
}
return this;
}
Expand Down

0 comments on commit 5fbb249

Please sign in to comment.