Reputation: 3
Im Trying To Send A Message Through DiscordJS
Can Someone Help Me
I Tried This Code
const channel = client.channels.cache.get('example discord guild');
channel.send('content');
But It Doesent Work
index.js
// Credetials
const { token } = require('./json/token.json');
const { guild } = require('./json/guild.json');
const { client } = require('./json/client.json')
// Init
const { Client, Intents } = require('discord.js');
const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_>
bot.login(guild);
console.log("Logged In As DeltaBOT");
const channel = client.channels.cache.get(guild);
channel.send('content');
Error
const channel = client.channels.cache.get(guild);
^
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.<anonymous> (/storage/emulated/0/Download/node/index.js:15:33)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
Upvotes: 0
Views: 468
Reputation: 27
The only issue you are mainly having by the first code you sent is that you seem to be confusing asyncronous code with sincronous one
Just making sure you understand the problem, as you can probably face it later on.
Looking at this code example you sent
const channel = client.channels.cache.get('example discord guild');
channel.send('content');
First thing is that it takes from cache, that usually only exists after having it fetched or any interaction on it (forget how it works exactly) so you would need to await the fetch or make something like that
const channel = client.channels.cache.get('example discord guild') || await client.channels.fetch(guild);
channel.send('content');
Also, you can't use a JSON as a client (that you did import) since the client that you want is a Class instance of the library, you use bot variable instead.
const { token } = require('./json/token.json');
const { guild } = require('./json/guild.json');
const { Client, Intents } = require('discord.js');
const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_>
const bot = await bot.login(guild);
console.log(`Logged in as ${client?.user?.username}`);
const channel = client.channels.cache.get(guild) || await client.channels.fetch(guild)
await channel.send({
content: 'content'
});
Upvotes: 0
Reputation: 86
You should wait for your client to login before attempting to send a message / fetching a channel. You can do this by using the ready event. For example:
bot.on('ready', async () => {
console.log('Logged in as: ' + bot.user.tag);
const channel = await bot.channels.fetch(guild);
await channel.send('content');
})
Another thing I noticed is that you use client instead of bot. client is a json object but you defined your discord bot object as the bot variable. So use the bot variable and not the client one.
Make sure that the guild is a valid guild ID. I don't know what is in your client json file but you don't appear to be using it.
Upvotes: 2