Truls
Truls

Reputation: 55

Discord bot send msg in specific channel

I know this has been asked before, but I couldn't find a solution that worked for me. But I want my discord bot to (when I write a command) send a message on a specific channel and that channel only.

Currently, I have only the basic files and folders in my workspace + a commands folder. The index file looks like this:

const discord = require('discord.js');
const config = require('./config.json');
const client = new discord.Client();
const prefix = '>';
const fs = require('fs');

client.commands = new discord.Collection();
const commandFiles = fs
 .readdirSync('./commands/')
 .filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
 const command = require(`./commands/${file}`);
 client.commands.set(command.name, command);
}

client.once('ready', () => {
 console.log(`${client.user.username} is online`);
});

client.on('message', (message) => {
 if (!message.content.startsWith(prefix) || message.author.bot) return;
 const args = message.content.slice(prefix.length).split(/ + /);
 const command = args.shift().toLowerCase();

 if (command === 'cmd') {
  client.commands.get('cmd').execute(message, args);
 }
});

client.login('CLIENT_ID');

and then there's a folder called "commands" at the same level as index.js, and it contains cmd.js which looks like this

const client = require('discord.js');

module.exports = {
 name: 'cmd',
 description: 'cmd',
 execute(message, args) {
  const channel = client.channels.cache.get('CHANNEL_ID').send('message');
 },
};

Upvotes: 0

Views: 476

Answers (1)

Lioness100
Lioness100

Reputation: 8412

client is not something you get from requiring the discord.js module. It is created at the beginning of your bot, and you can fetch your client instance through message.client

const channel = message.client.channels.cache.get('CHANNEL_ID').send('message');

Upvotes: 1

Related Questions