javascript - 将嵌入发送到 discord.js 中的频道不起作用
问题描述
我正在尝试将嵌入发送到我的服务器中的特定文本频道,但我似乎无法让它工作。有任何想法吗?
const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const client = new Discord.Client({disableEveryone: true})
client.on("ready", async () => {
console.log(`${client.user.username} is online!`)
});
const channel = client.channels.cache.get('12345678912345');
const rulesEmbed = new Discord.MessageEmbed()
.setColor('#db5151')
.setTitle('test')
.setDescription('test')
channel.send(rulesEmbed);
client.login(botconfig.token);
错误信息:
TypeError: Cannot read property 'send' of undefined
at Object.<anonymous> (C:\loremipsum\index.js:30:9)
←[90m at Module._compile (internal/modules/cjs/loader.js:1185:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:1034:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:923:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)←[39m
←[90m at internal/main/run_main_module.js:17:47←[39m
解决方案
代码对我来说看起来是正确的。但我认为频道有问题。每个公会的频道 ID 都不同。您首先必须找出机器人所在的公会。您可以通过获取它来client.guilds
做到这一点,也可以像下面这样:(这种类似命令的结构在 discords.js 中很常见;这可能有助于进入它:https ://discordjs.guide/popular-topics /embeds.html )
client.on("message", message => {
if(message.content === "sendEmbed"){
const channel = message.guilds.cache.get('12345678912345');
if(channel) {
channel.send(rulesEmbed);
}
}
});
如果您还没有完成,请考虑查看此内容 :) https://discordjs.guide/popular-topics/embeds.html
推荐阅读
- python - Gcode解析问题?- for 循环字节和字符串
- php - 从具有特定长度的字符串创建数组
- c# - 使用 jenkins 在 Windows 服务器上部署 Angular 应用程序
- c# - 具有多个受影响行的 RETURNING 子句
- java - 错误:在 com.company.StudentGrade 类中找不到主方法,请将主方法定义为:public static void main(String[] args)
- python - Python 正则表达式匹配用引号括起来的关键字在另一个用引号括起来的关键字之后
- php - 在 PHP 中的 Group Drop Down 中获取标签的值和选定的选项
- ruby-on-rails - 使用单个模型错误进行设计和活动管理员
- kubernetes - 使用部署和 LoadBalancer 服务公开 Kubernetes 服务
- javascript - How to stop my Hamburger menu from jumping me to the top of my page on click.