Neon Eviscerator
Neon Eviscerator

Reputation: 33

Sending message to specific channel, not working

Having trouble sending a bot message on a specific channel. I very simply want to have the bot send a message to #general when it activates, to let everyone know it's working again.

In the bot.on function, I've tried the classic client.channels.get().send(), but the error messages I'm getting show that it thinks client is undefined. (It says "cannot read property 'channels' of undefined")

bot.on('ready', client => {
    console.log("MACsim online")
    client.channels.get('#general').send("@here");
})

The bot crashes immediately, saying: Cannot read property 'channels' of undefined

Upvotes: 2

Views: 354

Answers (3)

Gugu72
Gugu72

Reputation: 2218

You need to get the guild / server by ID, and then the channel by ID, so your code will be something like :

const discordjs = require("discord.js");
const client = new discordjs.Client();

bot.on('ready', client => {
console.log("MACsim online")
    client.guilds.get("1836402401348").channels.get('199993777289').send("@here");
})

Edit : added client call

Upvotes: 0

Jakye
Jakye

Reputation: 6625

The ready event doesn't pass any client parameter. To get a channel by name use collection.find():

client.channels.find(channel => channel.name == "channel name here");

To get a channel by ID you can simply do collection.get("ID") because the channels are mapped by their IDs.

client.channels.get("channel_id");

Here is an example I made for you:

const Discord = require("discord.js");
const Client = new Discord.Client();

Client.on("ready", () => {
   let Channel = Client.channels.find(channel => channel.name == "my_channel");
   Channel.send("The bot is ready!");
});

Client.login("TOKEN");

Upvotes: 1

Steam gamer
Steam gamer

Reputation: 217

The ready event does not pass a parameter. To gain the bot's client, simply use the variable that you assigned when you created the client.
From your code, it looks like the bot variable, where you did const bot = new Discord.Client();.
From there, using the bot (which is the bot's client), you can access the channels property!

Upvotes: 0

Related Questions