Skip to content

Commit

Permalink
Merge branch 'main' into feat/message-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele authored Mar 7, 2025
2 parents 0d75c94 + e273afb commit 8cd7564
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
26 changes: 26 additions & 0 deletions packages/collection/__tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,29 @@ describe('union() tests', () => {
});
});

describe('groupBy() tests', () => {
test('returns a collection of grouped items', () => {
const items = [
{ name: 'Alice', age: 20 },
{ name: 'Bob', age: 20 },
{ name: 'Charlie', age: 30 },
];

expect<Collection<number, typeof items>>(Collection.groupBy(items, (item) => item.age)).toStrictEqual(
new Collection<number, typeof items>([
[
20,
[
{ name: 'Alice', age: 20 },
{ name: 'Bob', age: 20 },
],
],
[30, [{ name: 'Charlie', age: 30 }]],
]),
);
});
});

describe('toReversed() tests', () => {
test('reverses a collection', () => {
const coll = createTestCollection();
Expand Down Expand Up @@ -1152,5 +1175,8 @@ describe('subclassing tests', () => {
test('Collection.combineEntries()', () => {
expect(DerivedCollection.combineEntries([], Object)).toBeInstanceOf(DerivedCollection);
});
test('Collection.groupBy()', () => {
expect(DerivedCollection.groupBy([], Object)).toBeInstanceOf(DerivedCollection);
});
});
});
11 changes: 11 additions & 0 deletions packages/collection/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,17 @@ export class Collection<Key, Value> extends Map<Key, Value> {
return coll;
}

/**
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy | Map.groupBy()}
* but returns a Collection instead of a Map.
*/
public static override groupBy<Key, Item>(
items: Iterable<Item>,
keySelector: (item: Item, index: number) => Key,
): Collection<Key, Item[]> {
return new this[Symbol.species]<Key, Item[]>(Map.groupBy(items, keySelector));
}

/**
* @internal
*/
Expand Down
14 changes: 11 additions & 3 deletions packages/discord.js/src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,17 @@ class Webhook {
* @property {string} [threadName] Name of the thread to create (only available if the webhook is in a forum channel)
* @property {Snowflake[]} [appliedTags]
* The tags to apply to the created thread (only available if the webhook is in a forum channel)
* @property {boolean} [withComponents] Whether to allow sending non-interactive components in the message.
* <info>For application-owned webhooks, this property is ignored</info>
*/

/**
* Options that can be passed into editMessage.
* @typedef {MessageEditOptions} WebhookMessageEditOptions
* @property {Snowflake} [threadId] The id of the thread this message belongs to
* <info>For interaction webhooks, this property is ignored</info>
* @property {boolean} [withComponents] Whether to allow sending non-interactive components in the message.
* <info>For application-owned webhooks, this property is ignored</info>
*/

/**
Expand Down Expand Up @@ -217,6 +221,7 @@ class Webhook {
const query = makeURLSearchParams({
wait: true,
thread_id: messagePayload.options.threadId,
with_components: messagePayload.options.withComponents,
});

const { body, files } = await messagePayload.resolveFiles();
Expand Down Expand Up @@ -338,14 +343,17 @@ class Webhook {

const { body, files } = await messagePayload.resolveBody().resolveFiles();

const query = makeURLSearchParams({
thread_id: messagePayload.options.threadId,
with_components: messagePayload.options.withComponents,
});

const d = await this.client.rest.patch(
Routes.webhookMessage(this.id, this.token, typeof message === 'string' ? message : message.id),
{
body,
files,
query: messagePayload.options.threadId
? makeURLSearchParams({ thread_id: messagePayload.options.threadId })
: undefined,
query,
auth: false,
},
);
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6791,6 +6791,7 @@ export interface WebhookEditOptions {

export interface WebhookMessageEditOptions extends MessageEditOptions {
threadId?: Snowflake;
withComponents?: boolean;
}

export interface InteractionEditReplyOptions
Expand All @@ -6810,6 +6811,7 @@ export interface WebhookMessageCreateOptions
threadId?: Snowflake;
threadName?: string;
appliedTags?: readonly Snowflake[];
withComponents?: boolean;
}

export interface WidgetActivity {
Expand Down

0 comments on commit 8cd7564

Please sign in to comment.