const Discord = require('discord.js'); const client = new Discord.Client(); const config = require("./config.json"); const prefix = config.prefix; const readline = require('readline'); var queue = new Map(); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); //login message client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); client.user.setActivity(`${prefix}help \|`, { type: 'LISTENING' }); client.user.setStatus('online'); }); client.on('message', message => { if (message.content.indexOf(prefix) !== 0) return; if (message.author.id != config.userid) return; const args = message.content.slice(prefix.length).trim().split(/ +/g); const command = args.shift().toLowerCase(); const serverQueue = queue.get(message.guild.id); if (command === 'play') { play(message, serverQueue) }; if (command === 'leave') { playSong(message.guild) }; }) async function play(message, serverQueue) { const args = message.content.split(" "); const voiceChannel = message.member.voice.channel; if (!voiceChannel) return message.reply("You must be in a voice channel!"); const song = { path: args[1], }; console.log('輸入路徑:' + song) if (!serverQueue) { const queueConstruct = { textChannel: message.channel, voiceChannel: voiceChannel, connection: null, songs: [], volume: config.volume, playing: true, }; queue.set(message.guild.id, queueConstruct); queueConstruct.songs.push(song); try { var connection = await voiceChannel.join(); queueConstruct.connection = connection; playSong(message.guild, queueConstruct.songs[0]); } catch (err) { console.log(err); queue.delete(message.guild.id) return message.channel.send("There was an error playing! " + err); } } else { serverQueue.songs.push(song); return console.log(`Has been added to the queue!`); } } function playSong(guild, song) { const serverQueue = queue.get(guild.id); if (!song) { serverQueue.voiceChannel.leave(); queue.delete(guild.id); return; } const dispatcher = serverQueue.connection.play(song.path) .on('speaking', status => { if (!status) { serverQueue.songs.shift(); playSong(guild, serverQueue.songs[0]); } }) .on('error', error => { console.log(error); serverQueue.songs.shift(); playSong(guild, serverQueue.songs[0]); }) dispatcher.setVolumeLogarithmic(serverQueue.volume / 100); } //login client.login(config.token); |
{ "prefix":"", "token":"", "volume":40, "userid":"" } |
{ "name": "musicbot", "version": "1.0.0", "description": "", "main": "play.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "discord.js": "^12.2.0", "node-opus": "^0.3.3", "opusscript": "0.0.7" } } |