node.js - 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);
解决方案
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");
推荐阅读
- python - 尝试连接到 SQL 数据库时 Python 脚本挂起
- r - 人口普查,由在同一地方出生的夫妇(有或没有孩子)组成的家庭(dplyr 方式)
- bash - 如何在 shell 脚本 ssh 中读取目标环境变量
- python - 使用 python 即 opencv 检测裁剪面部的运动模糊
- scala - How to recreate if else statements in Scala using objects?
- reactjs - 将 innerHTML 数据和输入数据保存到 firebase
- python - 将深度嵌套的 XML 解析为 pandas 数据框
- ruby-on-rails - 大虾:如何从表头迭代中的表进行迭代?
- ssl - 向多台服务器广播 rtmps 流
- c - 如果我用 SIGPIPE 终止进程,为什么 WIFSIGNALED 为假 + 为什么退出代码 256?