我正在尝试使用Web界面创建一个不和谐机器人,所以我决定将Meteor与Discord.js一起使用 .

我第二次在服务器端调用Mongo的方法时遇到了问题(第一次工作正常)无论它是哪种方法(我用remove()和findOne()进行测试)它没有错误地卡在那里在控制台上 .

然后我尝试从mongo集合更改为数组,它的工作,但我想使用集合与客户端共享数据 .

这是我的代码,一首歌将在用户点击按钮并播放()ins meteor方法时启动:

server/main.js

import '../imports/queue.js';
import '../imports/bot.js';

import/queue.js

import { Mongo } from 'meteor/mongo';

export const Queue = new Mongo.Collection('queue');

import/bot.js

import { Meteor } from 'meteor/meteor';
import { Queue } from '../imports/queue';

if(Meteor.isServer) {
    const Discord = require('discord.js');
    const ytdl = require('ytdl-core');

    const token = 'my-token';
    const client = new Discord.Client();

    let streamSetting = { seek: 0, volume: 0.1 };

    voiceChannel = null;
    voiceConnection = null;
    dispatcher = null;

    // Discord bot preparation
    client.on('ready', () => {
    console.log('Unicot is running!');
    });

    client.on('message', m => {
        if (m.content === '>>hi') {
            if(voiceChannel == null) {
                //Get voiceChannel
                voiceChannel = client.channels.find(channel => channel.type === 'voice');

                //Join voiceChannel
                voiceChannel.join()
                    .then(connection => {
                        voiceConnection = connection;
                    })
                    .catch(console.error);
            }
        } else if(m.content === '>>bye') {
            if(voiceChannel != null) {
                //Leave voiceChannel
                voiceChannel.leave();
                voiceChannel = null;
            }
        }

    });

    client.login(token);

    //Dequeue Song
    function getNextSong() {
        console.log("getNextSong started");
        currentSong = Queue.findOne({});

        console.log("Got next song: "+currentSong.url);

        if(typeof currentSong != 'undefined') {
            //Play Stream
            playStream(currentSong);
        }
    }

    //Play Stream
    function playStream(currentSong) {
        if(dispatcher == null) {
            //stream song
            let stream = ytdl(currentSong.url, {filter : 'audioonly'});
            dispatcher = voiceConnection.playStream(stream,streamSetting);

            console.log("playing "+currentSong.url);

            dispatcher.on('end',function(currentSong) {
                if(dispatcher != null) {
                    console.log("Stream ended");
                    //Remove from queue
                    //currentSong = Queue.remove(currentSong);
                    console.log("Song removed");

                    dispatcher = null;
                    getNextSong();
                }
            })
        }
    }

    // Meteor Methods called from front-end
    Meteor.methods({
        'add': function(url) {
            Queue.insert({
                "url": url,
                "date": new Date()
            });

            playQueue.push({})
        },
        'play': function() {
            if(voiceConnection != null) {
                if(dispatcher == null) {
                    //No song playing
                    getNextSong();
                } else {
                    //Song is paused
                    dispatcher.resume();
                }
            }
        },
        'skip': function() {
            if(voiceConnection != null) {
                dispatcher = null;
                getNextSong();
            }
        },
        'pause': function() {
            if(voiceConnection != null) {
                dispatcher.pause();
            }
        },
        'remove': function(id) {
            console.log("removing");
        }
    })
}

client/main.js

import { Meteor } from 'meteor/meteor';
import angular from 'angular';
import angularMeteor from 'angular-meteor';

import { Queue } from '../imports/queue.js';

angular.module('unicot', [
  angularMeteor
]).controller('mainController',['$scope','$http',function($scope,$http) {
    /* Add song */
    $scope.add = function(url) {
      Meteor.call('add',url);
    }

    $scope.play = function() {
      Meteor.call('play');
    }

    $scope.pause = function() {
      Meteor.call('pause');
    }

    $scope.skip = function() {
      Meteor.call('skip');
    }

    $scope.helpers({
      queue() {
        return Queue.find({});
      },
    })
}]);

Result from console

I20170321-19:47:30.143(2)? Unicot is running!
I20170321-19:47:44.647(2)? getNextSong started
I20170321-19:47:44.648(2)? Got next song: https://www.youtube.com/watch?v=tPEE9ZwTmy0
I20170321-19:47:44.665(2)? playing https://www.youtube.com/watch?v=tPEE9ZwTmy0
I20170321-19:47:48.041(2)? Stream ended