Skip to content

Commit 5c95c32

Browse files
authored
fix: update @sapphire/pieces to v2.0.0 (#198)
BREAKING CHANGE: Updated `@sapphire/pieces` to 2.0.0 BREAKING CHANGE: Renamed `PieceContextExtras` to `Container`, usage and augmentation is the same. BREAKING CHANGE: Removed `Store.injectedContext`, use globally exported `container` variable instead. BREAKING CHANGE: Renamed `Store#context` to `Store#container`. BREAKING CHANGE: Renamed `Piece#context` to `Piece#container`.
1 parent 4ca0365 commit 5c95c32

18 files changed

+31
-30
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"dependencies": {
3232
"@sapphire/discord-utilities": "^2.1.1",
3333
"@sapphire/discord.js-utilities": "^1.5.7",
34-
"@sapphire/pieces": "^1.2.5",
34+
"@sapphire/pieces": "^2.0.0",
3535
"@sapphire/ratelimits": "^1.2.1",
3636
"@sapphire/utilities": "^1.5.1",
3737
"lexure": "^0.17.0",

src/arguments/CoreChannel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class CoreArgument extends Argument<Channel> {
88
}
99

1010
public run(parameter: string, context: ArgumentContext): ArgumentResult<Channel> {
11-
const channel = (context.message.guild ? context.message.guild.channels : this.context.client.channels).cache.get(parameter);
11+
const channel = (context.message.guild ? context.message.guild.channels : this.container.client.channels).cache.get(parameter);
1212
return channel
1313
? this.ok(channel)
1414
: this.error({

src/arguments/CoreMessage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class CoreArgument extends Argument<Message> {
3535
if (!matches) return null;
3636
const [, guildID, channelID, messageID] = matches;
3737

38-
const guild = this.context.client.guilds.cache.get(guildID);
38+
const guild = this.container.client.guilds.cache.get(guildID);
3939
if (guild !== message.guild) return null;
4040

4141
const channel = guild.channels.cache.get(channelID);

src/arguments/CoreUser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class CoreArgument extends Argument<User> {
1010

1111
public async run(parameter: string, context: ArgumentContext): AsyncArgumentResult<User> {
1212
const userID = UserOrMemberMentionRegex.exec(parameter) ?? SnowflakeRegex.exec(parameter);
13-
const user = userID ? await this.context.client.users.fetch(userID[1]).catch(() => null) : null;
13+
const user = userID ? await this.container.client.users.fetch(userID[1]).catch(() => null) : null;
1414
return user ? this.ok(user) : this.error({ parameter, message: 'The argument did not resolve to a user.', context });
1515
}
1616
}

src/errorEvents/CoreCommandError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export class CoreEvent extends Event<Events.CommandError> {
99

1010
public run(error: Error, context: CommandErrorPayload) {
1111
const { name, path } = context.piece;
12-
this.context.logger.error(`Encountered error on command "${name}" at path "${path}"`, error);
12+
this.container.logger.error(`Encountered error on command "${name}" at path "${path}"`, error);
1313
}
1414
}

src/errorEvents/CoreEventError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export class CoreEvent extends Event<Events.EventError> {
99

1010
public run(error: Error, context: EventErrorPayload) {
1111
const { name, event, path } = context.piece;
12-
this.context.logger.error(`Encountered error on event listener "${name}" for event "${event}" at path "${path}"`, error);
12+
this.container.logger.error(`Encountered error on event listener "${name}" for event "${event}" at path "${path}"`, error);
1313
}
1414
}

src/events/CoreReady.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export class CoreEvent extends Event<Events.Ready> {
88
}
99

1010
public run() {
11-
this.context.client.id ??= this.context.client.user?.id ?? null;
11+
this.container.client.id ??= this.container.client.user?.id ?? null;
1212
}
1313
}

src/events/command-handler/CoreMessage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ export class CoreEvent extends Event<Events.Message> {
1313
if (message.author.bot || message.webhookID) return;
1414

1515
// Run the message parser.
16-
this.context.client.emit(Events.PreMessageParsed, message);
16+
this.container.client.emit(Events.PreMessageParsed, message);
1717
}
1818
}

src/events/command-handler/CoreMessageParser.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class CoreEvent extends Event<Events.PreMessageParsed> {
1616

1717
let prefix = null;
1818
const mentionPrefix = this.getMentionPrefix(message.content);
19-
const { client } = this.context;
19+
const { client } = this.container;
2020
const { regexPrefix } = client.options;
2121
if (mentionPrefix) {
2222
if (message.content.length === mentionPrefix.length) {
@@ -47,7 +47,7 @@ export class CoreEvent extends Event<Events.PreMessageParsed> {
4747
}
4848

4949
private getMentionPrefix(content: string): string | null {
50-
const { id } = this.context.client;
50+
const { id } = this.container.client;
5151

5252
// If no client ID was specified, return null:
5353
if (!id) return null;
@@ -72,7 +72,7 @@ export class CoreEvent extends Event<Events.PreMessageParsed> {
7272

7373
private getPrefix(content: string, prefixes: readonly string[] | string | null): string | null {
7474
if (prefixes === null) return null;
75-
const { caseInsensitivePrefixes } = this.context.client.options;
75+
const { caseInsensitivePrefixes } = this.container.client.options;
7676

7777
if (caseInsensitivePrefixes) content = content.toLowerCase();
7878

src/events/command-handler/CorePreCommandRun.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class CoreEvent extends Event<Events.PreCommandRun> {
1111
const { message, command } = payload;
1212

1313
// Run global preconditions:
14-
const globalResult = await this.context.stores.get('preconditions').run(message, command, payload as any);
14+
const globalResult = await this.container.stores.get('preconditions').run(message, command, payload as any);
1515
if (!globalResult.success) {
1616
message.client.emit(Events.CommandDenied, globalResult.error, payload);
1717
return;

src/events/command-handler/CorePrefixedMessage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class CoreEvent extends Event<Events.PrefixedMessage> {
99
}
1010

1111
public run(message: Message, prefix: string | RegExp) {
12-
const { client, stores } = this.context;
12+
const { client, stores } = this.container;
1313
// Retrieve the command name and validate:
1414
const commandPrefix = this.getCommandPrefix(message.content, prefix);
1515
const prefixLess = message.content.slice(commandPrefix.length).trim();

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export {
33
AliasPieceOptions,
44
AliasStore,
55
Awaited,
6+
container,
67
LoaderError,
78
MissingExportsError,
89
Piece,

src/lib/SapphireClient.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Awaited, Store } from '@sapphire/pieces';
1+
import { Awaited, container } from '@sapphire/pieces';
22
import { Client, ClientOptions, Message } from 'discord.js';
33
import { join } from 'path';
44
import type { Plugin } from './plugins/Plugin';
@@ -200,18 +200,18 @@ export class SapphireClient extends Client {
200200
public constructor(options: ClientOptions = {}) {
201201
super(options);
202202

203-
Store.injectedContext.client = this;
203+
container.client = this;
204204

205205
for (const plugin of SapphireClient.plugins.values(PluginHook.PreGenericsInitialization)) {
206206
plugin.hook.call(this, options);
207207
this.emit(Events.PluginLoaded, plugin.type, plugin.name);
208208
}
209209

210210
this.logger = options.logger?.instance ?? new Logger(options.logger?.level ?? LogLevel.Info);
211-
Store.injectedContext.logger = this.logger;
211+
container.logger = this.logger;
212212

213213
this.stores = new StoreRegistry();
214-
Store.injectedContext.stores = this.stores;
214+
container.stores = this.stores;
215215

216216
this.fetchPrefix = options.fetchPrefix ?? (() => this.options.defaultPrefix ?? null);
217217

@@ -290,7 +290,7 @@ declare module 'discord.js' {
290290
}
291291

292292
declare module '@sapphire/pieces' {
293-
interface PieceContextExtras {
293+
interface Container {
294294
client: SapphireClient;
295295
logger: ILogger;
296296
stores: StoreRegistry;

src/lib/parsers/Args.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Store } from '@sapphire/pieces';
1+
import { container } from '@sapphire/pieces';
22
import type {
33
CategoryChannel,
44
Channel,
@@ -655,7 +655,7 @@ export class Args {
655655
*/
656656
private resolveArgument<T>(arg: keyof ArgType | IArgument<T>): IArgument<T> | undefined {
657657
if (typeof arg === 'object') return arg;
658-
return Store.injectedContext.stores.get('arguments').get(arg as string) as IArgument<T> | undefined;
658+
return container.stores.get('arguments').get(arg as string) as IArgument<T> | undefined;
659659
}
660660

661661
/**

src/lib/structures/Event.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export abstract class Event<E extends keyof ClientEvents | symbol = ''> extends
5555

5656
this.emitter =
5757
typeof options.emitter === 'undefined'
58-
? this.context.client
59-
: (typeof options.emitter === 'string' ? (Reflect.get(this.context.client, options.emitter) as EventEmitter) : options.emitter) ??
58+
? this.container.client
59+
: (typeof options.emitter === 'string' ? (Reflect.get(this.container.client, options.emitter) as EventEmitter) : options.emitter) ??
6060
null;
6161
this.event = options.event ?? this.name;
6262
this.once = options.once ?? false;
@@ -86,7 +86,7 @@ export abstract class Event<E extends keyof ClientEvents | symbol = ''> extends
8686
// @ts-expect-error Argument of type 'unknown[]' is not assignable to parameter of type 'E extends string | number ? ClientEvents[E] : unknown[]'. (2345)
8787
await this.run(...args);
8888
} catch (error) {
89-
this.context.client.emit(Events.EventError, error, { piece: this });
89+
this.container.client.emit(Events.EventError, error, { piece: this });
9090
}
9191
}
9292

src/lib/structures/ExtendedArgument.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export abstract class ExtendedArgument<K extends keyof ArgType, T> extends Argum
6262
* into the value used to compute the extended argument's value.
6363
*/
6464
public get base(): IArgument<ArgType[K]> {
65-
return this.context.stores.get('arguments').get(this.baseArgument) as IArgument<ArgType[K]>;
65+
return this.container.stores.get('arguments').get(this.baseArgument) as IArgument<ArgType[K]>;
6666
}
6767

6868
public async run(parameter: string, context: ArgumentContext<T>): AsyncArgumentResult<T> {

src/lib/utils/preconditions/PreconditionContainerSingle.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Store } from '@sapphire/pieces';
1+
import { container } from '@sapphire/pieces';
22
import type { Message } from 'discord.js';
33
import type { Command } from '../../structures/Command';
44
import type { PreconditionContext } from '../../structures/Precondition';
@@ -65,7 +65,7 @@ export class PreconditionContainerSingle implements IPreconditionContainer {
6565
* @param command The command the message invoked.
6666
*/
6767
public run(message: Message, command: Command, context: PreconditionContext = {}) {
68-
const precondition = Store.injectedContext.stores.get('preconditions').get(this.name);
68+
const precondition = container.stores.get('preconditions').get(this.name);
6969
if (precondition) return precondition.run(message, command, { ...context, ...this.context });
7070
throw new Error(`The precondition "${this.name}" is not available.`);
7171
}

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,10 @@
784784
prettier "^2.2.1"
785785
typescript "^4.2.4"
786786

787-
"@sapphire/pieces@^1.2.5":
788-
version "1.2.5"
789-
resolved "https://registry.yarnpkg.com/@sapphire/pieces/-/pieces-1.2.5.tgz#fcde0e596d480077c90aa07e754e0b9d20f72155"
790-
integrity sha512-2/kr7fiRgqL5CBcyBxBgM5p0L2SlUtkX/V6KRe20uwdQH1ZUf//GwjYj/KGLb/ymzkTXyhFrSrLmqomSQ+G/1g==
787+
"@sapphire/pieces@^2.0.0":
788+
version "2.0.0"
789+
resolved "https://registry.yarnpkg.com/@sapphire/pieces/-/pieces-2.0.0.tgz#cab97f89dd1f60b5a10f5849a098c4911e2b9023"
790+
integrity sha512-UjVwkCIOMRLwVzLNudrSSCsj1OeRX7cVR2XK/HE6NhsjSIP7ppATZaJ77fSUwqwZ4IgLCEXh96jNIhUyDX/COg==
791791
dependencies:
792792
"@discordjs/collection" "^0.1.6"
793793
tslib "^2.2.0"

0 commit comments

Comments
 (0)