Search code examples
javascriptdiscord.jsmessageroles

Discord.js Sending a message to another channel


I currently have a working script that assigns roles based on the args that the poster inputs. What I am trying to do is have the bot send a message to the local channel so the poster knows that it worked and then send another message to another channel welcoming the user who got the roles to the server. This is what I have:

const { client } = require('../main.js');


module.exports = {
    name: 'roles',
    description: "This is the command to give new recruits their role!",
    args: true,
    usage: '<department> <@user>',
    execute(message, args){

        //variables are defined here

        const channel = client.channels.cache.get('the channel id');
        message.delete(); //This deletes the posters command message

        if (!message.mentions.users.size){
            return message.channel.send('Please make sure to tag someone!');
             
        else if(args[0] === 'bcso'){
            user.roles.add(bcso);
            user.roles.add(member);
            user.roles.add(whitelist)
            user.roles.remove(leoR);
            message.channel.send(`${user} is now a full member of the BCSO`);
            channel.send('Message goes here');
        }
   //rest of my code

But the error I'm getting in the console is: "TypeError: Cannot read property 'channels' of undefined"

Just to confirm I have an advanced code handler set up so this code is inside of a commands folder in its own file.

Ideas?


Solution

  • It appears that your imported client is not defined. Maybe because you don't export it correctly in your main.js file.

    Luckily you can simply use the message to find the channel. To do that you need to change client.channels.cache.get('the channel id'); to message.client.channels.cache.get('ID HERE');