Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Meteor卡在第二个服务器端收集方法上(使用discord.js)_Javascript_Mongodb_Meteor_Discord - Fatal编程技术网

Javascript Meteor卡在第二个服务器端收集方法上(使用discord.js)

Javascript Meteor卡在第二个服务器端收集方法上(使用discord.js),javascript,mongodb,meteor,discord,Javascript,Mongodb,Meteor,Discord,我正在尝试创建一个具有web界面的discord机器人,所以我决定将Meteor与discord.js一起使用 当我第二次在服务器端调用Mongo的方法时,我发现了一个问题(第一次可以正常工作),无论它是哪种方法(我使用remove()和findOne()进行测试),它在控制台上都不会出错 然后我尝试从mongo集合更改为array,这是可行的,但我希望使用collection与客户端共享数据 这是我的代码,当用户点击按钮并调用play()ins meteor方法时,一首歌将开始: server

我正在尝试创建一个具有web界面的discord机器人,所以我决定将Meteor与discord.js一起使用

当我第二次在服务器端调用Mongo的方法时,我发现了一个问题(第一次可以正常工作),无论它是哪种方法(我使用remove()和findOne()进行测试),它在控制台上都不会出错

然后我尝试从mongo集合更改为array,这是可行的,但我希望使用collection与客户端共享数据

这是我的代码,当用户点击按钮并调用play()ins meteor方法时,一首歌将开始:

server/main.js

import '../imports/queue.js';
import '../imports/bot.js';
import { Mongo } from 'meteor/mongo';

export const Queue = new Mongo.Collection('queue');
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");
        }
    })
}
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({});
      },
    })
}]);
导入/queue.js

import '../imports/queue.js';
import '../imports/bot.js';
import { Mongo } from 'meteor/mongo';

export const Queue = new Mongo.Collection('queue');
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");
        }
    })
}
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({});
      },
    })
}]);
导入/bot.js

import '../imports/queue.js';
import '../imports/bot.js';
import { Mongo } from 'meteor/mongo';

export const Queue = new Mongo.Collection('queue');
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");
        }
    })
}
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({});
      },
    })
}]);
client/main.js

import '../imports/queue.js';
import '../imports/bot.js';
import { Mongo } from 'meteor/mongo';

export const Queue = new Mongo.Collection('queue');
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");
        }
    })
}
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({});
      },
    })
}]);
来自控制台的结果

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

你能分享你的客户端代码吗?它实际上是在调用你的meteor方法吗?@jordanwillis当然!我只是更新我的帖子。你可以在上面找到它。谢谢:)