Javascript 如何在NodeJS中读取消息

Javascript 如何在NodeJS中读取消息,javascript,node.js,Javascript,Node.js,我正在为Discord创建一个机器人,以流式传输我的广播/播客,但我被卡住了。我正在尝试让bot从./stations文件发送站点列表,如下面的代码所示 排队 if(命令=='stations'){ message.channel.createMessage(`);//此时卡住了。 我尝试过这种方法,但文本文件内容没有定义 {message.channel.createMessage(open(“./stations.txt”).readlines()); 因此,命令是!stations,它

我正在为Discord创建一个机器人,以流式传输我的广播/播客,但我被卡住了。我正在尝试让bot从./stations文件发送站点列表,如下面的代码所示

排队

if(命令=='stations'){
message.channel.createMessage(`);//此时卡住了。
我尝试过这种方法,但文本文件内容没有定义

{message.channel.createMessage(open(“./stations.txt”).readlines());
因此,命令是!stations,它应该返回txt文件中的stations列表

const Eris=require('Eris');
const client=new Eris(需要('./config.json').token{
maxShards:1
});
fs=需要('fs')
var stations=fs.readFileSync(“./stations.txt”{
“编码”:“utf-8”
});
client.connect();
client.on('ready',()=>{
console.log('Ready to go!')
})
client.on('messageCreate',message=>{
if(message.author.bot)返回;
if(message.content.startsWith(“!”){
let command=message.content.substring(1.split)(“”[0];
设args=message.content.substring(2+command.length);
如果(命令==‘站’){
message.channel.createMessage(`stacked AT THIS POINT`);
}else if(命令=='radio'){
如果(args=='')
return message.channel.createMessage(`请指定电台示例:*!radio**`);
if(需要('./stations.json')[args]){
如果(!message.member.voiceState)
return message.channel.createMessage(`:警告:**您需要在语音频道中。**`);
client.joinVoiceChannel(message.member.voiceState.channelID)。然后(vc=>{
如果(vc.playing)
vc.stopPlaying();
message.channel.createMessage(`:radio:You now streaming**${args}**.`);
play(需要('./stations.json')[args]);
})
}否则{
return message.channel.createMessage(`**找不到具有该名称的电台。**请确保其大写字母和拼写正确。请参阅固定消息以获取流列表。`);
}
}
}
})

非常感谢您的帮助。

在node.js中将文本文件读入变量的最简单方法是

fs.readFileSync(path, options).toString()
您已经在代码的顶部指定了
stations
变量,只需添加
.toString()
调用(最终添加进一步的文本处理,如
.replace(/\n/g,,)
以逗号替换换行符)并在需要时使用
stations
变量。您可以使用try/catch检查读取文件时是否有错误


参考:

使用stations var不起作用?不,否则我一定是做错了什么,我无法理解我做错了什么。好吧,确保您的服务器控制台中没有错误……我看不出任何错误您使用的是什么版本的node?有一个更好的解决方案,同样容易集成如果您的版本支持
async
/
wait
,则te作为当前接受的答案。指定
编码
选项时,不需要调用
.toString()
,因为这会导致返回值已经是字符串而不是缓冲区。