【问题标题】:If a variable = content in a txt file如果变量 = txt 文件中的内容
【发布时间】:2018-05-23 12:56:01
【问题描述】:

所以我需要方法来完成这项工作:

if(variable.indexOf(textfilescontent)
    do this
else
    do this

我刚开始使用 discord.js 和 javaScript 不知道如何操作,所以如果有人知道如何操作,请回复此帖子(这是我最后一次尝试...)!

编辑:我正在尝试检查文本文件中是否包含与变量相同的文本

编辑:我尝试制作的代码将作为一个整体执行此操作:它将获取最新的 yt 频道视频(完成)作为 entry.link。之后它会(这是我不知道该怎么做的部分)检查一个txt文件是否具有相同的链接。如果它没有相同的链接,则意味着视频已更新=它将发布一条与其中的链接不一致的消息。如果是,它将再次获取链接并再次尝试相同的事情。(我知道如何做不和谐部分,但我不知道如何检查 txtfilecontent=variablecontent

【问题讨论】:

标签: javascript discord.js


【解决方案1】:

我不完全确定您在问什么,但我假设您想检查文件的内容是否位于变量中的某个位置。如果是这种情况,那么您可以这样做:

  (async () => {
     let InFile = new Promise(async (resolve, reject) => {
        let file = require('fs').readFileSync(__dirname + '/FILE_NAME.EXTENSION');

        if(String(VAR_NAME).indexOf(file) > -1) {
           resolve(this);
        }else {
           reject(this);
        }
     });

     InFile.then(async () => {
        console.log('It is in file...');
        // Code You Want To Execute If The File Is In The Varible
        // You May Or May Not Want To Use The "async" keyword depending on the code that goes in here
     }).catch(async () => {
        console.log('It isn\'t in file...');
        // Code You Want To Execute If The File Is Not In The Varible
        // You May Or May Not Want To Use The "async" keyword depending on the code that goes in here
     });
  })();

如果这不是您所要求的,请提前道歉。

编辑:在运行此代码之前,您必须在项目目录中的终端中键入“npm install --save fs”

【讨论】:

  • 感谢您的回答,我会尽快查看!
  • 这就是我需要的!但它不起作用......首先它打印它在文件中,然后它应该返回,但它打印它不在文件中?!
  • 这很奇怪,因为它非常适合我。如果节点不是最新的,请尝试更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多