在继续执行代码之前完全运行功能的问题

时间:2019-04-14 15:27:05

标签: discord discord.js

我想删除我的日志文件中具有给定UserID的最后一行,这就是为什么我需要等到读取器完全通过logsFile的原因。 我的输出看起来像这样:

[4:55 PM] Skifty: <unban nolog 477627076753752064
[4:55 PM] BOTMafiBot: nologging
[4:55 PM] BOTMafiBot: Mitsuko#1398 unbanned.
[4:55 PM] BOTMafiBot: [14/3/2019 // 15:37] Gebannt durch: Skifty#3967 User: Mitsuko#1398 mit UserID 477627076753752064 mit ID 1
[4:55 PM] BOTMafiBot: [14/3/2019 // 15:39] Gebannt durch: Skifty#3967 User: Mitsuko#1398 mit UserID 477627076753752064 mit ID 1

最后两条消息应该显示在未禁止的消息之前。

已经对该功能(由于仍在代码中)进行了Async / Await的操作,但仍无济于事。

client.on('message', async message => {
  if (mess.startsWith(prefix + "unban")) {
    try {
      var bansFile = "Bans.txt";
      var logsFile = "banLogs.txt";
      var fileLine;
      var bans = fs.readFileSync(bansFile, "utf-8");
      var logs = fs.readFileSync(logsFile, "utf-8");
      var rd;
      var userID = args.split(" ")[1];
      var User = client.users.get(`${userID}`);
      message.channel.send("nologging");
      var found = false;
      if (User == undefined) return message.channel.send("Bitte gib die UserID des zu entbannenden Accounts ein!");
      if (!bans.includes(User.id)) return message.channel.send(`User ${User.tag} ist nicht gebannt!`);

      async function read() {
        rd = readline.createInterface({
          input: fs.createReadStream(logsFile)
        });
        rd.on('line', function(line) {
          line = line.concat('\n');
          if (line.includes(User.id)) {
            fileLine = line;
            message.channel.send(fileLine);
          }
        });
      }

      await read();
      if (found === true) {
        logs = logs.replace(fileLile, "");
        fs.writeFileSync(logsFile, logs);
      }

      message.guild.unban(User);
      message.channel.send(`${User.tag} unbanned.`);
    } catch (error) {
      return message.channel.send(`Dezenter Fehler: ${error}`);
    }
  }
});

0 个答案:

没有答案