简体   繁体   中英

Sending embed to channel in discord.js not working

I'm trying to send an embed to a specific text channel in my server, and I can't seem to get it to work. Any ideas?

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);

Error message:

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

The code looks correct to me. But I believe that there is something wrong with the channel. The channel IDs are different for every guild. You first have to find out in which guild the bot is. You can either do that by getting it from client.guilds or you do it like the following: (This command-like structure is very common in discords.js; This might help to get into it: 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);
        }
    }
});

Consider looking at this if you haven't done yet:) https://discordjs.guide/popular-topics/embeds.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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