Skip to content

Commit cee2599

Browse files
authored
refactor: Use withResponse (#1602)
1 parent 31bf979 commit cee2599

File tree

8 files changed

+52
-34
lines changed

8 files changed

+52
-34
lines changed

code-samples/popular-topics/reactions/14/awaiting-reactions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ client.on(Events.InteractionCreate, async interaction => {
1212
if (!interaction.isChatInputCommand()) return;
1313

1414
if (interaction.commandName === 'react-await') {
15-
const message = await interaction.reply({ content: 'Awaiting emojis...', fetchReply: true });
15+
const response = await interaction.reply({ content: 'Awaiting emojis...', withResponse: true });
16+
const { message } = response.resource;
1617
message.react('👍').then(() => message.react('👎'));
1718

1819
const collectorFilter = (reaction, user) => {

code-samples/popular-topics/reactions/14/basic-reacting.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ client.on(Events.InteractionCreate, async interaction => {
1414
const { commandName } = interaction;
1515

1616
if (commandName === 'react') {
17-
const message = await interaction.reply({ content: 'You can react with Unicode emojis!', fetchReply: true });
17+
const response = await interaction.reply({ content: 'You can react with Unicode emojis!', withResponse: true });
18+
const { message } = response.resource;
1819
message.react('😄');
1920
} else if (commandName === 'react-custom') {
20-
const message = await interaction.reply({ content: 'You can react with custom emojis!', fetchReply: true });
21+
const response = await interaction.reply({ content: 'You can react with custom emojis!', withResponse: true });
22+
const { message } = response.resource;
2123
message.react('123456789012345678');
2224
} else if (commandName === 'fruits') {
23-
const message = await interaction.reply({ content: 'Reacting with fruits!', fetchReply: true });
25+
const response = await interaction.reply({ content: 'Reacting with fruits!', withResponse: true });
26+
const { message } = response.resource;
27+
2428
message.react('🍎')
2529
.then(() => message.react('🍊'))
2630
.then(() => message.react('🍇'))

guide/additional-info/async-await.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ If you don't know how Node.js asynchronous execution works, you would probably t
101101
client.on(Events.InteractionCreate, interaction => {
102102
// ...
103103
if (commandName === 'react') {
104-
const message = interaction.reply({ content: 'Reacting!', fetchReply: true });
104+
const response = interaction.reply({ content: 'Reacting!', withResponse: true });
105+
const { message } = response.resource;
105106
message.react('🇦');
106107
message.react('🇧');
107108
message.react('🇨');
@@ -115,8 +116,10 @@ But since all of these methods are started at the same time, it would just be a
115116
client.on(Events.InteractionCreate, interaction => {
116117
// ...
117118
if (commandName === 'react') {
118-
interaction.reply({ content: 'Reacting!', fetchReply: true })
119-
.then(message => {
119+
interaction.reply({ content: 'Reacting!', withResponse: true })
120+
.then(response => {
121+
const { message } = response.resource;
122+
120123
message.react('🇦')
121124
.then(() => message.react('🇧'))
122125
.then(() => message.react('🇨'))
@@ -134,7 +137,8 @@ In this piece of code, the Promises are [chain resolved](https://developer.mozil
134137
client.on(Events.InteractionCreate, async interaction => {
135138
// ...
136139
if (commandName === 'react') {
137-
const message = await interaction.reply({ content: 'Reacting!', fetchReply: true });
140+
const response = await interaction.reply({ content: 'Reacting!', withResponse: true });
141+
const { message } = response.resource;
138142
await message.react('🇦');
139143
await message.react('🇧');
140144
await message.react('🇨');
@@ -148,7 +152,8 @@ It's mostly the same code, but how would you catch Promise rejections now since
148152
client.on(Events.InteractionCreate, async interaction => {
149153
if (commandName === 'react') {
150154
try {
151-
const message = await interaction.reply({ content: 'Reacting!', fetchReply: true });
155+
const response = await interaction.reply({ content: 'Reacting!', withResponse: true });
156+
const { message } = response.resource;
152157
await message.react('🇦');
153158
await message.react('🇧');
154159
await message.react('🇨');
@@ -169,23 +174,23 @@ Let's look at an example where you want to delete a sent reply.
169174
client.on(Events.InteractionCreate, interaction => {
170175
// ...
171176
if (commandName === 'delete') {
172-
interaction.reply({ content: 'This message will be deleted.', fetchReply: true })
173-
.then(replyMessage => setTimeout(() => replyMessage.delete(), 10_000))
177+
interaction.reply({ content: 'This message will be deleted.', withResponse: true })
178+
.then(response => setTimeout(() => response.resource.message.delete(), 10_000))
174179
.catch(error => {
175180
// handle error
176181
});
177182
}
178183
});
179184
```
180185

181-
The return value of a `.reply()` with the `fetchReply` option set to `true` is a Promise which resolves with the reply when it has been sent, but how would the same code with async/await look?
186+
The return value of a `.reply()` with the `withResponse` option set to `true` is a promise which resolves with <DocsLink path="InteractionCallbackResponse:Class" />, but how would the same code with async/await look?
182187

183188
```js {1,4-10}
184189
client.on(Events.InteractionCreate, async interaction => {
185190
if (commandName === 'delete') {
186191
try {
187-
const replyMessage = await interaction.reply({ content: 'This message will be deleted.', fetchReply: true });
188-
setTimeout(() => replyMessage.delete(), 10_000);
192+
const response = await interaction.reply({ content: 'This message will be deleted.', withResponse: true });
193+
setTimeout(() => response.resource.message.delete(), 10_000);
189194
} catch (error) {
190195
// handle error
191196
}

guide/additional-info/changes-in-v14.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ The following properties & methods have been moved to the `GuildAuditLogsEntry`
311311

312312
### Interaction
313313

314-
Whenever an interaction is replied to and one fetches the reply, it could possibly give an `APIMessage` if the guild was not cached. However, interaction replies now always return a discord.js `Message` object with `fetchReply` as `true`.
314+
Whenever an interaction is replied to and one fetches the reply, it could possibly give an `APIMessage` if the guild was not cached. However, interaction replies now always return an <DocsLink path="InteractionCallbackResponse:Class" /> with `withResponse` set to `true`.
315315

316316
The base interaction class is now `BaseInteraction`.
317317

guide/popular-topics/collectors.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ const collectorFilter = response => {
6666
return item.answers.some(answer => answer.toLowerCase() === response.content.toLowerCase());
6767
};
6868

69-
interaction.reply({ content: item.question, fetchReply: true })
70-
.then(() => {
71-
interaction.channel.awaitMessages({ filter: collectorFilter, max: 1, time: 30_000, errors: ['time'] })
69+
interaction.reply({ content: item.question, withResponse: true })
70+
.then(response => {
71+
response.resource.message.channel.awaitMessages({ filter: collectorFilter, max: 1, time: 30_000, errors: ['time'] })
7272
.then(collected => {
7373
interaction.followUp(`${collected.first().author} got the correct answer!`);
7474
})

guide/popular-topics/faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ If you're using [sharding](/sharding/), a specific shard's heartbeat can be foun
319319
The second, **Roundtrip Latency**, describes the amount of time a full API roundtrip (from the creation of the command message to the creation of the response message) takes. You then edit the response to the respective value to avoid needing to send yet another message:
320320
321321
```js
322-
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true });
322+
const sent = await interaction.reply({ content: 'Pinging...' });
323323
interaction.editReply(`Roundtrip latency: ${sent.createdTimestamp - interaction.createdTimestamp}ms`);
324324
```
325325

guide/popular-topics/reactions.md

+16-13
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ client.on(Events.InteractionCreate, async interaction => {
6060
const { commandName } = interaction;
6161

6262
if (commandName === 'react') {
63-
const message = await interaction.reply({ content: 'You can react with Unicode emojis!', fetchReply: true });
64-
message.react('😄');
63+
const response = await interaction.reply({ content: 'You can react with Unicode emojis!', withResponse: true });
64+
response.resource.message.react('😄');
6565
}
6666
});
6767
```
@@ -107,8 +107,8 @@ client.on(Events.InteractionCreate, async interaction => {
107107
const { commandName } = interaction;
108108

109109
if (commandName === 'react-custom') {
110-
const message = await interaction.reply({ content: 'You can react with custom emojis!', fetchReply: true });
111-
message.react('123456789012345678');
110+
const response = await interaction.reply({ content: 'You can react with custom emojis!', withResponse: true });
111+
response.resource.message.react('123456789012345678');
112112
}
113113
});
114114
```
@@ -158,7 +158,8 @@ Using `.find()`, your code would look something like this:
158158

159159
```js {3-4}
160160
if (commandName === 'react-custom') {
161-
const message = await interaction.reply({ content: 'You can react with custom emojis!', fetchReply: true });
161+
const response = await interaction.reply({ content: 'You can react with custom emojis!', withResponse: true });
162+
const message = response.resource.message;
162163
const reactionEmoji = message.guild.emojis.cache.find(emoji => emoji.name === 'blobreach');
163164
message.react(reactionEmoji);
164165
}
@@ -170,9 +171,9 @@ Using `.get()`, your code would look something like this:
170171

171172
```js {3-4}
172173
if (commandName === 'react-custom') {
173-
const message = await interaction.reply({ content: 'You can react with custom emojis!', fetchReply: true });
174+
const response = await interaction.reply({ content: 'You can react with custom emojis!', withResponse: true });
174175
const reactionEmoji = client.emojis.cache.get('123456789012345678');
175-
message.react(reactionEmoji);
176+
response.resource.message.react(reactionEmoji);
176177
}
177178
```
178179

@@ -189,8 +190,8 @@ client.on(Events.InteractionCreate, async interaction => {
189190
const { commandName } = interaction;
190191

191192
if (commandName === 'fruits') {
192-
interaction.reply('Reacting with fruits!');
193-
const message = await interaction.fetchReply();
193+
const response = await interaction.reply({ content: 'Reacting with fruits!', withResponse: true });
194+
const { message } = response.resource;
194195
message.react('🍎');
195196
message.react('🍊');
196197
message.react('🍇');
@@ -260,8 +261,9 @@ client.on(Events.InteractionCreate, async interaction => {
260261
const { commandName } = interaction;
261262

262263
if (commandName === 'fruits') {
263-
const message = await interaction.reply({ content: 'Reacting with fruits!', fetchReply: true });
264-
message.react('🍎')
264+
const response = await interaction.reply({ content: 'Reacting with fruits!', withResponse: true });
265+
266+
response.resource.message.react('🍎')
265267
.then(() => message.react('🍊'))
266268
.then(() => message.react('🍇'))
267269
.catch(error => console.error('One of the emojis failed to react:', error));
@@ -278,7 +280,8 @@ client.on(Events.InteractionCreate, async interaction => {
278280
const { commandName } = interaction;
279281

280282
if (commandName === 'fruits') {
281-
const message = await interaction.reply({ content: 'Reacting with fruits!', fetchReply: true });
283+
const response = await interaction.reply({ content: 'Reacting with fruits!', withResponse: true });
284+
const { message } = response.resource;
282285

283286
try {
284287
await message.react('🍎');
@@ -356,7 +359,7 @@ However, if you don't mind the order the emojis react in, you can take advantage
356359

357360
```js {3-8}
358361
if (commandName === 'fruits') {
359-
const message = await interaction.reply({ content: 'Reacting with fruits!', fetchReply: true });
362+
const message = await interaction.reply({ content: 'Reacting with fruits!' });
360363
Promise.all([
361364
message.react('🍎'),
362365
message.react('🍊'),

guide/slash-commands/response-methods.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,14 @@ await interaction.reply('Pong!');
200200
await interaction.deleteReply();
201201
```
202202

203-
Lastly, you may require the `Message` object of a reply for various reasons, such as adding reactions. You can use the `ChatInputCommandInteraction#fetchReply()` method to fetch the `Message` instance of an initial response:
203+
Lastly, you may require the `Message` object of a reply for various reasons, such as adding reactions. Pass `withResponse: true` to obtain the <DocsLink path="InteractionCallbackResponse:Class" />. You can then access the `Message` object like so:
204204

205-
<!-- eslint-skip -->
205+
```js
206+
const response = await interaction.reply({ content: 'Pong!', withResponse: true });
207+
console.log(response.resource.message);
208+
```
209+
210+
You can also use the `ChatInputCommandInteraction#fetchReply()` method to fetch the `Message` instance. Do note that this incurs an extra API call in comparison to `withResponse: true`:
206211

207212
```js
208213
await interaction.reply('Pong!');

0 commit comments

Comments
 (0)