首页 > 解决方案 > Discord.js V12 Sending an embed to a channel in a different guild

问题描述

HI i was making a command where it sends an embed to a channel with an id however the command works normally when used in the guild where it has that channel but when the command is used in a different guild it throws error "TypeError: Cannot read property 'send' of undefined" is there any way to fix this? i want the command to be usable in other guilds as well

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

标签: node.jsdiscord.js

解决方案


Since every single channel on Discord has a different ID, you can't get two different channel objects with one ID. What you can do instead is use a channel's name (the channel will have to have the same name in both servers), or you can hard code both channels manually.

e.g.

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


推荐阅读