首页 > 解决方案 > 将嵌入发送到 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

标签: javascriptnode.jsdiscord.js

解决方案


代码对我来说看起来是正确的。但我认为频道有问题。每个公会的频道 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


推荐阅读