#TypeError: Cannot read properties of undefined (reading 'send')

1 messages · Page 1 of 1 (latest)

high totem
#

if i code the command in test.js command file it sends an error message and if i code the command in index.js it sends the message as i want

i would like to code it in test.js because i want to be organised

index.js command is working
const embed = new EmbedBuilder()
.setColor(16777215)
.setTitle('test')
.setDescription('description')
.setTimestamp()
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;

const { commandName } = interaction;
const channel = client.channels.cache.get('1010253296218406952')

if (commandName === 'test') {

        channel.send({ embeds: [embed] });
        await interaction.reply({content: "Message envoyé", ephemeral: true});

}

test.js command is not working
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, MessageFlags } = require('discord.js');
const { SlashCommandBuilder } = require('discord.js');
const { Client, Collection, GatewayIntentBits, MessageComponentInteraction } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

module.exports = {
data: new SlashCommandBuilder()
.setName('test')
.setDescription('description'),

    async execute(interaction) {

        const channel = client.channels.cache.get('1010255880597540954');
        const embed = new EmbedBuilder()
        .setColor(16777215)
        .setTitle('Test')
        .setDescription('Description')
        .setTimestamp();

        channel.send({ embeds: [embed] });

},

};

upper aurora
#
const embed = new EmbedBuilder()
.setColor(16777215)
.setTitle('test')
.setDescription('description')
.setTimestamp()
client.on('interactionCreate', async interaction => {
    if (!interaction.isChatInputCommand()) return;

    const { commandName } = interaction;
    const channel = client.channels.cache.get('1010253296218406952')

    if (commandName === 'test') {

            channel.send({ embeds: [embed] });
            await interaction.reply({content: "Message envoyé", ephemeral: true});
} 

**test.js** command is not working
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, MessageFlags } = require('discord.js');
const { SlashCommandBuilder } = require('discord.js');
const { Client, Collection, GatewayIntentBits, MessageComponentInteraction } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });


module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('description'),

        async execute(interaction) {

            const channel = client.channels.cache.get('1010255880597540954');
            const embed = new EmbedBuilder()
            .setColor(16777215)
            .setTitle('Test')
            .setDescription('Description')
            .setTimestamp();

            channel.send({ embeds: [embed] });

    },
};
#

This is covered in the guide

reef jackalBOT
#

Suggestion for @high totem:
guide Creating Your Bot: Individual command files
read more

high totem
#

interaction.reply is working fine but i can't use channel.send and there's nothing about it in the guide

upper aurora
high totem
#

|mLucas#5516 in #info triggered an interaction.
TypeError: Cannot read properties of undefined (reading 'send')
at Object.execute (C:\Users\lucas\Desktop\bot\commands\test.js:21:12)
at Client.<anonymous> (C:\Users\lucas\Desktop\bot\index.js:80:23)
at Client.emit (node:events:525:35)
at InteractionCreateAction.handle (C:\Users\lucas\Desktop\bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:81:12)
at module.exports [as INTERACTION_CREATE] (C:\Users\lucas\Desktop\bot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\lucas\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
at WebSocketShard.onPacket (C:\Users\lucas\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:481:22)
at WebSocketShard.onMessage (C:\Users\lucas\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:321:10)
at WebSocket.onMessage (C:\Users\lucas\Desktop\bot\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:513:28)

upper aurora
#

Then the channel id is wrong, what intents do you have?

high totem
#

i'm sure 100% the id is correct because it's working in index.js

#

but the .send is undefined

#

TypeError: Cannot read properties of undefined (reading 'send')

#

@upper aurora

upper aurora
#

Ok change it to a fetch instead and see what happens

high totem
#

same error again

upper aurora
high totem
#

line 21 is channel.send that's what i'm talking about

high totem
#

@upper aurora

upper aurora
#

That channel Id sinply cannot be correct...

What guild is it supposed to be in, have you tried checking for it in guild?

#

Wait.

#

Where are you getting client from in test.js?

#

F...you can't make a new client and it to work, that's not how DJS works

#

@high totem use interaction.client