我想在不和谐频道发送一个文件,机器人将读取这个文件

i want to send a file in discord channel and the bot will read this file

我是 discord 机器人的开发者。 我想知道如何让机器人读取我在不和谐服务器中发送的文件 例如:

!read (file)

并且机器人将记录文件的内容或将其发送为

message.channel.send(content)

我尝试了一些东西,但它们不起作用..

这是我试过的代码:

const Discord = require("discord.js");
const fs = require("fs")
module.exports.run = async(client, message, args, item) => {
 if(!message.member.hasPermission('ADMINISTRATOR')) return

      let useTips = new Discord.RichEmbed();
                useTips.setAuthor(message.author.tag, message.author.avatarURL)
                useTips.setDescription('Please Send A File')
                useTips.setColor(0xdb001d)
      message.channel.send(useTips)
      const filter = m => m.author.id === message.author.id;
      await message.channel.awaitMessages(filter, {max: 1, time: 30000,errors: ['time']})
                .then(collected => {
                        let collection = collected.first();
                        let file21 = collection.attachments;
                        message.channel.send(file21)
                          fs.readFile(file21, (err, data) => {
                          message.channel.send(data);
  });
                       
                })
              
                
}

但是没用.. 有人可以帮助我吗?

您无法将附件作为文件阅读。您需要从 url 获取此附件。 其使用节点请求库的第一种方式

    const Discord = require("discord.js");
    const fs = require("fs")
    const request = require('request');
    module.exports.run = async(client, message, args, item) => {
     if(!message.member.hasPermission('ADMINISTRATOR')) return

          let useTips = new Discord.RichEmbed();
                    useTips.setAuthor(message.author.tag, message.author.avatarURL)
                    useTips.setDescription('Please Send A File')
                    useTips.setColor(0xdb001d)
          message.channel.send(useTips)
          const filter = m => m.author.id === message.author.id;
          await message.channel.awaitMessages(filter, {max: 1, time: 30000,errors: ['time']})
                    .then(collected => {
                            let collection = collected.first();
                            if(!collection.attachments) return
                            request.get(collection.attachments.array()[0].url, function (error, response, body) {
                            if (!error && response.statusCode == 200) {
                              console.log(body)
                                }
                            });

                    })
                    .catch(console.error)
    }
}