2017-09-08 5 views
0

私は、ラジオ/ポッドキャストをストリームするためにディスディスプのボットを作成していますが、私は立ち往生しています。以下のコードからわかるように、./stationsファイルからステーションのリストを送信するようにボットを取得しようとしています。ラインからノードJSのメッセージを読む方法

if(command == 'stations') { 
    message.channel.createMessage(``); // Stuck at this point. 

私はこの方法を試してみましたが、テキストファイルの内容が定義されていません。

したがって、コマンドは!ステーションです。これは、ステーションのリストとともにtxtファイルから戻ってくるはずです。

const Eris = require('eris'); 
const client = new Eris(require('./config.json').token, { 
    maxShards: 1 
}); 
fs = require('fs') 
var stations = fs.readFileSync("./stations.txt", { 
    "encoding": "utf-8" 
}); 


client.connect(); 

client.on('ready',() => { 
    console.log('Ready to go!') 
}) 

client.on('messageCreate', message => { 
    if (message.author.bot) return; 
    if (message.content.startsWith('!')) { 
    let command = message.content.substring(1).split(" ")[0]; 
    let args = message.content.substring(2 + command.length); 

    if (command == 'stations') { 
     message.channel.createMessage(`STUCK AT THIS POINT`); 
    } else if (command == 'radio') { 
     if (args == '') 
     return message.channel.createMessage(`Please specify the radio station example: **!radio <station name>**`); 

     if (require('./stations.json')[args]) { 
     if (!message.member.voiceState) 
      return message.channel.createMessage(`:warning: **You need to be in a voice channel.**`); 
     client.joinVoiceChannel(message.member.voiceState.channelID).then(vc => { 
      if (vc.playing) 
      vc.stopPlaying(); 

      message.channel.createMessage(`:radio: You are now streaming **${args}**.`); 
      vc.play(require('./stations.json')[args]); 
     }) 
     } else { 
     return message.channel.createMessage(`**Cannot find a radio station with that name.** Make sure it has capitals and the correct spelling. See pinned messages for stream list.`); 

     } 
    } 
    } 
}) 

あなたの助けに感謝します。

+0

が動作しませんか? – Salketer

+0

いいえ、または私はここで何か間違っている必要があります、私は間違ってやっている私の頭の中で私の頭を得ることはできません。 – troxie

+0

よく、サーバーコンソールにエラーがないことを確認してください...何も間違っているとは思わない – Salketer

答えて

-1

Node.jsの中の変数にテキストファイルを読むための最も簡単な方法は、

fs.readFileSync(path, options).toString() 

であるあなたは既にだけ.toString()呼び出しを追加、あなたはstations変数を割り当てる上部にあなたのコードでこれを持っています(最終的には、改行をカンマで置き換えるために.replace(/\n/g, ', ')のようなテキスト処理を追加します)、必要に応じてstations変数を使用してください。 try/catchを使用して、ファイルの読み込みにエラーがあるかどうかを確認できます。

参考:ステーションのVARを使用してNodeJS - fs readfilesync

+1

'.toString()'呼び出しは、 'encoding'オプションが指定されているときは不要です。戻り値がすでにバッファではなく文字列であるためです。 –