简体   繁体   中英

Sending an Embed to a different channel (Discord JS)

I am trying to send an embed to a different channel not to the channel where the command was executed but I am getting an error "Cannot access 'bot' before initialization" I dont know what i did wrong tbh bc ive declared everything on the top of the index.js.

 case "alert":
        let text = message.content.replace(prefix + "alert", "")
        const alertembed = new Discord.MessageEmbed()
        .setTitle("**Embed Title**")
        .setDescription(text)
        bot.channels.find("carts").send(alertembed)
        embed.Message.react("💸")
        embed.Message.react("💰")
const Discord = require("discord.js")
const bot = new Discord.Client();
const ms = require("ms")
const fs = require("fs")


var version = "1.0"

const config = require("./config.json")
let prefix = config.prefix;
const token = config.token;

bot.on("ready", () =>{
    console.log("Succesfully started the tools bot");
})

bot.on("message", message=>{

   let args = message.content.substring(prefix.length).split(" ");



   switch(args[0]){
...

Alright, based on the information you've provided I see a few problems, but the error message isn't what I'm expecting for them. These are certainly issues, but I'm not sure it's the issue.

In v12 most collections are replaced by manager objects that contain the collection named cache . This includes message.channels. So to access the channels cache collection you would need to use:

message.channels.cache.find()

This however still has a problem because using find based on a string was deprecated in v11 and removed in v12. You must pass in a function that returns true if it's what you want.

bot.channels.cache.find(ch => ch.name === "carts")

Update: One other thing I noticed.. embed.Message.react("") I don't see a variable named embed. I think you meant do this from the send.

bot.channels.cache.find(ch => ch.name === "carts").send(alertembed).then(sent => {
    sent.react("💸")
    sent.react("💰")
});

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