diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | bot.js | 68 | ||||
| -rw-r--r-- | package.json | 6 |
3 files changed, 45 insertions, 32 deletions
@@ -1,4 +1,5 @@ token.json node_modules yarn.lock -music/*.mp3
\ No newline at end of file +music/*.mp3 +package-lock.json
\ No newline at end of file @@ -7,41 +7,47 @@ * *********************************************/ const Discord = require('discord.js'); -const fs = require("fs"); +const fs = require('fs'); const client = new Discord.Client(); const config = require('./token.json'); -let botOwner = "242775871059001344"; -var voiceChannel = "734184921433899108"; -var prefix = "!"; +var botOwner = '242775871059001344'; +var voiceChannel = '734184921433899108'; +var prefix = '!'; + +client.login(config.token); function playAudio() { - let audio; const channel = client.channels.cache.get(voiceChannel); - if (!channel) return console.error("The channel does not exist!"); - - fs.readdir('./music', (err, files) => { - if (err) console.error(err); - audio = files[Math.floor(Math.random() * files.length)]; - return audio; - }); + if (!channel) return console.error('The channel does not exist!'); channel.join().then(connection => { - console.log("Connected to the voice channel."); - const dispatcher = connection.play("./music/" + audio); + console.log('Connected to the voice channel.'); + let files = fs.readdirSync('./music'); + let audio; - console.log("Project Jul-2020 Bot:\nNow playing " + audio); + while (true) { + audio = files[Math.floor(Math.random() * files.length)]; + console.log('Searching file...'); + if (audio.endsWith('.mp3')) { + break; + } + } + + const dispatcher = connection.play('./music/' + audio); + + console.log('Now playing ' + audio); let serviceChannel = client.channels.cache.get('606602551634296968'); - serviceChannel.send("**Project Jul-2020 Bot:**\nNow playing " + audio); + serviceChannel.send('**Project Jul-2020 Bot:**\nNow playing ' + audio); + }).catch(e => { console.error(e); }); } - client.on('ready', () => { - console.log("Bot is ready!") + console.log('Bot is ready!') console.log(`Logged in as ${client.user.tag}!`); console.log(`Prefix: ${prefix}`); - client.user.setStatus("invisible"); + client.user.setStatus('invisible'); playAudio(); }); @@ -50,9 +56,8 @@ client.on('message', async msg => { if (!msg.guild) return; if (!msg.content.startsWith(prefix)) return; if (![botOwner].includes(msg.author.id)) return; - const args = msg.content.slice(prefix.length).trim().split(/ +/g); - - let command = msg.content.split(" ")[0]; + + let command = msg.content.split(' ')[0]; command = command.slice(prefix.length); if (command == 'ping') { @@ -60,26 +65,27 @@ client.on('message', async msg => { } if (command == 'stop') { - await msg.reply("Powering off...") - console.log("Powering off..."); + await msg.reply('Powering off...') + console.log('Powering off...'); client.destroy(); process.exit(0); } if (command == 'join') { // Only try to join the sender's voice channel if they are in one themselves - playAudio(); + msg.reply('Joining voice channel.'); + playAudio(); } if (command == 'skip') { - //This feature might not make it in + } + if (command == 'leave') { const channel = client.channels.cache.get(voiceChannel); - if (!channel) return console.error("The channel does not exist!"); - console.log("Leaving voice channel."); + if (!channel) return console.error('The channel does not exist!'); + msg.reply('Leaving voice channel.') + console.log('Leaving voice channel.'); channel.leave(); } -}); - -client.login(config.token);
\ No newline at end of file +});
\ No newline at end of file diff --git a/package.json b/package.json index 571f522..8806178 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,15 @@ "version": "1.0.0", "main": "bot.js", "license": "MIT", + "scripts": { + "dev": "nodemon bot.js" + }, "dependencies": { "@discordjs/opus": "^0.3.2", "discord.js": "^12.2.0", "ffmpeg-static": "^4.2.5" + }, + "devDependencies": { + "nodemon": "^2.0.4" } } |
