Can someone please explain why its not sending the message?
Im currently testing smth so when you say ">send", My bot sends a message to a channel:
if (msg.content === prefix + "send") {
const channel = client.channels.cache.get('1137720568850956300');
channel.send('test succes');
}
the 1137720568850956300
is my channel id, but i keep getting this error:
TypeError: Cannot read properties of undefined (reading 'send')
Trust me its the correct id (i doubled checked) and idk why its not working
what surprises me is that i copy and pasted it from the discord.js guide thinking it would work but it doesnt. if you really want it heres my full code:
const {
Events,
EmbedBuilder,
Client,
GatewayIntentBits,
} = require('discord.js');
const greeting = [
'yo wsp',
'hi',
'hello muthafucka',
'stfu and talk with other people',
];
const lolo = greeting[Math.floor(Math.random() * greeting.length)];
const prefix = '>';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const EmbedReply = {
title: 'Do not ping this person.',
description: ' 🙏 hes ded because of the pings already 🙏',
color: '10038562',
footer: {
text: 'Current version: 0.0.1',
icon_url:
'https://cdn.discordapp.com/attachments/1141734104543539220/1141750630763987015/pooperson.jpg',
},
};
module.exports = {
name: Events.MessageCreate,
once: false,
execute(msg) {
if (msg.content === '<@1140642830302335098>') {
msg.reply(`${lolo}`);
}
if (msg.content === '<@1089518964360609792>') {
msg.reply({ content: `<@${msg.author.id}>`, embeds: [EmbedReply] });
}
if (msg.content === prefix + 'test') {
msg.reply('test succesful');
}
if (msg.content === prefix + 'send') {
const channel = client.channels.cache.get('1137720568850956300');
channel.send('test succes');
}
},
};
thanks in advance 😀
2
Answers
Chances are the channel simply isn’t cached at the time. If the result is undefined, try to fetch the channel. If that doesn’t work, the error will be more precise about your permissions.
In order for this to work, your current function must have the
async
modifier applied, i.e.async function(...) {}
orasync (...) => {}
, however you wrote it.It’s because you instantiate a new
client
and it’s not the one logged in and listening to theMessageCreate
event. You will need to remove that and get the client from themsg
variable: