node.js - 尝试发送消息时出现 UnhandledPromiseRejectionWarning
问题描述
我正在尝试通过 discord.js 发送消息,但出现以下错误:
(node:10328) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
这是我的代码:
// Init
const Discord = require("discord.js");
const bot = new Discord.Client();
const channel = bot.users.cache.get('4257');
// Vars
// Code
bot.on("ready", () => {
console.log("The Bot is ready!");
channel.send("Test");
});
// Login
bot.login(" "); // I will hide this.
怎么了?是通道变量上的 id 吗?我只是输入了我的机器人的 id,因为我不知道该输入什么。
起初我在“文本权限”下给了它所有的权限,但我也尝试给他管理员权限。它仍然没有工作。我究竟做错了什么?
解决方案
问题是这一行:
const channel = bot.users.cache.get('4257');
这是它的问题所在:
const channel = bot.users.cache // this returns a collection of users, you want channels.
.get('4257'); // this is a user discriminator, you want a channel ID
以下是解决方法:
const id = <ID of channel you want to send the message to>
const channel = bot.channels.cache.get(id)
// ...
channel.send('Test')
这是一个例子:
const channel = bot.channels.cache.get('699220239698886679')
channel.send('This is the #general channel in my personal Discord')
推荐阅读
- python - 字典验证中至少有一个键?
- python - 不和谐.py | 检查 2 条或更多消息
- python - 下载excel在浏览器中打开而不是直接下载?蟒蛇和烧瓶
- c# - 通过 SendRequestAsync 在正文中发送字符串
- html - 当我单击登录屏幕并返回主页时,我的 html 网站以正确的方式溢出
- r - 没有关于 x64 的 .o 文件的信息:关于使用 Rcpp 进行 R 包检查的注意事项
- python - django 使用 get_query 和搜索表单组合多个 ListView
- android - Android 上的 Chrome 从网站中查找颜色并将其用于操作栏。如何在使用 Java 的 Android 中使用 WebView 来实现这一点?
- oracle - 在执行程序以在 PL/SQL 中查找阶乘时出现 PLS-00103 错误
- haproxy - HA 代理重写和重定向