节点版本: v16.13.1
Discord.js版本: 13.3.1
TypeError: targetchannel.send is not a function如何修复此错误?
代码:
fs.readdir('channels', function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
targetchannel = client.channels.fetch(fs.readFileSync('channels/' + file,{encoding:'utf8'}))
console.log(targetchannel)
targetchannel.then((value) => {
targetchannel.send("asdra")
});
});
});日志的目标通道=允诺{<待定>}
发布于 2021-12-13 06:48:51
修复
我当时很傻。机器人在登录之前调用了.send。
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
if (!fs.existsSync('channels/')) {
fs.mkdirSync('channels');
}
fs.readdir('channels', async function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(async function (file) {
targetchannel = await client.channels.fetch(fs.readFileSync('channels/' + file,{encoding:'utf8'}))
console.log(targetchannel)
targetchannel.send("asdra")
});
});});发布于 2021-12-12 12:12:28
"TypeError:.send不是一个函数“意味着通道没有正确定义或者没有被找到。
在您的情况下,您需要等待,因为.fetch是一个异步方法。
targetchannel = await client.channels.fetch()编辑:作者修复了它,并有另一个问题。
发布于 2021-12-12 16:30:30
targetChannel仍然是悬而未决的承诺。使用value.send()代替,因为value是已解决的承诺
targetchannel.then((value) => {
value.send("asdra")
})https://stackoverflow.com/questions/70323139
复制相似问题