Issue with using .sendMessage in a Discord bot built with JavaScript

I’m developing a Discord bot and attempting to utilize the .sendMessage method to send a message. I want to avoid the bot sending a message automatically when I type, so I’m not utilizing bot.on('message', function(message) {});. However, I’m encountering an error stating that .sendMessage is not a function. Here’s my current code:

const Discord = require('discord.js');
const BotToken = 'your-token-here';
const bot = new Discord.Client();

bot.login(BotToken);

bot.sendMessage('your-server-id', 'Hello, world!');

I have installed discord.js using npm, assuming it was included in the package. Additionally, I’ve tried using .setStreaming, but it produces a similar error. I’ve followed the installation instructions from the discord.js documentation. Can someone help me identify what I might be overlooking?

hey Emma! the issue is sendMessage is no longer supported in discord.js v12+. Instead, you should use the send() method. Get the channel object first, then call channel.send('Hello, world!'). You may want to update your code and try again. hope this helps! :slight_smile:

The error you are encountering is pretty common if you’re transitioning between versions of discord.js. In the latest versions, the method to send messages is indeed changed. First, make sure you’ve declared your bot to be ready using bot.on('ready', () => {});. Then retrieve the specific channel using bot.channels.cache.get('channel-ID') and call send() method on that channel to send a message. This should solve the issue if everything else is set correctly in your configuration.