From ceb5b655061513c032300fc36606c863eb91ad6f Mon Sep 17 00:00:00 2001 From: Dalton Date: Wed, 5 Dec 2018 19:10:58 -0800 Subject: [PATCH] tweaks and trying to add a command --- .gitignore | 1 + bot.js | 110 ++++++++++++++++++++++++++++----------------------- package.json | 2 +- test.js | 9 +++++ 4 files changed, 72 insertions(+), 50 deletions(-) create mode 100644 test.js diff --git a/.gitignore b/.gitignore index f448302..e032c6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ auth.json node_modules package-lock.json +.idea \ No newline at end of file diff --git a/bot.js b/bot.js index cb00b95..fed8d72 100644 --- a/bot.js +++ b/bot.js @@ -11,17 +11,15 @@ var channel = null; // available commands function availableCommands() { - var commands = "Available commands: " + + return "Available commands: " + "\n**[SFW]** b.<*coinflip, 8ball, roll, compute, bunny, dankmemes, meirl, animemes, moe, moe-boys, hotguys , kgirls, kboys*>" + "\n**[NSFW]** b.<*lewd, ecchi, hentai, yaoi, yuri, neko, pokeporn, hgifs*>" + "\nFor more information on a command, you may use: *b.help *"; - - return commands; } // help commands function commandsHelp(requestedCommand) { - var commands = {"coinflip": "**coinflip:** Returns a result of heads or tails.", + var commands = {"coinflip": "**coinflip:** Returns a result of heads or tails.", "8ball": "**8ball:** Shakes an 8 ball and returns a result.", "roll": "**roll:** Rolls a number between 1-100 (or a specified range [ex: b.roll 50-100])", "compute": "**compute:** Uses WolframAlpha to process the question asked.", @@ -56,7 +54,7 @@ function commandsHelp(requestedCommand) { // coin flip function coinFlip() { - return (Math.floor(Math.random() * 2) == 0) ? 'heads' : 'tails'; + return (Math.floor(Math.random() * 2) === 0) ? 'heads' : 'tails'; } // 8 ball @@ -68,7 +66,7 @@ function eightBall() { 'Sorry, bucko.', 'Hell, yes.', 'Hell to the no.', 'The future is bleak.', 'The future is uncertain.', 'I would rather not say.', 'Who cares?', 'Possibly.', 'Never, ever, ever.', 'There is a small chance.', 'Yes!']; - + return answers[(Math.floor(Math.random() * answers.length))]; } @@ -86,7 +84,7 @@ function stupidQuestion() { 'Is that supposed to be a question?', 'Who asked such a dumb question?', 'How about a good question instead?', 'Only good questions please', 'Oh god, is that a question?', 'Are you dumb because that\'s a dumb question.']; - + return answers[(Math.floor(Math.random() * answers.length))]; } @@ -105,10 +103,10 @@ function imgurRequest(subreddit, page_max) var req = new XMLHttpRequest(); var returnText = ""; - - req.onreadystatechange = function() - { - if (req.readyState == 4 && req.status == 200) + + req.onreadystatechange = function() + { + if (req.readyState === 4 && req.status === 200) { if(req.responseText != "Not found") { @@ -116,9 +114,9 @@ function imgurRequest(subreddit, page_max) returnText = json.data[getRandomInt(0,json.data.length-1)].link; } } - } - - req.open("GET", request_url, false); + }; + + req.open("GET", request_url, false); req.setRequestHeader('Authorization', 'Client-ID ' + auth.imgur); req.send(); @@ -136,9 +134,9 @@ function subredditRequest(subreddit) req.onreadystatechange = function() { - if(req.readyState == 4 && req.status == 200) + if(req.readyState === 4 && req.status === 200) { - + var json = JSON.parse(req.responseText); while(true) { @@ -153,11 +151,12 @@ function subredditRequest(subreddit) } catch (error) // error parsing json { - continue; + // Ignore the error and continue. + // continue; <-- last line in the loop... so does nothing. } } } - } + }; req.open("GET", redditURL, false); req.send(); @@ -176,7 +175,7 @@ function redditbooruRequest(subreddit) req.onreadystatechange = function() { - if(req.readyState == 4 && req.status == 200) + if(req.readyState === 4 && req.status === 200) { var json = JSON.parse(req.responseText); var imageID = getRandomInt(0, json.length); @@ -199,7 +198,7 @@ function calculate(message) req.onreadystatechange = function() { - if(req.readyState == 4 && req.status == 200) + if(req.readyState === 4 && req.status === 200) { returnText = req.responseText; } @@ -212,7 +211,7 @@ function calculate(message) } // get random number with starting and ending number -function getRandomInt(min, max) +function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } @@ -249,19 +248,19 @@ function setupVoice(file, volume, voice_channel) // voice queue function handlePlayQueue(connection) { - if (playQueue.length == 0) + if (playQueue.length === 0) { return; } currentCommand = playQueue[0]; const dispatcher = connection.playFile(currentCommand) - dispatcher.on("end", () => + dispatcher.on("end", () => { playQueue.pop(); playVolume.pop(); - if(playQueue.length == 0) + if(playQueue.length === 0) { queueExists = false; if(channel != null) @@ -288,18 +287,18 @@ client.on('ready', () => { // commands client.on('message', message => { - if (message.content.substring(0, 2) == 'b.' || message.content.substring(0, 2) == 'B.') { + if (message.content.substring(0, 2).toUpperCase() === 'B.') { var args = message.content.substring(2).split(' '); var cmd = args[0]; - + switch(cmd) { // Reply commands case 'command': case 'commands': case 'help': - var message_content = message.content.substring(2).split(' '); - if(message_content[1] === undefined) + //var message_content = message.content.substring(2).split(' '); + if(message.content.substring(2).split(' ')[1] === undefined) { message.channel.send(availableCommands()); } @@ -363,7 +362,7 @@ client.on('message', message => { message.reply(noQuestion()); } break; - + // SFW Generic Image commands case 'bun': case 'bunny': @@ -372,7 +371,7 @@ client.on('message', message => { break; case 'dankmeme': case 'dankmemes': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('dankmemes') : imgurRequest('dankmemes', 5)); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? subredditRequest('dankmemes') : imgurRequest('dankmemes', 5)); break; case 'source': message.channel.send("BunnyBot's source code: https://git.dtam.pw/daniel/discord-bot-js"); @@ -383,19 +382,19 @@ client.on('message', message => { // SFW Anime Image commands case 'meirl': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('anime_irl') : imgurRequest('anime_irl', 5)); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? subredditRequest('anime_irl') : imgurRequest('anime_irl', 5)); break; case 'animemes': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('animemes') : imgurRequest('animemes', 5)); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? subredditRequest('animemes') : imgurRequest('animemes', 5)); break; case 'moe': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? redditbooruRequest('awwnime') : imgurRequest('awwnime', 5)); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? redditbooruRequest('awwnime') : imgurRequest('awwnime', 5)); break; case 'moe-boys': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('cuteanimeboys') : imgurRequest('cuteanimeboys', 1)); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? subredditRequest('cuteanimeboys') : imgurRequest('cuteanimeboys', 1)); break; case 'hotguys': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? redditbooruRequest('bishounen') : subredditRequest('bishounen')); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? redditbooruRequest('bishounen') : subredditRequest('bishounen')); break; case 'kgirls': var availableRequests = [ @@ -428,26 +427,40 @@ client.on('message', message => { message.channel.send(availableRequests[Math.floor(Math.random() * availableRequests.length)]); break; case 'hentai': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? imgurRequest('hentai', 5) : subredditRequest('hentai', 5)); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? imgurRequest('hentai', 5) : subredditRequest('hentai', 5)); break; case 'yaoi': message.channel.send(imgurRequest('yaoi', 5)); - break; + break; case 'yuri': message.channel.send(imgurRequest('yuri', 5)); break; case 'neko': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? imgurRequest('Nekomimi', 5) : subredditRequest('Nekomimi', 5)); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? imgurRequest('Nekomimi', 5) : subredditRequest('Nekomimi', 5)); break; case 'pokeporn': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('pokeporn') : redditbooruRequest('pokeporn')); + message.channel.send((Math.floor(Math.random() * 2) === 0) ? subredditRequest('pokeporn') : redditbooruRequest('pokeporn')); break; case 'hgifs': message.channel.send(subredditRequest('nsfwanimegifs', 5)); break; case 'hentaibondage': - message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('hentaibondage') : imgurRequest('hentaibondage', 5)); - break; + message.channel.send((Math.floor(Math.random() * 2) === 0) ? subredditRequest('hentaibondage') : imgurRequest('hentaibondage', 5)); + break; + + case 'summon': + var commandArgOne = message.content.substring(0, message.content.indexOf(' ')); + if(commandArgOne !== undefined && commandArgOne.length > 0) + { + var redditToSummon = message.content.substring(message.content.indexOf(' ')+1); + message.channel.send(message.author + ' summons ' + redditToSummon + '*\n'); + message.channel.send(subredditRequest(redditToSummon)) + } + else + { + message.reply("wut do i summon?"); + } + break; // Voice commands case 'join': @@ -465,28 +478,27 @@ client.on('message', message => { playQueue = []; break; case 'airhorn': - setupVoice('voice/mlg-airhorn.mp3', 0.4, message.member.voiceChannel); + setupVoice('voice/mlg-airhorn.mp3', 0.4, message.member.voiceChannel); break; case 'quiethorn': - setupVoice('voice/mlg-airhorn.mp3', 0.03, message.member.voiceChannel); + setupVoice('voice/mlg-airhorn.mp3', 0.03, message.member.voiceChannel); break; case 'weed': - setupVoice('voice/smoke-weed.mp3', 0.2, message.member.voiceChannel); + setupVoice('voice/smoke-weed.mp3', 0.2, message.member.voiceChannel); break; case 'damnson': - setupVoice('voice/damnson.mp3', 0.35, message.member.voiceChannel); + setupVoice('voice/damnson.mp3', 0.35, message.member.voiceChannel); break; case 'wombo': - setupVoice('voice/wombocombo.mp3', 0.06, message.member.voiceChannel); + setupVoice('voice/wombocombo.mp3', 0.06, message.member.voiceChannel); break; case 'cena': - setupVoice('voice/cena.mp3', 0.10, message.member.voiceChannel); + setupVoice('voice/cena.mp3', 0.10, message.member.voiceChannel); break; case 'triple': - setupVoice('voice/triple.mp3', 0.20, message.member.voiceChannel); + setupVoice('voice/triple.mp3', 0.20, message.member.voiceChannel); break; } - } }); diff --git a/package.json b/package.json index 354e552..ec61a1b 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,6 @@ "discord.js": "^11.2.1", "ffmpeg-binaries": "^3.2.2-3", "xmlhttprequest": "^1.8.0", - "node-opus": "^0.2.7" + "node-opus": "^0.2.7" } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..fdc1655 --- /dev/null +++ b/test.js @@ -0,0 +1,9 @@ +var assert = require('assert'); + +describe('Array', function() { + describe('#indexOf()', function() { + it('should return -1 when the value is not present', function(){ + assert.equal(-1, [1,2,3].indexOf(4)); + }); + }); +}); \ No newline at end of file