Need help sending an echo to a channel without it being a reply to a slash command.

The title says it all. I want the reply to be sent as an ephemeral message to the user in the channel they performed the slash command, and I want the message to echo into another text channel. I've been at this all day and haven't had any luck. Here's what I've got code wise at the moment but I've tried just about everything I can find.
13 Replies
d.js toolkit
d.js toolkit13mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Collywog
Collywog13mo ago
const { SlashCommandBuilder, ChannelType } = require("discord.js"); module.exports = { data: new SlashCommandBuilder() .setName("message") .setDescription("Send a message as the bot.") .addStringOption(option => option.setName('title') .setDescription('Enter the title for your message') .setRequired(false)) .addStringOption(option => option.setName('message') .setDescription('Enter the message for the bot to send') .setRequired(true)) .addChannelOption(option => option.setName('channel') .setDescription('The channel to echo into')), //Bot actions async execute(interaction, client) { //get the user input fields and set them to a variable if (!interaction.isChatInputCommand()) return; let title = "" + interaction.options.getString('title') + ""; let userMessage = interaction.options.getString('message'); let channelId = interaction.options.getString('channel'); //format the bot message depending on if a title was included or not let botMessage = ""; if (title != ""+ null + ""){ botMessage = title + "\n" + userMessage; }else{ botMessage = userMessage; } //check if a channel id was used if(channelId == null){ const message = await interaction.reply({ content: "A channel is required. Please select a channel option." }) } console.log(title, userMessage, channelId) const message = await interaction.channel.reply({

});
},
}; the message rn won't send to anything and I know that, I've just been at this all day and that was an attempt to check syntax.
duck
duck13mo ago
this code should receive an error about option types since you use getString on a channel option consider using getChannel (which returns a GuildChannel) and just using <TextChannel>.send() you could also use <SlashCommandChannelOptionBuilder>.addChannelTypes() to restrict the channel option to text based channels
Collywog
Collywog13mo ago
I'm sorry, I'm still new to this. How would the syntax for <TextChannel>.send() look after the ephemeral reply to the channel? would that be the channel id that the get channel command grabs?
duck
duck13mo ago
getChannel returns a GuildChannel object, not the channel id on the assumption the channel is a text channel, this is the object you would call send on
Collywog
Collywog13mo ago
so in my code if I were to change channelId from a getString to a getChannel then put in channelId.send(botMessage); before the reply in theory that should do what I'm looking for?
const { SlashCommandBuilder, ChannelType } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("message")
.setDescription("Send a message as the bot.")
.addStringOption((option) =>
option
.setName("title")
.setDescription("Enter the title for your message")
.setRequired(false)
)
.addStringOption((option) =>
option
.setName("message")
.setDescription("Enter the message for the bot to send")
.setRequired(true)
)
.addChannelOption((option) =>
option.setName("channel")
.setDescription("The channel to echo into")
.addChannelTypes(ChannelType.GuildText)
.setRequired(true)
),

//Bot actions
async execute(interaction, client) {
//get the user input fields and set them to a variable
if (!interaction.isChatInputCommand()) return;

let title = "**" + interaction.options.getString("title") + "**";
let userMessage = interaction.options.getString("message");
let channelId = interaction.options.getChannel("channel");

//format the bot message depending on if a title was included or not
let botMessage = "";

if (title != "**" + null + "**") {
botMessage = title + "\n" + userMessage;
} else {
botMessage = userMessage;
}

//check if a channel id was used
if (channelId == null) {
const message = await interaction.reply({
content: "A channel is required. Please select a channel option.",
ephemeral: true
});
}

console.log(title, userMessage, channelId);
channelId.send(botMessage);


const message = await interaction.reply({
content: botMessage,
ephemeral: true
});
},
};
const { SlashCommandBuilder, ChannelType } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("message")
.setDescription("Send a message as the bot.")
.addStringOption((option) =>
option
.setName("title")
.setDescription("Enter the title for your message")
.setRequired(false)
)
.addStringOption((option) =>
option
.setName("message")
.setDescription("Enter the message for the bot to send")
.setRequired(true)
)
.addChannelOption((option) =>
option.setName("channel")
.setDescription("The channel to echo into")
.addChannelTypes(ChannelType.GuildText)
.setRequired(true)
),

//Bot actions
async execute(interaction, client) {
//get the user input fields and set them to a variable
if (!interaction.isChatInputCommand()) return;

let title = "**" + interaction.options.getString("title") + "**";
let userMessage = interaction.options.getString("message");
let channelId = interaction.options.getChannel("channel");

//format the bot message depending on if a title was included or not
let botMessage = "";

if (title != "**" + null + "**") {
botMessage = title + "\n" + userMessage;
} else {
botMessage = userMessage;
}

//check if a channel id was used
if (channelId == null) {
const message = await interaction.reply({
content: "A channel is required. Please select a channel option.",
ephemeral: true
});
}

console.log(title, userMessage, channelId);
channelId.send(botMessage);


const message = await interaction.reply({
content: botMessage,
ephemeral: true
});
},
};
for reference this is my updated code
duck
duck13mo ago
this will encounter an error unrelated to sending a normal message to the given channel but sure
Collywog
Collywog13mo ago
the error I'm getting is invalid form body, but I'm not sure where it's invalid
duck
duck13mo ago
care to share the whole error?
Collywog
Collywog13mo ago
Collywog
Collywog13mo ago
oh wait This works! thank you so much Duck T.T if there's an area I can give you kudos I'd love to
chewie 🌈
chewie 🌈13mo ago
We know he does a good job firShy