Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update @sapphire/pieces to v2.0.0 #198

Merged
merged 2 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"@sapphire/discord-utilities": "^2.1.1",
"@sapphire/discord.js-utilities": "^1.5.7",
"@sapphire/pieces": "^1.2.5",
"@sapphire/pieces": "^2.0.0",
"@sapphire/ratelimits": "^1.2.1",
"@sapphire/utilities": "^1.5.1",
"lexure": "^0.17.0",
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/CoreChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CoreArgument extends Argument<Channel> {
}

public run(parameter: string, context: ArgumentContext): ArgumentResult<Channel> {
const channel = (context.message.guild ? context.message.guild.channels : this.context.client.channels).cache.get(parameter);
const channel = (context.message.guild ? context.message.guild.channels : this.container.client.channels).cache.get(parameter);
return channel
? this.ok(channel)
: this.error({
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/CoreMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CoreArgument extends Argument<Message> {
if (!matches) return null;
const [, guildID, channelID, messageID] = matches;

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

const channel = guild.channels.cache.get(channelID);
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/CoreUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class CoreArgument extends Argument<User> {

public async run(parameter: string, context: ArgumentContext): AsyncArgumentResult<User> {
const userID = UserOrMemberMentionRegex.exec(parameter) ?? SnowflakeRegex.exec(parameter);
const user = userID ? await this.context.client.users.fetch(userID[1]).catch(() => null) : null;
const user = userID ? await this.container.client.users.fetch(userID[1]).catch(() => null) : null;
return user ? this.ok(user) : this.error({ parameter, message: 'The argument did not resolve to a user.', context });
}
}
2 changes: 1 addition & 1 deletion src/errorEvents/CoreCommandError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export class CoreEvent extends Event<Events.CommandError> {

public run(error: Error, context: CommandErrorPayload) {
const { name, path } = context.piece;
this.context.logger.error(`Encountered error on command "${name}" at path "${path}"`, error);
this.container.logger.error(`Encountered error on command "${name}" at path "${path}"`, error);
}
}
2 changes: 1 addition & 1 deletion src/errorEvents/CoreEventError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export class CoreEvent extends Event<Events.EventError> {

public run(error: Error, context: EventErrorPayload) {
const { name, event, path } = context.piece;
this.context.logger.error(`Encountered error on event listener "${name}" for event "${event}" at path "${path}"`, error);
this.container.logger.error(`Encountered error on event listener "${name}" for event "${event}" at path "${path}"`, error);
}
}
2 changes: 1 addition & 1 deletion src/events/CoreReady.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export class CoreEvent extends Event<Events.Ready> {
}

public run() {
this.context.client.id ??= this.context.client.user?.id ?? null;
this.container.client.id ??= this.container.client.user?.id ?? null;
}
}
2 changes: 1 addition & 1 deletion src/events/command-handler/CoreMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export class CoreEvent extends Event<Events.Message> {
if (message.author.bot || message.webhookID) return;

// Run the message parser.
this.context.client.emit(Events.PreMessageParsed, message);
this.container.client.emit(Events.PreMessageParsed, message);
}
}
6 changes: 3 additions & 3 deletions src/events/command-handler/CoreMessageParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CoreEvent extends Event<Events.PreMessageParsed> {

let prefix = null;
const mentionPrefix = this.getMentionPrefix(message.content);
const { client } = this.context;
const { client } = this.container;
const { regexPrefix } = client.options;
if (mentionPrefix) {
if (message.content.length === mentionPrefix.length) {
Expand Down Expand Up @@ -47,7 +47,7 @@ export class CoreEvent extends Event<Events.PreMessageParsed> {
}

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/events/command-handler/CorePreCommandRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class CoreEvent extends Event<Events.PreCommandRun> {
const { message, command } = payload;

// Run global preconditions:
const globalResult = await this.context.stores.get('preconditions').run(message, command, payload as any);
const globalResult = await this.container.stores.get('preconditions').run(message, command, payload as any);
if (!globalResult.success) {
message.client.emit(Events.CommandDenied, globalResult.error, payload);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/events/command-handler/CorePrefixedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class CoreEvent extends Event<Events.PrefixedMessage> {
}

public run(message: Message, prefix: string | RegExp) {
const { client, stores } = this.context;
const { client, stores } = this.container;
// Retrieve the command name and validate:
const commandPrefix = this.getCommandPrefix(message.content, prefix);
const prefixLess = message.content.slice(commandPrefix.length).trim();
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
AliasPieceOptions,
AliasStore,
Awaited,
container,
LoaderError,
MissingExportsError,
Piece,
Expand Down
10 changes: 5 additions & 5 deletions src/lib/SapphireClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Awaited, Store } from '@sapphire/pieces';
import { Awaited, container } from '@sapphire/pieces';
import { Client, ClientOptions, Message } from 'discord.js';
import { join } from 'path';
import type { Plugin } from './plugins/Plugin';
Expand Down Expand Up @@ -200,18 +200,18 @@ export class SapphireClient extends Client {
public constructor(options: ClientOptions = {}) {
super(options);

Store.injectedContext.client = this;
container.client = this;

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

this.logger = options.logger?.instance ?? new Logger(options.logger?.level ?? LogLevel.Info);
Store.injectedContext.logger = this.logger;
container.logger = this.logger;

this.stores = new StoreRegistry();
Store.injectedContext.stores = this.stores;
container.stores = this.stores;

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

Expand Down Expand Up @@ -290,7 +290,7 @@ declare module 'discord.js' {
}

declare module '@sapphire/pieces' {
interface PieceContextExtras {
interface Container {
client: SapphireClient;
logger: ILogger;
stores: StoreRegistry;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/parsers/Args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Store } from '@sapphire/pieces';
import { container } from '@sapphire/pieces';
import type {
CategoryChannel,
Channel,
Expand Down Expand Up @@ -655,7 +655,7 @@ export class Args {
*/
private resolveArgument<T>(arg: keyof ArgType | IArgument<T>): IArgument<T> | undefined {
if (typeof arg === 'object') return arg;
return Store.injectedContext.stores.get('arguments').get(arg as string) as IArgument<T> | undefined;
return container.stores.get('arguments').get(arg as string) as IArgument<T> | undefined;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/structures/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export abstract class Event<E extends keyof ClientEvents | symbol = ''> extends

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

Expand Down
2 changes: 1 addition & 1 deletion src/lib/structures/ExtendedArgument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export abstract class ExtendedArgument<K extends keyof ArgType, T> extends Argum
* into the value used to compute the extended argument's value.
*/
public get base(): IArgument<ArgType[K]> {
return this.context.stores.get('arguments').get(this.baseArgument) as IArgument<ArgType[K]>;
return this.container.stores.get('arguments').get(this.baseArgument) as IArgument<ArgType[K]>;
}

public async run(parameter: string, context: ArgumentContext<T>): AsyncArgumentResult<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/preconditions/PreconditionContainerSingle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Store } from '@sapphire/pieces';
import { container } from '@sapphire/pieces';
import type { Message } from 'discord.js';
import type { Command } from '../../structures/Command';
import type { PreconditionContext } from '../../structures/Precondition';
Expand Down Expand Up @@ -65,7 +65,7 @@ export class PreconditionContainerSingle implements IPreconditionContainer {
* @param command The command the message invoked.
*/
public run(message: Message, command: Command, context: PreconditionContext = {}) {
const precondition = Store.injectedContext.stores.get('preconditions').get(this.name);
const precondition = container.stores.get('preconditions').get(this.name);
if (precondition) return precondition.run(message, command, { ...context, ...this.context });
throw new Error(`The precondition "${this.name}" is not available.`);
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,10 @@
prettier "^2.2.1"
typescript "^4.2.4"

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