Make Discord Bot接收文件并将内容发送到频道

时间:2018-06-14 18:03:27

标签: javascript node.js discord.js

所以我想这样做,如果我这样做:

  1. 我在频道中使用!命令..
  2. (机器人要求提供文件)
  3. 我发送文件
  4. 然后,机器人应该读取文件并发送内容。

    我试过了:

    await message.channel.send("Send your file please..");
    
    const MSG1 = await message.channel.awaitMessages(msg => {
      const buffer = fs.readFileSync(`${msg.url}`);
    
      message.channel.send('Read the file! Fetching data...');
      message.channel.send(buffer)
    })
    

    await message.channel.send("Send your file please..");
    
    const MSG1 = await message.channel.awaitMessages(msg => {
      fs.readFile(Buffer.from(MSG1.attachments.first()), 'utf8', function(err, data) {
        if (err) throw err;
    
        console.log('Read the file! Fetching data...');
        console.log(data);
      })
    })
    

    await message.channel.send("Send your file please..");
    
    const MSG1 = await message.channel.awaitMessages(msg => {
      fs.readFile(Buffer.from(`${msg.attachments.first().attachment}`), 'utf8', function(err, data) {
        if (err) throw err;
    
        console.log('Read the file! Fetching data...');
        console.log(data);
      })
    })
    

    await message.channel.send("Send your file please..");
    
    const MSG1 = await message.channel.awaitMessages(msg => {
      let buffer = Buffer.from(msg.attachments.first().attachment)
    
      message.channel.send('Read the file! Fetching data...');
      message.channel.send(buffer)
    })
    

    但这些都不起作用。

1 个答案:

答案 0 :(得分:0)

我看到您正在使用TextChannel.awaitMessages(),就好像它具有回调参数一样,但是您传递的函数应该是filter。不用awaitMessages(function),而要使用awaitMessages().then(function)

话虽如此,我还建议仅在作者相同的情况下使用过滤器并获取附件:

let filter = m => m.author == message.author; //use that message only if the author is the same
message.channel.awaitMessages(filter, {max: 1}).then(msg => {
  let file = msg.attachments.first().file;
  fs.readFile(file, (err, data) => {
    msg.channel.send("Read the file! Fetching data...");
    msg.channel.send(data);
  });
});

应该可以,如果您有任何问题,请告诉我