Say Command - PT2
Simple say command, but anonymous.
In this tutorial of the /say command, we will teach you how to make the command anonymous, hiding the author who ran the command.
Before continuing on with this tutorial, please follow the beginning of Say Command so we can get the basic structure of our command.
Now that we have gotten our code in check, it should look like this:
const { SlashCommandBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName('say')
.setDescription('Say a message, as the bot')
.addStringOption(option => (
option,
.setName('message')
.setDescription('The message to say')
.setRequired(true)
)),
run: async ({ client, interaction }) => {
const message = interaction.options.getString("message");
},
};Before you jump the gun and reply to the interaction with the message, there is a few things we need to do.
So, you can go ahead and add our interaction.reply function, but do it as shown below:
interaction.reply({ content: ``, ephemeral: })By itself this command will fail because you are not providing it with its needed data, but I am going to explain a bit before we jump back into the code.
About Objects
This form of interaction reply function takes in an object, or "properties". Say for example a car has properties such as "color" and "weight". All the properties are the same from car to car, but the property values are different for each car.
In this example, interaction.reply({}) can take in multiple properties, such as components, embeds, ephemeral, and content.
TL;DR > Objects are forms of variables/functions that take in multiple values.
About Ephemeral
Ephemeral is a fancy way of saying "author-only" or "only visible by author" (At least in Discord terms). This will response to an interaction with a message that only the author can see, and nobody else.
This is the perfect way of removing the author command data from Slash Commands, and we can use this to make anonymous messages.
Back to Command
Back in our command, we need to fill the values of interaction.reply({}), so we will do the following:
interaction.reply({ content: `Message Sent`, ephemeral: true })This will send a reply message (that only we the author can see) as our interaction reply, because you must reply to an interaction.
Our command should now look like:
const { SlashCommandBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName('say')
.setDescription('Say a message, as the bot')
.addStringOption(option => (
option,
.setName('message')
.setDescription('The message to say')
.setRequired(true)
)),
run: async ({ client, interaction }) => {
const message = interaction.options.getString("message");
interaction.reply({ content: `Message Sent`, ephemeral: true })
},
};But this will not send our message, so to do this, we need to send a message to the interaction channel using a separate function: interaction.channel.send().
So, if we add this to our command, it should look similar to:
const { SlashCommandBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName('say')
.setDescription('Say a message, as the bot')
.addStringOption(option => (
option,
.setName('message')
.setDescription('The message to say')
.setRequired(true)
)),
run: async ({ client, interaction }) => {
const message = interaction.options.getString("message");
interaction.reply({ content: `Message Sent`, ephemeral: true })
await interaction.channel.send(message)
},
};We will also add await to the front of this function, so it waits for all other functions to be executed before running.
Now your anonymous /say command is finished!
Last updated