简体   繁体   English

将嵌入发送到不同的频道 (Discord JS)

[英]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.我正在尝试将嵌入发送到不同的频道,而不是发送到执行命令的频道,但我收到错误“初始化之前无法访问'bot'”我不知道我做错了什么,我在顶部声明了所有内容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 .在 v12 中,大多数 collections 被包含名为cache的集合的管理器对象替换。 This includes message.channels.这包括 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.然而,这仍然存在问题,因为在 v11 中不推荐使用基于字符串的 find 并在 v12 中删除。 You must pass in a function that returns true if it's what you want.你必须传入一个 function 如果它是你想要的,它会返回 true。

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.更新:我注意到的另一件事.. embed.Message.react("")我没有看到名为 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("💰")
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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