簡體   English   中英

將嵌入發送到 discord.js 中的通道不起作用

[英]Sending embed to channel in discord.js not working

我正在嘗試將嵌入發送到我的服務器中的特定文本頻道,但我似乎無法讓它工作。 有任何想法嗎?

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

代碼對我來說看起來是正確的。 但我認為頻道有問題。 每個公會的頻道 ID 都不同。 您首先必須找出機器人所在的公會。 您可以通過從client.guilds獲取它來做到這一點,或者您可以這樣做:(這種類似命令的結構在 discords.js 中很常見;這可能有助於進入它: https://discordjs.guide/流行主題/嵌入。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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM