简体   繁体   English

无法在 discord.js 中发送嵌入消息

[英]Unable to send the embeded message in discord.js

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 .检查toSend中的频道 ID。 I have recreated a simply version of this code, and able to get the same error while the channel ID is inputed wrongly .我重新创建了此代码的简单版本,并且在错误输入通道 ID 时能够得到相同的错误 If your command handler is working fine, the problem is mostly come up at the clients.channels.cache.get... part.如果您的命令处理程序工作正常,则问题主要出现在clients.channels.cache.get...部分。 Above TypeError it should be showing you which line of code is being run as occuring the error.在 TypeError 上方,它应该向您显示发生错误时正在运行的代码行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM