Skip to content

Commit

Permalink
chore: ReadonlyCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Dec 10, 2023
1 parent b9f301e commit 4c879fb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 44 deletions.
12 changes: 12 additions & 0 deletions packages/discord.js/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,25 @@
"selector": "MethodDefinition > TSEmptyBodyFunctionExpression > Identifier :not(TSTypeOperator[operator=readonly]) > TSArrayType",
"message": "Array parameters on methods must be readonly"
},
{
"selector": "MethodDefinition > TSEmptyBodyFunctionExpression > Identifier TSTypeReference > Identifier[name=Collection]",
"message": "Parameters of type Collection on methods must use ReadonlyCollection"
},
{
"selector": "TSDeclareFunction > Identifier :not(TSTypeOperator[operator=readonly]) > TSArrayType",
"message": "Array parameters on functions must be readonly"
},
{
"selector": "TSDeclareFunction Identifier TSTypeReference > Identifier[name=Collection]",
"message": "Parameters of type Collection on functions must use ReadonlyCollection"
},
{
"selector": "TSInterfaceDeclaration TSPropertySignature :not(TSTypeOperator[operator=readonly]) > TSArrayType",
"message": "Array properties on interfaces must be readonly"
},
{
"selector": "TSInterfaceDeclaration TSPropertySignature TSTypeReference > Identifier[name=Collection]",
"message": "Interface properties of type Collection must use ReadonlyCollection"
}
]
}
Expand Down
89 changes: 49 additions & 40 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ export class AutoModerationRule extends Base {
public setActions(actions: readonly AutoModerationActionOptions[], reason?: string): Promise<AutoModerationRule>;
public setEnabled(enabled?: boolean, reason?: string): Promise<AutoModerationRule>;
public setExemptRoles(
roles: Collection<Snowflake, Role> | readonly RoleResolvable[],
roles: ReadonlyCollection<Snowflake, Role> | readonly RoleResolvable[],
reason?: string,
): Promise<AutoModerationRule>;
public setExemptChannels(
channels: Collection<Snowflake, GuildBasedChannel> | readonly GuildChannelResolvable[],
channels: ReadonlyCollection<Snowflake, GuildBasedChannel> | readonly GuildChannelResolvable[],
reason?: string,
): Promise<AutoModerationRule>;
}
Expand Down Expand Up @@ -1114,7 +1114,7 @@ export interface CollectorEventTypes<Key, Value, Extras extends unknown[] = []>
collect: [Value, ...Extras];
ignore: [Value, ...Extras];
dispose: [Value, ...Extras];
end: [collected: Collection<Key, Value>, reason: string];
end: [collected: ReadonlyCollection<Key, Value>, reason: string];
}

export abstract class Collector<Key, Value, Extras extends unknown[] = []> extends EventEmitter {
Expand Down Expand Up @@ -1895,14 +1895,14 @@ export class InteractionCollector<Interaction extends CollectedInteraction> exte
public on(event: 'collect' | 'dispose' | 'ignore', listener: (interaction: Interaction) => Awaitable<void>): this;
public on(
event: 'end',
listener: (collected: Collection<Snowflake, Interaction>, reason: string) => Awaitable<void>,
listener: (collected: ReadonlyCollection<Snowflake, Interaction>, reason: string) => Awaitable<void>,
): this;
public on(event: string, listener: (...args: readonly any[]) => Awaitable<void>): this;

public once(event: 'collect' | 'dispose' | 'ignore', listener: (interaction: Interaction) => Awaitable<void>): this;
public once(
event: 'end',
listener: (collected: Collection<Snowflake, Interaction>, reason: string) => Awaitable<void>,
listener: (collected: ReadonlyCollection<Snowflake, Interaction>, reason: string) => Awaitable<void>,
): this;
public once(event: string, listener: (...args: readonly any[]) => Awaitable<void>): this;
}
Expand Down Expand Up @@ -2241,8 +2241,8 @@ export class MessageFlagsBitField extends BitField<MessageFlagsString> {
export class MessageMentions<InGuild extends boolean = boolean> {
private constructor(
message: Message,
users: readonly APIUser[] | Collection<Snowflake, User>,
roles: readonly Snowflake[] | Collection<Snowflake, Role>,
users: readonly APIUser[] | ReadonlyCollection<Snowflake, User>,
roles: readonly Snowflake[] | ReadonlyCollection<Snowflake, Role>,
everyone: boolean,
repliedUser?: APIUser | User,
);
Expand Down Expand Up @@ -2559,7 +2559,10 @@ export class ReactionCollector extends Collector<Snowflake | string, MessageReac
event: 'collect' | 'dispose' | 'remove' | 'ignore',
listener: (reaction: MessageReaction, user: User) => void,
): this;
public on(event: 'end', listener: (collected: Collection<Snowflake, MessageReaction>, reason: string) => void): this;
public on(
event: 'end',
listener: (collected: ReadonlyCollection<Snowflake, MessageReaction>, reason: string) => void,
): this;
public on(event: string, listener: (...args: readonly any[]) => void): this;

public once(
Expand All @@ -2568,7 +2571,7 @@ export class ReactionCollector extends Collector<Snowflake | string, MessageReac
): this;
public once(
event: 'end',
listener: (collected: Collection<Snowflake, MessageReaction>, reason: string) => void,
listener: (collected: ReadonlyCollection<Snowflake, MessageReaction>, reason: string) => void,
): this;
public once(event: string, listener: (...args: readonly any[]) => void): this;
}
Expand Down Expand Up @@ -3269,7 +3272,7 @@ export class UserFlagsBitField extends BitField<UserFlagsString> {
export function basename(path: string, ext?: string): string;
export function cleanContent(str: string, channel: TextBasedChannel): string;
export function discordSort<Key, Value extends { rawPosition: number; id: Snowflake }>(
collection: Collection<Key, Value>,
collection: ReadonlyCollection<Key, Value>,
): Collection<Key, Value>;
export function cleanCodeBlockContent(text: string): string;
export function fetchRecommendedShardCount(token: string, options?: FetchRecommendedShardCountOptions): Promise<number>;
Expand Down Expand Up @@ -3297,7 +3300,7 @@ export function setPosition<Item extends Channel | Role>(
item: Item,
position: number,
relative: boolean,
sorted: Collection<Snowflake, Item>,
sorted: ReadonlyCollection<Snowflake, Item>,
client: Client<true>,
route: string,
reason?: string,
Expand Down Expand Up @@ -4104,11 +4107,11 @@ export class GuildEmojiRoleManager extends DataManager<Snowflake, Role, RoleReso
public emoji: GuildEmoji;
public guild: Guild;
public add(
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | Collection<Snowflake, Role>,
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
): Promise<GuildEmoji>;
public set(roles: readonly RoleResolvable[] | Collection<Snowflake, Role>): Promise<GuildEmoji>;
public set(roles: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>): Promise<GuildEmoji>;
public remove(
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | Collection<Snowflake, Role>,
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
): Promise<GuildEmoji>;
}

Expand Down Expand Up @@ -4219,12 +4222,15 @@ export class GuildMemberRoleManager extends DataManager<Snowflake, Role, RoleRes
public guild: Guild;

public add(
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | Collection<Snowflake, Role>,
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
reason?: string,
): Promise<GuildMember>;
public set(
roles: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
reason?: string,
): Promise<GuildMember>;
public set(roles: readonly RoleResolvable[] | Collection<Snowflake, Role>, reason?: string): Promise<GuildMember>;
public remove(
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | Collection<Snowflake, Role>,
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
reason?: string,
): Promise<GuildMember>;
}
Expand Down Expand Up @@ -4265,7 +4271,7 @@ export class PermissionOverwriteManager extends CachedManager<
> {
private constructor(client: Client<true>, iterable?: Iterable<RawPermissionOverwriteData>);
public set(
overwrites: readonly OverwriteResolvable[] | Collection<Snowflake, OverwriteResolvable>,
overwrites: readonly OverwriteResolvable[] | ReadonlyCollection<Snowflake, OverwriteResolvable>,
reason?: string,
): Promise<NonThreadGuildBasedChannel>;
private upsert(
Expand Down Expand Up @@ -4496,7 +4502,7 @@ export interface ActivityOptions {
export interface AddGuildMemberOptions {
accessToken: string;
nick?: string;
roles?: Collection<Snowflake, Role> | readonly RoleResolvable[];
roles?: ReadonlyCollection<Snowflake, Role> | readonly RoleResolvable[];
mute?: boolean;
deaf?: boolean;
force?: boolean;
Expand Down Expand Up @@ -4938,7 +4944,7 @@ export type CacheWithLimitsOptions = {

export interface CategoryCreateChannelOptions {
name: string;
permissionOverwrites?: readonly OverwriteResolvable[] | Collection<Snowflake, OverwriteResolvable>;
permissionOverwrites?: readonly OverwriteResolvable[] | ReadonlyCollection<Snowflake, OverwriteResolvable>;
topic?: string;
type?: CategoryChannelType;
nsfw?: boolean;
Expand Down Expand Up @@ -5020,7 +5026,7 @@ export interface ClientEvents {
guildMemberAvailable: [member: GuildMember | PartialGuildMember];
guildMemberRemove: [member: GuildMember | PartialGuildMember];
guildMembersChunk: [
members: Collection<Snowflake, GuildMember>,
members: ReadonlyCollection<Snowflake, GuildMember>,
guild: Guild,
data: { index: number; count: number; notFound: readonly unknown[]; nonce: string | undefined },
];
Expand All @@ -5032,10 +5038,13 @@ export interface ClientEvents {
messageDelete: [message: Message | PartialMessage];
messageReactionRemoveAll: [
message: Message | PartialMessage,
reactions: Collection<string | Snowflake, MessageReaction>,
reactions: ReadonlyCollection<string | Snowflake, MessageReaction>,
];
messageReactionRemoveEmoji: [reaction: MessageReaction | PartialMessageReaction];
messageDeleteBulk: [messages: Collection<Snowflake, Message | PartialMessage>, channel: GuildTextBasedChannel];
messageDeleteBulk: [
messages: ReadonlyCollection<Snowflake, Message | PartialMessage>,
channel: GuildTextBasedChannel,
];
messageReactionAdd: [reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser];
messageReactionRemove: [reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser];
messageUpdate: [oldMessage: Message | PartialMessage, newMessage: Message | PartialMessage];
Expand All @@ -5047,11 +5056,11 @@ export interface ClientEvents {
roleUpdate: [oldRole: Role, newRole: Role];
threadCreate: [thread: AnyThreadChannel, newlyCreated: boolean];
threadDelete: [thread: AnyThreadChannel];
threadListSync: [threads: Collection<Snowflake, AnyThreadChannel>, guild: Guild];
threadListSync: [threads: ReadonlyCollection<Snowflake, AnyThreadChannel>, guild: Guild];
threadMemberUpdate: [oldMember: ThreadMember, newMember: ThreadMember];
threadMembersUpdate: [
addedMembers: Collection<Snowflake, ThreadMember>,
removedMembers: Collection<Snowflake, ThreadMember | PartialThreadMember>,
addedMembers: ReadonlyCollection<Snowflake, ThreadMember>,
removedMembers: ReadonlyCollection<Snowflake, ThreadMember | PartialThreadMember>,
thread: AnyThreadChannel,
];
threadUpdate: [oldThread: AnyThreadChannel, newThread: AnyThreadChannel];
Expand Down Expand Up @@ -5162,12 +5171,12 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
}

export interface CommandInteractionResolvedData<Cached extends CacheType = CacheType> {
users?: Collection<Snowflake, User>;
members?: Collection<Snowflake, CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>>;
roles?: Collection<Snowflake, CacheTypeReducer<Cached, Role, APIRole>>;
channels?: Collection<Snowflake, CacheTypeReducer<Cached, Channel, APIInteractionDataResolvedChannel>>;
messages?: Collection<Snowflake, CacheTypeReducer<Cached, Message, APIMessage>>;
attachments?: Collection<Snowflake, Attachment>;
users?: ReadonlyCollection<Snowflake, User>;
members?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>>;
roles?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, Role, APIRole>>;
channels?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, Channel, APIInteractionDataResolvedChannel>>;
messages?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, Message, APIMessage>>;
attachments?: ReadonlyCollection<Snowflake, Attachment>;
}

export interface AutocompleteFocusedOption extends Pick<CommandInteractionOption, 'name'> {
Expand Down Expand Up @@ -5413,8 +5422,8 @@ export interface FetchChannelOptions extends BaseFetchOptions {
}

export interface FetchedThreads {
threads: Collection<Snowflake, AnyThreadChannel>;
members: Collection<Snowflake, ThreadMember>;
threads: ReadonlyCollection<Snowflake, AnyThreadChannel>;
members: ReadonlyCollection<Snowflake, ThreadMember>;
}

export interface FetchedThreadsMore extends FetchedThreads {
Expand Down Expand Up @@ -5669,8 +5678,8 @@ export interface AutoModerationRuleCreateOptions {
triggerMetadata?: AutoModerationTriggerMetadataOptions;
actions: readonly AutoModerationActionOptions[];
enabled?: boolean;
exemptRoles?: Collection<Snowflake, Role> | readonly RoleResolvable[];
exemptChannels?: Collection<Snowflake, GuildBasedChannel> | readonly GuildChannelResolvable[];
exemptRoles?: ReadonlyCollection<Snowflake, Role> | readonly RoleResolvable[];
exemptChannels?: ReadonlyCollection<Snowflake, GuildBasedChannel> | readonly GuildChannelResolvable[];
reason?: string;
}

Expand Down Expand Up @@ -5714,7 +5723,7 @@ export interface GuildChannelEditOptions {
parent?: CategoryChannelResolvable | null;
rateLimitPerUser?: number;
lockPermissions?: boolean;
permissionOverwrites?: readonly OverwriteResolvable[] | Collection<Snowflake, OverwriteResolvable>;
permissionOverwrites?: readonly OverwriteResolvable[] | ReadonlyCollection<Snowflake, OverwriteResolvable>;
defaultAutoArchiveDuration?: ThreadAutoArchiveDuration;
rtcRegion?: string | null;
videoQualityMode?: VideoQualityMode | null;
Expand Down Expand Up @@ -5778,13 +5787,13 @@ export interface GuildEditOptions {
export interface GuildEmojiCreateOptions {
attachment: BufferResolvable | Base64Resolvable;
name: string;
roles?: Collection<Snowflake, Role> | readonly RoleResolvable[];
roles?: ReadonlyCollection<Snowflake, Role> | readonly RoleResolvable[];
reason?: string;
}

export interface GuildEmojiEditOptions {
name?: string;
roles?: Collection<Snowflake, Role> | readonly RoleResolvable[];
roles?: ReadonlyCollection<Snowflake, Role> | readonly RoleResolvable[];
reason?: string;
}

Expand All @@ -5805,7 +5814,7 @@ export interface GuildStickerEditOptions {

export interface GuildMemberEditOptions {
nick?: string | null;
roles?: Collection<Snowflake, Role> | readonly RoleResolvable[];
roles?: ReadonlyCollection<Snowflake, Role> | readonly RoleResolvable[];
mute?: boolean;
deaf?: boolean;
channel?: GuildVoiceChannelResolvable | null;
Expand Down
9 changes: 5 additions & 4 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ import {
} from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
import { ReadonlyCollection } from '@discordjs/collection';

// Test type transformation:
declare const serialize: <Value>(value: Value) => Serialized<Value>;
Expand Down Expand Up @@ -1167,8 +1168,8 @@ client.on('threadMembersUpdate', (addedMembers, removedMembers, thread) => {
expectType<Client<true>>(addedMembers.first()!.client);
expectType<Client<true>>(removedMembers.first()!.client);
expectType<Client<true>>(thread.client);
expectType<Collection<Snowflake, ThreadMember>>(addedMembers);
expectType<Collection<Snowflake, ThreadMember | PartialThreadMember>>(removedMembers);
expectType<ReadonlyCollection<Snowflake, ThreadMember>>(addedMembers);
expectType<ReadonlyCollection<Snowflake, ThreadMember | PartialThreadMember>>(removedMembers);
expectType<AnyThreadChannel>(thread);
const left = removedMembers.first();
if (!left) return;
Expand Down Expand Up @@ -1986,7 +1987,7 @@ client.on('interactionCreate', async interaction => {
const requiredOption = interaction.options.get('name', true);
expectType<CommandInteractionOption | null>(optionalOption);
expectType<CommandInteractionOption>(requiredOption);
expectType<CommandInteractionOption[] | undefined>(requiredOption.options);
expectType<readonly CommandInteractionOption[] | undefined>(requiredOption.options);

expectType<string | null>(interaction.options.getString('name', booleanValue));
expectType<string | null>(interaction.options.getString('name', false));
Expand Down Expand Up @@ -2078,7 +2079,7 @@ collector.on('dispose', (vals, ...other) => {
});

collector.on('end', (collection, reason) => {
expectType<Collection<string, Interaction>>(collection);
expectType<ReadonlyCollection<string, Interaction>>(collection);
expectType<string>(reason);
});

Expand Down

0 comments on commit 4c879fb

Please sign in to comment.