如何解决尝试发送消息时出现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。
最初,我在“文本权限”下为它提供了所有权限,但我也尝试为他提供admin privs。它仍然没有用。我在做什么错了?
解决方法
问题是这一行:
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')
,
const通道在此处为空。您需要确保在其中分配值。
channel.send("Test");
如果该值不是强制性的,则使用try-catch。
try {
channel.send("Test");
} catch(e) {
console.log(e)
}
请投票支持,如果有帮助,请回答。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。