Discord.js V12 将嵌入发送到不同公会中的频道

时间:2021-01-08 22:12:00

标签: node.js discord.js

您好,我正在创建一个命令,它向带有 id 的频道发送嵌入内容,但是该命令在拥有该频道的公会中使用时可以正常工作,但是当该命令用于不同的公会时,它会抛出错误“TypeError :无法读取未定义的属性“发送””有什么办法可以解决这个问题?我希望该命令也可以在其他公会中使用

const channel = message.guild.channels.cache.find(c => c.id === "746423099871985755")
                
                const exampleEmbed = new Discord.MessageEmbed()
                .setColor('#FFFF33')
                .setTitle(message.member.user.tag)
                .setAuthor(`Order ID: ${ticketID}`)
                .setDescription(order)
                .setThumbnail(message.author.avatarURL())
                .setTimestamp()
                .setFooter(`From ${message.guild.name} (${message.guild.id})`);
            
                channel.send(exampleEmbed);

3 个答案:

答案 0 :(得分:0)

由于 Discord 上的每个频道都有不同的 ID,因此您无法使用一个 ID 获得两个不同的频道对象。您可以做的是使用频道的名称(频道必须在两个服务器中具有相同的名称),或者您可以手动对两个频道进行硬编码。

例如

// using names
const channel = message.guild.channels.cache.find(c => c.name === "channel-name");

// hardcoding IDs
const channel = message.guild.channels.cache.find(c => c.id === "ID1" || c.id === "ID2");

答案 1 :(得分:0)

这也应该有效message.guild.channels.cache.get('GUILD_ID').send(embed);

答案 2 :(得分:0)

Message#guild 是当前公会。因此,如果您想将消息从另一个公会发送到某个频道,您需要从客户端的缓存中搜索该公会。你可以这样写:

const guild = message.client.guilds.cache.get("/* Your guild id */");
const channel = guild.channels.cache.get("/* Your channel id */");

你也可以做得更紧凑一些:

const channel = message.client.guilds.cache.get("/* Your guild id */").channels.cache.get("/* Your channel id */");

如果您想使用频道名称进行搜索:

const channel = message.client.guilds.cache.get("/* Your guild id */").channels.cache.find(c => c.name === "/* Channel name */");