Skip to content

Commit

Permalink
Update all contexts to use server context
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Jul 15, 2024
1 parent 302c287 commit 2f92f3d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 39 deletions.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ export class BaseSlashCreator extends (EventEmitter as any as new () => TypedEve
interaction: AnyRequestData,
respond: RespondFunction | null,
webserverMode: boolean,
context: unknown
serverContext: unknown
) {
this.emit('rawInteraction', interaction);

Expand Down Expand Up @@ -598,7 +598,7 @@ export class BaseSlashCreator extends (EventEmitter as any as new () => TypedEve
webserverMode,
this.unknownCommand.deferEphemeral,
!this.options.disableTimeouts,
context
serverContext
);
return this._runCommand(this.unknownCommand, ctx);
} else if (this.options.unknownCommandResponse)
Expand Down Expand Up @@ -627,7 +627,7 @@ export class BaseSlashCreator extends (EventEmitter as any as new () => TypedEve
webserverMode,
command.deferEphemeral,
!this.options.disableTimeouts,
context
serverContext
);

// Ensure the user has permission to use the command
Expand Down Expand Up @@ -659,7 +659,7 @@ export class BaseSlashCreator extends (EventEmitter as any as new () => TypedEve
);

if (this._componentCallbacks.size || this.listenerCount('componentInteraction') > 0) {
const ctx = new ComponentContext(this, interaction, respond, !this.options.disableTimeouts);
const ctx = new ComponentContext(this, interaction, respond, !this.options.disableTimeouts, serverContext);
this.emit('componentInteraction', ctx);

this.cleanRegisteredComponents();
Expand All @@ -685,7 +685,7 @@ export class BaseSlashCreator extends (EventEmitter as any as new () => TypedEve
}
case InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE: {
const command = this._getCommandFromInteraction(interaction);
const ctx = new AutocompleteContext(this, interaction, respond);
const ctx = new AutocompleteContext(this, interaction, respond, serverContext);
this.emit('autocompleteInteraction', ctx, command);

if (!command) {
Expand Down Expand Up @@ -716,20 +716,25 @@ export class BaseSlashCreator extends (EventEmitter as any as new () => TypedEve
}
case InteractionType.MODAL_SUBMIT: {
try {
const context = new ModalInteractionContext(this, interaction, respond, !this.options.disableTimeouts);
this.emit('modalInteraction', context);
const ctx = new ModalInteractionContext(
this,
interaction,
respond,
!this.options.disableTimeouts,
serverContext
);
this.emit('modalInteraction', ctx);

this.cleanRegisteredComponents();

const modalCallbackKey = `${context.user.id}-${context.customID}`;
const globalCallbackKey = `global-${context.customID}`;
const modalCallbackKey = `${ctx.user.id}-${ctx.customID}`;
const globalCallbackKey = `global-${ctx.customID}`;
if (this._modalCallbacks.has(modalCallbackKey)) {
this._modalCallbacks.get(modalCallbackKey)!.callback(context);
this._modalCallbacks.get(modalCallbackKey)!.callback(ctx);
this._modalCallbacks.delete(modalCallbackKey);
return;
}
if (this._modalCallbacks.has(globalCallbackKey))
this._modalCallbacks.get(modalCallbackKey)!.callback(context);
if (this._modalCallbacks.has(globalCallbackKey)) this._modalCallbacks.get(modalCallbackKey)!.callback(ctx);

return;
} catch (err) {
Expand Down
12 changes: 9 additions & 3 deletions src/structures/interfaces/autocompleteContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BaseInteractionContext } from './baseInteraction';
import { CommandContext } from './commandContext';

/** Represents a autocomplete interaction context. */
export class AutocompleteContext extends BaseInteractionContext {
export class AutocompleteContext<ServerContext extends any = unknown> extends BaseInteractionContext<ServerContext> {
/** The full interaction data. */
readonly data: CommandAutocompleteRequestData;
/** The options given to the command. */
Expand All @@ -24,9 +24,15 @@ export class AutocompleteContext extends BaseInteractionContext {
* @param creator The instantiating creator.
* @param data The interaction data.
* @param respond The response function for the interaction.
* @param serverContext The context of the server.
*/
constructor(creator: BaseSlashCreator, data: CommandAutocompleteRequestData, respond: RespondFunction) {
super(creator, data);
constructor(
creator: BaseSlashCreator,
data: CommandAutocompleteRequestData,
respond: RespondFunction,
serverContext: ServerContext
) {
super(creator, data, serverContext);
this._respond = respond;

this.data = data;
Expand Down
8 changes: 6 additions & 2 deletions src/structures/interfaces/baseInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { ResolvedMember } from '../resolvedMember';
import { Role } from '../role';

/** Represents a basic interaction context. */
export class BaseInteractionContext {
export class BaseInteractionContext<ServerContext extends any = unknown> {
/** The creator of the interaction request. */
readonly creator: BaseSlashCreator;
/** Context passed by the server */
readonly serverContext: ServerContext;
/** The interaction's token. */
readonly interactionToken: string;
/** The interaction's ID. */
Expand Down Expand Up @@ -61,9 +63,11 @@ export class BaseInteractionContext {
/**
* @param creator The instantiating creator.
* @param data The interaction data.
* @param serverContext The context of the server.
*/
constructor(creator: BaseSlashCreator, data: any) {
constructor(creator: BaseSlashCreator, data: any, serverContext: ServerContext) {
this.creator = creator;
this.serverContext = serverContext;

this.interactionToken = data.token;
this.interactionID = data.id;
Expand Down
9 changes: 3 additions & 6 deletions src/structures/interfaces/commandContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AnyCommandOption, ApplicationCommandType, InteractionRequestData } from
import { ModalSendableContext } from './modalSendableContext';

/** Context representing a command interaction. */
export class CommandContext<ServerContext extends any = unknown> extends ModalSendableContext {
export class CommandContext<ServerContext extends any = unknown> extends ModalSendableContext<ServerContext> {
/** The full interaction data. */
readonly data: InteractionRequestData;

Expand All @@ -24,9 +24,6 @@ export class CommandContext<ServerContext extends any = unknown> extends ModalSe
/** Whether the context is from a webserver. */
private webserverMode: boolean;

/** Context passed by the server */
readonly serverContext: ServerContext;

/**
* @param creator The instantiating creator.
* @param data The interaction data for the context.
Expand All @@ -44,7 +41,7 @@ export class CommandContext<ServerContext extends any = unknown> extends ModalSe
useTimeout = true,
serverContext: ServerContext
) {
super(creator, data, respond);
super(creator, data, respond, serverContext);
this.data = data;
this.webserverMode = webserverMode;

Expand All @@ -54,7 +51,7 @@ export class CommandContext<ServerContext extends any = unknown> extends ModalSe
if (data.data.target_id) this.targetID = data.data.target_id;
this.options = data.data.options ? CommandContext.convertOptions(data.data.options) : {};
this.subcommands = data.data.options ? CommandContext.getSubcommandArray(data.data.options) : [];
this.serverContext = serverContext;

// Auto-defer if no response was given in 2 seconds
if (useTimeout) this._timeout = setTimeout(() => this.defer(deferEphemeral || false), 2000);
}
Expand Down
8 changes: 5 additions & 3 deletions src/structures/interfaces/componentContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { formatAllowedMentions, FormattedAllowedMentions } from '../../util';
import { ModalSendableContext } from './modalSendableContext';

/** Represents an interaction context from a message component. */
export class ComponentContext extends ModalSendableContext {
export class ComponentContext<ServerContext extends any = unknown> extends ModalSendableContext<ServerContext> {
/** The request data. */
readonly data: MessageComponentRequestData;

Expand All @@ -25,14 +25,16 @@ export class ComponentContext extends ModalSendableContext {
* @param data The interaction data for the context.
* @param respond The response function for the interaction.
* @param useTimeout Whether to use the acknowledgement timeout.
* @param serverContext The context of the server.
*/
constructor(
creator: BaseSlashCreator,
data: MessageComponentRequestData,
respond: RespondFunction,
useTimeout = true
useTimeout = true,
serverContext: ServerContext
) {
super(creator, data, respond);
super(creator, data, respond, serverContext);
this.data = data;

this.customID = data.data.custom_id;
Expand Down
9 changes: 6 additions & 3 deletions src/structures/interfaces/messageInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { Message, MessageEmbedOptions } from '../message';
import { BaseInteractionContext } from './baseInteraction';

/** Represents a interaction context that handles messages. */
export class MessageInteractionContext extends BaseInteractionContext {
export class MessageInteractionContext<
ServerContext extends any = unknown
> extends BaseInteractionContext<ServerContext> {
/** Whether the initial response was sent. */
initiallyResponded = false;
/** Whether there is a deferred message available. */
Expand All @@ -22,9 +24,10 @@ export class MessageInteractionContext extends BaseInteractionContext {
* @param creator The instantiating creator.
* @param data The interaction data.
* @param respond The response function for the interaction.
* @param serverContext The context of the server.
*/
constructor(creator: BaseSlashCreator, data: any, respond: RespondFunction) {
super(creator, data);
constructor(creator: BaseSlashCreator, data: any, respond: RespondFunction, serverContext: ServerContext) {
super(creator, data, serverContext);
this._respond = respond;
}

Expand Down
15 changes: 12 additions & 3 deletions src/structures/interfaces/modalInteractionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { Message } from '../message';
import { EditMessageOptions, MessageInteractionContext } from './messageInteraction';

/** Represents an interaction context from a modal submission. */
export class ModalInteractionContext extends MessageInteractionContext {
export class ModalInteractionContext<
ServerContext extends any = unknown
> extends MessageInteractionContext<ServerContext> {
/** The request data. */
readonly data: ModalSubmitRequestData;
/** The ID of the component to identify its origin from. */
Expand All @@ -27,9 +29,16 @@ export class ModalInteractionContext extends MessageInteractionContext {
* @param data The interaction data for the context.
* @param respond The response function for the interaction.
* @param useTimeout Whether to use the deferral timeout.
* @param serverContext The context of the server.
*/
constructor(creator: BaseSlashCreator, data: ModalSubmitRequestData, respond: RespondFunction, useTimeout = true) {
super(creator, data, respond);
constructor(
creator: BaseSlashCreator,
data: ModalSubmitRequestData,
respond: RespondFunction,
useTimeout = true,
serverContext: ServerContext
) {
super(creator, data, respond, serverContext);

this.data = data;
this.customID = data.data.custom_id;
Expand Down
8 changes: 5 additions & 3 deletions src/structures/interfaces/modalSendableContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { generateID } from '../../util';
import { MessageInteractionContext } from './messageInteraction';

/** Represents an interaction that can send modals. */
export class ModalSendableContext extends MessageInteractionContext {
constructor(creator: BaseSlashCreator, data: any, respond: RespondFunction) {
super(creator, data, respond);
export class ModalSendableContext<
ServerContext extends any = unknown
> extends MessageInteractionContext<ServerContext> {
constructor(creator: BaseSlashCreator, data: any, respond: RespondFunction, serverContext: ServerContext) {
super(creator, data, respond, serverContext);
}

/**
Expand Down

0 comments on commit 2f92f3d

Please sign in to comment.