如何获取在语音通道中花费的时间?

How to get the time spent speaking in a voice channel?

我想获取语音频道播放视频的位置。 我正在使用 ytdl-core 来播放视频,但我不知道 ytdl-core 是否有可以做到这一点的功能。我需要以 hour:minute:second 格式返回的位置。如果我需要另一个图书馆来做那也没关系。

这是我用来播放视频的代码:

const commando = require('discord.js-commando');
const ytdl = require('ytdl-core');
var args_res;
var streamOptions;
var servers = {};

function Play(connection, message) {
  var server = servers[message.guild.id];
  server.dispatcher = connection.playStream(ytdl(server.queue[0], {
    filter: "audioonly"
  }), streamOptions);

  server.queue.shift();
  server.dispatcher.on("end", function() {
    if (server.queue[0]) {
      Play(connection, message);
    } else {
      connection.disconnect();
    }
  });
}

class JoinChannelCommand extends commando.Command {
    constructor(client) {
      super(client, {
        name: 'play',
        group: 'audio',
        memberName: 'play',
        description: 'Joins the same voice channel that the person who typed the commands is in, and plays the song.'
      });
    }

    async run(message, args) {
      if (message.member.voiceChannel) {
        if (!message.guild.voiceConnection) {
          if (!servers[message.guild.id]) {
            servers[message.guild.id] = {
              queue: []
            }
          }
          message.member.voiceChannel.join()
            .then(connection => {
              var server = servers[message.guild.id];
              args_res = args.split(" ");
              streamOptions = {
                seek: 0,
                volume: (args_res[1] / 100)
              };

              server.queue.push(args_res[0]);
              console.log(server.queue[0] + "  " + args_res[1]);
              Play(connection, message);
            });
        }
      } else {
        message.reply("You must be in a voice channel for me to join!");
      }
    }

    module.exports = JoinChannelCommand;

提前致谢。

您可以使用 StreamDispatcher.time,因为每次播放新视频时您都会重置调度程序。
该值可以转换为您喜欢的时间格式:例如,如果您有 ms = 15600000,您可以通过这样做获得您的时间格式:

var ms = 15600000,
  hr = Math.floor(ms / 1000 / 60 / 60),
  mn = Math.floor(ms / 1000 / 60 % 60),
  ss = Math.round(ms / 1000 % 60 % 60);
  
function format(number = 0) {
  return `0${number}`.slice(-2);
}

console.log(`${hr}:${format(mn)}:${format(ss)}`);