Skip to content

Commit 01fc79f

Browse files
committed
refactor: Use MessageFlags.Ephemeral (#1592)
1 parent eb43fa3 commit 01fc79f

File tree

10 files changed

+32
-34
lines changed

10 files changed

+32
-34
lines changed

Diff for: code-samples/additional-features/cooldowns/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('node:fs');
22
const path = require('node:path');
3-
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
3+
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
44
const { token } = require('./config.json');
55

66
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
@@ -53,7 +53,7 @@ client.on(Events.InteractionCreate, async interaction => {
5353

5454
if (now < expirationTime) {
5555
const expiredTimestamp = Math.round(expirationTime / 1000);
56-
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
56+
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, flags: MessageFlags.Ephemeral });
5757
}
5858
}
5959

@@ -65,9 +65,9 @@ client.on(Events.InteractionCreate, async interaction => {
6565
} catch (error) {
6666
console.error(error);
6767
if (interaction.replied || interaction.deferred) {
68-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
68+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
6969
} else {
70-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
70+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
7171
}
7272
}
7373
});

Diff for: code-samples/additional-features/reloading-commands/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('node:fs');
22
const path = require('node:path');
3-
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
3+
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
44
const { token } = require('./config.json');
55

66
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
@@ -53,7 +53,7 @@ client.on(Events.InteractionCreate, async interaction => {
5353

5454
if (now < expirationTime) {
5555
const expiredTimestamp = Math.round(expirationTime / 1000);
56-
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
56+
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, flags: MessageFlags.Ephemeral });
5757
}
5858
}
5959

@@ -65,9 +65,9 @@ client.on(Events.InteractionCreate, async interaction => {
6565
} catch (error) {
6666
console.error(error);
6767
if (interaction.replied || interaction.deferred) {
68-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
68+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
6969
} else {
70-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
70+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
7171
}
7272
}
7373
});

Diff for: code-samples/creating-your-bot/command-deployment/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('node:fs');
22
const path = require('node:path');
3-
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
3+
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
44
const { token } = require('./config.json');
55

66
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
@@ -41,9 +41,9 @@ client.on(Events.InteractionCreate, async interaction => {
4141
} catch (error) {
4242
console.error(error);
4343
if (interaction.replied || interaction.deferred) {
44-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
44+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
4545
} else {
46-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
46+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
4747
}
4848
}
4949
});

Diff for: code-samples/creating-your-bot/command-handling/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('node:fs');
22
const path = require('node:path');
3-
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
3+
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
44
const { token } = require('./config.json');
55

66
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
@@ -41,9 +41,9 @@ client.on(Events.InteractionCreate, async interaction => {
4141
} catch (error) {
4242
console.error(error);
4343
if (interaction.replied || interaction.deferred) {
44-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
44+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
4545
} else {
46-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
46+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
4747
}
4848
}
4949
});

Diff for: code-samples/creating-your-bot/event-handling/events/interactionCreate.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Events } = require('discord.js');
1+
const { Events, MessageFlags } = require('discord.js');
22

33
module.exports = {
44
name: Events.InteractionCreate,
@@ -17,9 +17,9 @@ module.exports = {
1717
} catch (error) {
1818
console.error(error);
1919
if (interaction.replied || interaction.deferred) {
20-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
20+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
2121
} else {
22-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
22+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
2323
}
2424
}
2525
},

Diff for: guide/additional-features/cooldowns.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ if (timestamps.has(interaction.user.id)) {
7171

7272
if (now < expirationTime) {
7373
const expiredTimestamp = Math.round(expirationTime / 1_000);
74-
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
74+
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, flags: MessageFlags.Ephemeral });
7575
}
7676
}
7777
```

Diff for: guide/creating-your-bot/command-handling.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ client.on(Events.InteractionCreate, async interaction => {
123123
} catch (error) {
124124
console.error(error);
125125
if (interaction.replied || interaction.deferred) {
126-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
126+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
127127
} else {
128-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
128+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
129129
}
130130
}
131131
});

Diff for: guide/creating-your-bot/event-handling.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ client.on(Events.InteractionCreate, async interaction => {
3333
} catch (error) {
3434
console.error(error);
3535
if (interaction.replied || interaction.deferred) {
36-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
36+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
3737
} else {
38-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
38+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
3939
}
4040
}
4141
});
@@ -82,7 +82,7 @@ module.exports = {
8282
:::
8383
::: code-group-item events/interactionCreate.js
8484
```js
85-
const { Events } = require('discord.js');
85+
const { Events, MessageFlags } = require('discord.js');
8686

8787
module.exports = {
8888
name: Events.InteractionCreate,
@@ -101,9 +101,9 @@ module.exports = {
101101
} catch (error) {
102102
console.error(error);
103103
if (interaction.replied || interaction.deferred) {
104-
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
104+
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
105105
} else {
106-
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
106+
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
107107
}
108108
}
109109
},

Diff for: guide/popular-topics/collectors.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ collector.on('collect', i => {
143143
if (i.user.id === interaction.user.id) {
144144
i.reply(`${i.user.id} clicked on the ${i.customId} button.`);
145145
} else {
146-
i.reply({ content: `These buttons aren't for you!`, ephemeral: true });
146+
i.reply({ content: `These buttons aren't for you!`, flags: MessageFlags.Ephemeral });
147147
}
148148
});
149149

Diff for: guide/slash-commands/response-methods.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ Initially an interaction token is only valid for three seconds, so that's the ti
3232

3333
You may not always want everyone who has access to the channel to see a slash command's response. Previously, you would have had to DM the user to achieve this, potentially encountering the high rate limits associated with DM messages, or simply being unable to do so, if the user's DMs were disabled.
3434

35-
Thankfully, Discord provides a way to hide response messages from everyone but the executor of the slash command. This type of message is called an `ephemeral` message and can be set by providing `ephemeral: true` in the `InteractionReplyOptions`, as follows:
35+
Thankfully, Discord provides a way to hide response messages from everyone but the executor of the slash command. This is called an ephemeral message and can be set by providing `flags: MessageFlags.Ephemeral` in the `InteractionReplyOptions`, as follows:
3636

3737
```js {5}
3838
client.on(Events.InteractionCreate, async interaction => {
3939
if (!interaction.isChatInputCommand()) return;
4040

4141
if (interaction.commandName === 'ping') {
42-
await interaction.reply({ content: 'Secret Pong!', ephemeral: true });
42+
await interaction.reply({ content: 'Secret Pong!', flags: MessageFlags.Ephemeral });
4343
}
4444
});
4545
```
@@ -108,12 +108,10 @@ client.on(Events.InteractionCreate, async interaction => {
108108

109109
If you have a command that performs longer tasks, be sure to call `deferReply()` as early as possible.
110110

111-
Note that if you want your response to be ephemeral, you must pass an `ephemeral` flag to the `InteractionDeferReplyOptions` here:
112-
113-
<!-- eslint-skip -->
111+
Note that if you want your response to be ephemeral, utilize `flags` from `InteractionDeferReplyOptions` here:
114112

115113
```js
116-
await interaction.deferReply({ ephemeral: true });
114+
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
117115
```
118116

119117
It is not possible to edit a reply to change its ephemeral state once sent.
@@ -158,12 +156,12 @@ If you run this code you should end up having something that looks like this:
158156
</DiscordMessage>
159157
</DiscordMessages>
160158

161-
You can also pass an `ephemeral` flag to the `InteractionReplyOptions`:
159+
You can also pass the ephemeral flag to the `InteractionReplyOptions`:
162160

163161
<!-- eslint-skip -->
164162

165163
```js
166-
await interaction.followUp({ content: 'Pong again!', ephemeral: true });
164+
await interaction.followUp({ content: 'Pong again!', flags: MessageFlags.Ephemeral });
167165
```
168166

169167
<DiscordMessages>

0 commit comments

Comments
 (0)