I am trying to send an announcement to the channel but I receive this error:
TypeError: Cannot read properties of undefined (reading 'send')
The script:
const Discord = require("discord.js");
const clients = new Discord.Client({ intents: 14087});
const allow = require("./admin/announceAllow.json")
module.exports = {
name: 'announce',
description: "announce stuff to announcement server",
execute(message, args, cmdName, extraString, status, reason,user,id, prefix,lang){
if(cmdName == ""){
let err = new Discord.MessageEmbed()
.setTitle("There was an error while announcing")
.setDescription("Reason: `Empty string`")
.setColor("RED")
message.channel.send({embeds:[err]})
}else
if(!(cmdName == "")){
var toAnnounce = cmdName.replace("announce ","")
var toSend = "929580453554434089"
var splitToAnnounce = toAnnounce.split(",,")
console.log(`${splitToAnnounce[0].replace("|n", "\n")}\n${splitToAnnounce[1].replace("|n", "\n")}\n${splitToAnnounce[2].replace("|n", "\n")}`)
if(allow[message.author.id + ""]){
try{
let announce = new Discord.MessageEmbed()
.setTitle(`${splitToAnnounce[0].replace("|n", "\n")}`)
.setDescription(`${splitToAnnounce[1].replace("|n", "\n")}`)
.setColor(`${splitToAnnounce[2].replace("|n", "\n")}`)
clients.channels.cache.get(`${toSend}`).send({embeds:[announce]})
}catch(e){
let err = new Discord.MessageEmbed()
.setTitle("There was an error while announcing")
.setDescription("Possible reason: `Bot don't have permission, bot script outdated, bot error, user input error, color error`\n\nUsage: <prefix> announce `title,,content,,color`\ntip: use `|n` for new line\ncolor: `RED,BLUE,GREEN` etc")
.setColor("RED")
message.channel.send({embeds:[err]})
console.log(e)
}
}else
if(!(allow[message.author.id + ""])){...........
Anyone, please explain why did this error occur and how to fix it
Have a check on the channel ID in toSend.
I have recreated a simply version of this code, and able to get the same error while the channel ID is inputed wrongly. If your command handler is working fine, the problem is mostly come up at the clients.channels.cache.get... part.
Above TypeError it should be showing you which line of code is being run as occuring the error.