How do I send a message in a seperate channel from a slash command?

I've been trying to do this but can't seem to figure out how to get the client into the slash command. Any help would be greatly appreciated. Below is a snippet of my imports and some code which is trying to send the message, however failing because client is undefined. Imports:
const { EmbedBuilder, SlashCommandBuilder, ButtonStyle, ButtonBuilder } = require('discord.js');
const path = require('path');
const config = require(path.join(__dirname, '..', '..', 'configuration.json'));
const { EmbedBuilder, SlashCommandBuilder, ButtonStyle, ButtonBuilder } = require('discord.js');
const path = require('path');
const config = require(path.join(__dirname, '..', '..', 'configuration.json'));
Code:
const channelID = '1135231042287042561';
const channel = client.channels.cache.get(channelID);

if (channel && channel.isText()) {
const exampleEmbed = new EmbedBuilder()
.setColor('5865F2')
.setTitle("New Script")
.setDescription(`A new script has been posted by <@${interaction.user.id}>!`)
.addFields(
{"name": "Script URL", "value": "[Open the script here](https://raw.githubusercontent.com/" + DataURL + ")", "inline": true,},
{"name": "Script Name", "value": data.name, "inline": true,},
{"name": "Script ID", "value": random})
const confirm = new ButtonBuilder()
.setCustomId('delete')
.setLabel('Delete Script')
.setStyle(ButtonStyle.Danger);

const row = new MessageActionRow()
.addComponents(confirm)

channel.send({ embeds: [exampleEmbed], components: [row] })
.then(() => console.log('Message sent with the "delete" button!'))
.catch(console.error);
}
const channelID = '1135231042287042561';
const channel = client.channels.cache.get(channelID);

if (channel && channel.isText()) {
const exampleEmbed = new EmbedBuilder()
.setColor('5865F2')
.setTitle("New Script")
.setDescription(`A new script has been posted by <@${interaction.user.id}>!`)
.addFields(
{"name": "Script URL", "value": "[Open the script here](https://raw.githubusercontent.com/" + DataURL + ")", "inline": true,},
{"name": "Script Name", "value": data.name, "inline": true,},
{"name": "Script ID", "value": random})
const confirm = new ButtonBuilder()
.setCustomId('delete')
.setLabel('Delete Script')
.setStyle(ButtonStyle.Danger);

const row = new MessageActionRow()
.addComponents(confirm)

channel.send({ embeds: [exampleEmbed], components: [row] })
.then(() => console.log('Message sent with the "delete" button!'))
.catch(console.error);
}
2 Replies
d.js toolkit
d.js toolkit2y ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Daar Archival (Autocorrect)
Error setting data: TypeError: Cannot read properties of undefined (reading 'channels') at c:\Users\User\OneDrive\Desktop\Documents\Bot\slashcmds\script\set.js:68:28 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: 'TypeError: Cannot read properties of undefine…ions (node:internal/process/task_queues:95:5)', message: 'Cannot read properties of undefined (reading 'channels')'}
Want results from more Discord servers?
Add your server