Skip to content

Commit 2207fa3

Browse files
committed
refactor: support new builders
1 parent 497294e commit 2207fa3

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

packages/discord.js/src/managers/ChannelManager.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const process = require('node:process');
4-
const { lazy } = require('@discordjs/util');
4+
const { lazy, isFileBodyEncodable, isJSONEncodable } = require('@discordjs/util');
55
const { Routes } = require('discord-api-types/v10');
66
const { BaseChannel } = require('../structures/BaseChannel.js');
77
const { MessagePayload } = require('../structures/MessagePayload.js');
@@ -147,7 +147,7 @@ class ChannelManager extends CachedManager {
147147
* Creates a message in a channel.
148148
*
149149
* @param {TextChannelResolvable} channel The channel to send the message to
150-
* @param {string|MessagePayload|MessageCreateOptions} options The options to provide
150+
* @param {string|MessagePayload|MessageCreateOptions|JSONEncodable<RESTPostAPIChannelMessageJSONBody>|FileBodyEncodable<RESTPostAPIChannelMessageJSONBody>} options The options to provide
151151
* @returns {Promise<Message>}
152152
* @example
153153
* // Send a basic message
@@ -174,18 +174,21 @@ class ChannelManager extends CachedManager {
174174
* .catch(console.error);
175175
*/
176176
async createMessage(channel, options) {
177-
let messagePayload;
177+
let payload;
178178

179179
if (options instanceof MessagePayload) {
180-
messagePayload = options.resolveBody();
180+
payload = await options.resolveBody().resolveFiles();
181+
} else if (isFileBodyEncodable(options)) {
182+
payload = options.toFileBody();
183+
} else if (isJSONEncodable(options)) {
184+
payload = { body: options.toJSON(), files: [] };
181185
} else {
182-
messagePayload = MessagePayload.create(this, options).resolveBody();
186+
payload = await MessagePayload.create(this, options).resolveBody().resolveFiles();
183187
}
184188

185189
const resolvedChannelId = this.resolveId(channel);
186190
const resolvedChannel = this.resolve(channel);
187-
const { body, files } = await messagePayload.resolveFiles();
188-
const data = await this.client.rest.post(Routes.channelMessages(resolvedChannelId), { body, files });
191+
const data = await this.client.rest.post(Routes.channelMessages(resolvedChannelId), payload);
189192

190193
return resolvedChannel?.messages._add(data) ?? new (getMessage())(this.client, data);
191194
}

packages/discord.js/src/managers/MessageManager.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const { Collection } = require('@discordjs/collection');
44
const { makeURLSearchParams } = require('@discordjs/rest');
5+
const { isFileBodyEncodable, isJSONEncodable } = require('@discordjs/util');
56
const { Routes } = require('discord-api-types/v10');
67
const { DiscordjsTypeError, ErrorCodes } = require('../errors/index.js');
78
const { Message } = require('../structures/Message.js');
@@ -230,14 +231,18 @@ class MessageManager extends CachedManager {
230231
const messageId = this.resolveId(message);
231232
if (!messageId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'message', 'MessageResolvable');
232233

233-
const { body, files } = await (
234-
options instanceof MessagePayload
235-
? options
236-
: MessagePayload.create(message instanceof Message ? message : this, options)
237-
)
238-
.resolveBody()
239-
.resolveFiles();
240-
const data = await this.client.rest.patch(Routes.channelMessage(this.channel.id, messageId), { body, files });
234+
let payload;
235+
if (options instanceof MessagePayload) {
236+
payload = await options.resolveBody().resolveFiles();
237+
} else if (isFileBodyEncodable(options)) {
238+
payload = options.toFileBody();
239+
} else if (isJSONEncodable(options)) {
240+
payload = { body: options.toJSON(), files: [] };
241+
} else {
242+
payload = await MessagePayload.create(this, options).resolveBody().resolveFiles();
243+
}
244+
245+
const data = await this.client.rest.patch(Routes.channelMessage(this.channel.id, messageId), payload);
241246

242247
const existing = this.cache.get(messageId);
243248
if (existing) {

packages/discord.js/src/structures/interfaces/TextBasedChannel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class TextBasedChannel {
156156
/**
157157
* Sends a message to this channel.
158158
*
159-
* @param {string|MessagePayload|MessageCreateOptions} options The options to provide
159+
* @param {string|MessagePayload|MessageCreateOptions|JSONEncodable<RESTPostAPIChannelMessageJSONBody>|FileBodyEncodable<RESTPostAPIChannelMessageJSONBody>} options The options to provide
160160
* @returns {Promise<Message>}
161161
* @example
162162
* // Send a basic message

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { MessagePort, Worker } from 'node:worker_threads';
55
import { ApplicationCommandOptionAllowedChannelType, MessageActionRowComponentBuilder } from '@discordjs/builders';
66
import { Collection, ReadonlyCollection } from '@discordjs/collection';
77
import { BaseImageURLOptions, ImageURLOptions, RawFile, REST, RESTOptions, EmojiURLOptions } from '@discordjs/rest';
8-
import { Awaitable, JSONEncodable } from '@discordjs/util';
8+
import { Awaitable, FileBodyEncodable, JSONEncodable } from '@discordjs/util';
99
import { WebSocketManager, WebSocketManagerOptions } from '@discordjs/ws';
1010
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
1111
import {
@@ -2234,7 +2234,12 @@ export class Message<InGuild extends boolean = boolean> extends Base {
22342234
): InteractionCollector<MappedInteractionTypes<InGuild>[ComponentType]>;
22352235
public delete(): Promise<OmitPartialGroupDMChannel<Message<InGuild>>>;
22362236
public edit(
2237-
content: MessageEditOptions | MessagePayload | string,
2237+
content:
2238+
| FileBodyEncodable<RESTPostAPIChannelMessageJSONBody>
2239+
| JSONEncodable<RESTPostAPIChannelMessageJSONBody>
2240+
| MessageEditOptions
2241+
| MessagePayload
2242+
| string,
22382243
): Promise<OmitPartialGroupDMChannel<Message<InGuild>>>;
22392244
public equals(message: Message, rawData: unknown): boolean;
22402245
public fetchReference(): Promise<OmitPartialGroupDMChannel<Message<InGuild>>>;
@@ -4258,7 +4263,12 @@ export class ChannelManager extends CachedManager<Snowflake, Channel, ChannelRes
42584263
private constructor(client: Client<true>, iterable: Iterable<RawChannelData>);
42594264
public createMessage(
42604265
channel: Exclude<TextBasedChannelResolvable, PartialGroupDMChannel>,
4261-
options: MessageCreateOptions | MessagePayload | string,
4266+
options:
4267+
| FileBodyEncodable<RESTPostAPIChannelMessageJSONBody>
4268+
| JSONEncodable<RESTPostAPIChannelMessageJSONBody>
4269+
| MessageCreateOptions
4270+
| MessagePayload
4271+
| string,
42624272
): Promise<OmitPartialGroupDMChannel<Message>>;
42634273
public fetch(id: Snowflake, options?: FetchChannelOptions): Promise<Channel | null>;
42644274
}
@@ -4787,7 +4797,14 @@ export class VoiceStateManager extends CachedManager<Snowflake, VoiceState, type
47874797
export type Constructable<Entity> = abstract new (...args: any[]) => Entity;
47884798

47894799
export interface SendMethod<InGuild extends boolean = boolean> {
4790-
send(options: MessageCreateOptions | MessagePayload | string): Promise<Message<InGuild>>;
4800+
send(
4801+
options:
4802+
| FileBodyEncodable<RESTPostAPIChannelMessageJSONBody>
4803+
| JSONEncodable<RESTPostAPIChannelMessageJSONBody>
4804+
| MessageCreateOptions
4805+
| MessagePayload
4806+
| string,
4807+
): Promise<Message<InGuild>>;
47914808
}
47924809

47934810
export interface PinnableChannelFields {
@@ -6250,7 +6267,7 @@ export interface GuildEmojiEditOptions {
62506267

62516268
export interface GuildStickerCreateOptions {
62526269
description?: string | null;
6253-
file: AttachmentPayload | BufferResolvable | JSONEncodable<AttachmentBuilder> | Stream;
6270+
file: AttachmentPayload | BufferResolvable | Stream;
62546271
name: string;
62556272
reason?: string;
62566273
tags: string;

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ import {
229229
UserSelectMenuComponent,
230230
UserSelectMenuInteraction,
231231
Webhook,
232+
MessageBuilder,
232233
} from './index.js';
233234

234235
// Test type transformation:
@@ -452,15 +453,9 @@ client.on('messageCreate', async message => {
452453
assertIsMessage(client.channels.createMessage(channel, {}));
453454
assertIsMessage(client.channels.createMessage(channel, { embeds: [] }));
454455

455-
const attachment = new AttachmentBuilder('file.png');
456456
const embed = new EmbedBuilder();
457-
assertIsMessage(channel.send({ files: [attachment] }));
458457
assertIsMessage(channel.send({ embeds: [embed] }));
459-
assertIsMessage(channel.send({ embeds: [embed], files: [attachment] }));
460-
461-
assertIsMessage(client.channels.createMessage(channel, { files: [attachment] }));
462458
assertIsMessage(client.channels.createMessage(channel, { embeds: [embed] }));
463-
assertIsMessage(client.channels.createMessage(channel, { embeds: [embed], files: [attachment] }));
464459

465460
if (message.inGuild()) {
466461
expectAssignable<Message<true>>(message);
@@ -3033,14 +3028,11 @@ await guildScheduledEventManager.edit(snowflake, { recurrenceRule: null });
30333028
});
30343029
}
30353030

3036-
await textChannel.send({
3037-
files: [
3038-
new AttachmentBuilder('https://example.com/voice-message.ogg')
3039-
.setDuration(2)
3040-
.setWaveform('AFUqPDw3Eg2hh4+gopOYj4xthU4='),
3041-
],
3042-
flags: MessageFlags.IsVoiceMessage,
3043-
});
3031+
await textChannel.send(
3032+
new MessageBuilder()
3033+
.setContent(':)')
3034+
.addAttachments(attachment => attachment.setFileData(':)').setFilename('smiley.txt')),
3035+
);
30443036

30453037
await textChannel.send({
30463038
files: [

0 commit comments

Comments
 (0)