将嵌入发送到discord.js中的频道不起作用

时间:2020-08-03 09:29:29

标签: javascript node.js discord.js

我正在尝试将嵌入内容发送到服务器中的特定文本通道,但似乎无法正常工作。有什么想法吗?

const botconfig = require("./botconfig.json");
const Discord = require("discord.js");

const client = new Discord.Client({disableEveryone: true})

client.on("ready", async () => {
    console.log(`${client.user.username} is online!`)
});

const channel = client.channels.cache.get('12345678912345');

const rulesEmbed = new Discord.MessageEmbed()
    .setColor('#db5151')
    .setTitle('test')
    .setDescription('test')
    
channel.send(rulesEmbed);

client.login(botconfig.token);

错误消息:

TypeError: Cannot read property 'send' of undefined
    at Object.<anonymous> (C:\loremipsum\index.js:30:9)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1185:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:1034:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:923:14)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m

1 个答案:

答案 0 :(得分:1)

代码对我来说似乎正确。但我认为该渠道存在问题。每个公会的频道ID都不相同。您首先必须找出该机器人所在的公会。您可以通过从client.guilds获取来做到这一点,也可以按照以下方式进行: (这种类似命令的结构在discords.js中很常见;这可能有助于进入它:https://discordjs.guide/popular-topics/embeds.html

client.on("message", message => {
    if(message.content === "sendEmbed"){
        const channel = message.guilds.cache.get('12345678912345');
        if(channel) {
            channel.send(rulesEmbed);
        }
    }
});

如果还没有考虑,请考虑一下:) https://discordjs.guide/popular-topics/embeds.html