Skip to content

Commit 978a983

Browse files
authored
Merge branch 'main' into types/flags
2 parents 7b138b7 + a65c982 commit 978a983

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

packages/discord.js/src/errors/Messages.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ const Messages = {
7070
[DjsErrorCodes.ChannelNotCached]: 'Could not find the channel where this message came from in the cache!',
7171
[DjsErrorCodes.StageChannelResolve]: 'Could not resolve channel to a stage channel.',
7272
[DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.',
73-
[DjsErrorCodes.FetchOwnerId]: type => `Couldn't resolve the ${type} ownerId to fetch the ${type} member.`,
73+
[DjsErrorCodes.FetchOwnerId]: type =>
74+
`Couldn't resolve the ${type} ownerId to fetch the ${type} ${type === 'group DM' ? 'owner' : 'member'}.`,
7475

7576
[DjsErrorCodes.InvalidType]: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`,
7677
[DjsErrorCodes.InvalidElement]: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`,

packages/discord.js/src/structures/PartialGroupDMChannel.js

+64
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22

33
const { BaseChannel } = require('./BaseChannel');
4+
const TextBasedChannel = require('./interfaces/TextBasedChannel');
45
const { DiscordjsError, ErrorCodes } = require('../errors');
56
const PartialGroupDMMessageManager = require('../managers/PartialGroupDMMessageManager');
67

78
/**
89
* Represents a Partial Group DM Channel on Discord.
910
* @extends {BaseChannel}
11+
* @implements {TextBasedChannel}
1012
*/
1113
class PartialGroupDMChannel extends BaseChannel {
1214
constructor(client, data) {
@@ -44,6 +46,36 @@ class PartialGroupDMChannel extends BaseChannel {
4446
* @type {PartialGroupDMMessageManager}
4547
*/
4648
this.messages = new PartialGroupDMMessageManager(this);
49+
50+
if ('owner_id' in data) {
51+
/**
52+
* The user id of the owner of this Group DM Channel
53+
* @type {?Snowflake}
54+
*/
55+
this.ownerId = data.owner_id;
56+
} else {
57+
this.ownerId ??= null;
58+
}
59+
60+
if ('last_message_id' in data) {
61+
/**
62+
* The channel's last message id, if one was sent
63+
* @type {?Snowflake}
64+
*/
65+
this.lastMessageId = data.last_message_id;
66+
} else {
67+
this.lastMessageId ??= null;
68+
}
69+
70+
if ('last_pin_timestamp' in data) {
71+
/**
72+
* The timestamp when the last pinned message was pinned, if there was one
73+
* @type {?number}
74+
*/
75+
this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null;
76+
} else {
77+
this.lastPinTimestamp ??= null;
78+
}
4779
}
4880

4981
/**
@@ -55,13 +87,45 @@ class PartialGroupDMChannel extends BaseChannel {
5587
return this.icon && this.client.rest.cdn.channelIcon(this.id, this.icon, options);
5688
}
5789

90+
/**
91+
* Fetches the owner of this Group DM Channel.
92+
* @param {BaseFetchOptions} [options] The options for fetching the user
93+
* @returns {Promise<User>}
94+
*/
95+
async fetchOwner(options) {
96+
if (!this.ownerId) {
97+
throw new DiscordjsError(ErrorCodes.FetchOwnerId, 'group DM');
98+
}
99+
100+
return this.client.users.fetch(this.ownerId, options);
101+
}
102+
58103
delete() {
59104
return Promise.reject(new DiscordjsError(ErrorCodes.DeleteGroupDMChannel));
60105
}
61106

62107
fetch() {
63108
return Promise.reject(new DiscordjsError(ErrorCodes.FetchGroupDMChannel));
64109
}
110+
111+
// These are here only for documentation purposes - they are implemented by TextBasedChannel
112+
/* eslint-disable no-empty-function */
113+
get lastMessage() {}
114+
get lastPinAt() {}
115+
createMessageComponentCollector() {}
116+
awaitMessageComponent() {}
65117
}
66118

119+
TextBasedChannel.applyToClass(PartialGroupDMChannel, true, [
120+
'bulkDelete',
121+
'send',
122+
'sendTyping',
123+
'createMessageCollector',
124+
'awaitMessages',
125+
'fetchWebhooks',
126+
'createWebhook',
127+
'setRateLimitPerUser',
128+
'setNSFW',
129+
]);
130+
67131
module.exports = PartialGroupDMChannel;

packages/discord.js/typings/index.d.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ export class ContextMenuCommandInteraction<Cached extends CacheType = CacheType>
13121312
// tslint:disable-next-line no-empty-interface
13131313
export interface DMChannel
13141314
extends Omit<
1315-
TextBasedChannelFields<false>,
1315+
TextBasedChannelFields<false, true>,
13161316
'bulkDelete' | 'fetchWebhooks' | 'createWebhook' | 'setRateLimitPerUser' | 'setNSFW'
13171317
> {}
13181318
export class DMChannel extends BaseChannel {
@@ -2595,15 +2595,29 @@ export class OAuth2Guild extends BaseGuild {
25952595
public permissions: Readonly<PermissionsBitField>;
25962596
}
25972597

2598+
export interface PartialGroupDMChannel
2599+
extends Omit<
2600+
TextBasedChannelFields<false, false>,
2601+
| 'bulkDelete'
2602+
| 'send'
2603+
| 'sendTyping'
2604+
| 'createMessageCollector'
2605+
| 'awaitMessages'
2606+
| 'fetchWebhooks'
2607+
| 'createWebhook'
2608+
| 'setRateLimitPerUser'
2609+
| 'setNSFW'
2610+
> {}
25982611
export class PartialGroupDMChannel extends BaseChannel {
25992612
private constructor(client: Client<true>, data: RawPartialGroupDMChannelData);
26002613
public type: ChannelType.GroupDM;
26012614
public flags: null;
26022615
public name: string | null;
26032616
public icon: string | null;
26042617
public recipients: PartialRecipient[];
2605-
public messages: PartialGroupDMMessageManager;
2618+
public ownerId: Snowflake | null;
26062619
public iconURL(options?: ImageURLOptions): string | null;
2620+
public fetchOwner(options?: BaseFetchOptions): Promise<User>;
26072621
public toString(): ChannelMention;
26082622
}
26092623

@@ -4556,13 +4570,13 @@ export interface PartialTextBasedChannelFields<InGuild extends boolean = boolean
45564570
send(options: string | MessagePayload | MessageCreateOptions): Promise<Message<InGuild>>;
45574571
}
45584572

4559-
export interface TextBasedChannelFields<InGuild extends boolean = boolean>
4573+
export interface TextBasedChannelFields<InGuild extends boolean = boolean, InDM extends boolean = boolean>
45604574
extends PartialTextBasedChannelFields<InGuild> {
45614575
lastMessageId: Snowflake | null;
45624576
get lastMessage(): Message | null;
45634577
lastPinTimestamp: number | null;
45644578
get lastPinAt(): Date | null;
4565-
messages: If<InGuild, GuildMessageManager, DMMessageManager>;
4579+
messages: If<InGuild, GuildMessageManager, If<InDM, DMMessageManager, PartialGroupDMMessageManager>>;
45664580
awaitMessageComponent<ComponentType extends MessageComponentType>(
45674581
options?: AwaitMessageCollectorOptionsParams<ComponentType, true>,
45684582
): Promise<MappedInteractionTypes[ComponentType]>;

0 commit comments

Comments
 (0)