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

Add Settings Sync Code (Auto Sync) #1012

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/actions/GUILD_CREATE.ts
@@ -0,0 +1,31 @@
import { Action, extender, Guild } from '@klasa/core';

import type { GuildCreateDispatch } from '@klasa/ws';

export default class CoreAction extends Action {

public async run(data: GuildCreateDispatch): Promise<void> {
const built = this.build(data);
if (built) {
await built.settings.sync();
this.cache(built);
this.client.emit(this.clientEvent, built);
}
}

public check(): null {
return null;
}

public build(data: GuildCreateDispatch): Guild {
// eslint-disable-next-line camelcase
return new (extender.get('Guild'))(this.client, data.d, data.shard_id);
}

public cache(data: Guild): void {
if (this.client.options.cache.enabled) {
this.client.guilds.set(data.id, data);
}
}

}
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -5,6 +5,7 @@ export * from './lib/Client';
export * from './lib/extensions/KlasaGuild';
export * from './lib/extensions/KlasaMessage';
export * from './lib/extensions/KlasaUser';
export * from './lib/extensions/KlasaUserStore';

// lib/permissions
export * from './lib/permissions/PermissionLevels';
Expand Down
5 changes: 5 additions & 0 deletions src/lib/Client.ts
Expand Up @@ -17,6 +17,9 @@ import { isObject, mergeDefault } from '@klasa/utils';
import { join } from 'path';
import { PermissionLevels } from './permissions/PermissionLevels';

// lib/extensions
import { KlasaUserStore } from './extensions/KlasaUserStore';

// lib/schedule
import { Schedule } from './schedule/Schedule';

Expand Down Expand Up @@ -419,6 +422,8 @@ export class KlasaClient extends Client {
mergeDefault(KlasaClientDefaults as unknown as Required<KlasaClientOptions>, options);
super(options);

Reflect.set(this, 'users', new KlasaUserStore(this));

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.console = new KlasaConsole(this.options.console);
Expand Down
16 changes: 16 additions & 0 deletions src/lib/extensions/KlasaUserStore.ts
@@ -0,0 +1,16 @@
import { UserStore, User } from '@klasa/core';

export class KlasaUserStore extends UserStore {

/**
* Gets a {@link User user} by its ID, then syncs the user's settings instance
* @since 0.6.0
* @param userID The {@link User user} ID.
*/
public async fetch(userID: string): Promise<User> {
const user = await super.fetch(userID);
await user.settings.sync();
return user;
}

}