如何在歌曲后自动断开人声?

时间:2017-08-07 18:39:21

标签: javascript discord discord.js

我正在学习Discord.js,我希望在从VoiceChannel播放歌曲后让我的随机本地音乐机器人自动断开连接。我试图做到这一点,但这不起作用。

const Discord = require('discord.js');
const path = require('path');
const bot = new Discord.Client();
const ddiff = require('return-deep-diff');
const prefix = "<";
const fs = require("fs");

var musique = [
    "./musiques/test.wav",
    "./musiques/test.wav",
    "./musiques/test.wav",
    "./musiques/test.wav",
    "./musiques/test.wav",
    "./musiques/test.wav",
    "./musiques/1.mp3",
    "./musiques/2.mp3",
    "./musiques/3.wav"
]

var cover = [
   "./musiques/1.mp3",
   "./musiques/2.mp3" 
]

var impro = [
    "./musiques/3.wav"
]

var servers = {};
var guildid = {};

bot.commands = new Discord.Collection();

bot.on("error", (e) => console.error(e));
bot.on("warn", (e) => console.warn(e));
bot.on("debug", (e) => console.info(e));

bot.on('ready', function() {
    console.log("no")})

bot.on('ready', () => {
  bot.user.setGame("<help")
  bot.user.setStatus("online")
}),

bot.on("message", function (message) {
  if (message.author.equals(bot.user)) return;
  if (!message.content.startsWith(prefix)) return;
  if (message.content.startsWith(prefix)) {
    message.delete(100)
  }

  var args = message.content.substring(prefix.length).split(" ");


// Commandes
  switch (args[0]) {
    case "stop":
        var server = servers[message.guild.id];

        if (!message.guild.voiceConnection) message.channel.send("blah")

        if (message.guild.voiceConnection) message.guild.voiceConnection.disconnect();

        break;

    case "random":
        if (!message.member.voiceChannel) {
        message.channel.send('blah')
        }
        if (message.member.voiceChannel) {
        message.member.voiceChannel.join()
        .then (connection => {
        const stream = connection.playStream(musique[Math.floor(Math.random() * musique.length)])
        stream.once('end', () => bot.voiceConnections.get(guildid).disconnect())
        })
      } else {
      return;
      }

      break;
}})

bot.login("nono")

拜托,我需要一些帮助。有人说我需要“让函数存储语音连接并检查它是否为空”,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

检查作者是否在语音频道

if (!msg.member.voiceChannel)

检查机器人是否在语音通道中!

if (!msg.guild.voiceConnection)