Sending embed to channel in discord.js not working

Viewed 1679

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
1 Answers

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

Related