Files
discord-bot-js/bot.js

438 lines
15 KiB
JavaScript

// requirements
const Discord = require('discord.js');
const auth = require('./auth.json');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
// voice setup
var playQueue = [];
var playVolume = [];
var queueExists = false;
var channel = null;
// available commands
function availableCommands() {
var commands = "Available commands: " +
"\n**[SFW]** b.<*coinflip, 8ball, compute, bunny, dankmemes, meirl, moe, moe-boys, hotguys , kgirls, kboys*>" +
"\n**[NSFW]** b.<*lewd, ecchi, hentai, yaoi, yuri, neko, hgifs*>" +
"\nFor more information on a command, you may use: *b.help <command>*";
return commands;
}
// help commands
function commandsHelp(requestedCommand) {
var commands = {"coinflip": "**coinflip:** Returns a result of heads or tails.",
"8ball": "**8ball:** Shakes an 8 ball and returns a result.",
"compute": "**compute:** Uses WolframAlpha to process the question asked.",
"bunny": "**bunny:** Posts a picture from imgur on r/rabbits",
"dankmemes": "**dankmemes:** Posts a picture from reddit/imgur on r/dankmemes",
"meirl": "**meirl:** Posts a picture from reddit/imgur on r/anime_irl",
"moe": "**moe:** Posts a picture from redditbooru/imgur on r/awwnime",
"moe-boys": "**moe-boys:** Posts a picture from reddit/imgur on r/cuteanimeboys",
"hotguys": "**hotguys:** Posts a picture from redditbooru/reddit on r/bishounen",
"kgirls": "**kgirls:** Posts a picture from redditbooru/imgur on r/kpics or reddit on r/kpopfap",
"kboys": "**kboys:** Posts a picture from imgur on r/kfanservice, r/kpecs, or r/cutekboys",
"lewd": "[NSFW] **lewd:** Posts a picture from redditbooru on r/pantsu",
"ecchi": "[NSFW] **ecchi:** Posts a picture from reddit/imgur on r/ecchi",
"hentai": "[NSFW] **hentai:** Posts a picture from reddit/imgur on r/hentai or imgur on r/sukebei",
"yaoi": "[NSFW] **yaoi:** Posts a picture from imgur on r/yaoi",
"yuru": "[NSFW] **yuri:** Post a picture from imgur on r/yuri",
"neko": "[NSFW] **neko:** Posts a picture from reddit/imgur on r/nekomimi",
"hgifs": "[NSFW] **hgifs:** Posts a gif from reddit on r/nsfwanimegifs"};
if (commands[requestedCommand] === undefined)
{
return "Command not found, try entering **b.commands** for a full list of commands";
}
else
{
return commands[requestedCommand];
}
}
// coin flip
function coinFlip() {
return (Math.floor(Math.random() * 2) == 0) ? 'heads' : 'tails';
}
// 8 ball
function eightBall() {
var answers = [
'Maybe.', 'Certainly not.', 'I hope so.', 'Not in your wildest dreams.',
'There is a good chance.', 'Quite likely.', 'I think so.', 'I hope not.',
'I hope so.', 'Never!', 'Fuhgeddaboudit.', 'Ahaha! Really?!?', 'Pfft.',
'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))];
}
// stupid question response
function stupidQuestion() {
var answers = [
'What kind of dumb question is that?', 'A real question please.',
'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))];
}
function noQuestion() {
var answers = ['it helps to ask a question...', 'question plz.', 'where is the question?'];
return answers[(Math.floor(Math.random() * answers.length))];
}
// imgur request
function imgurRequest(subreddit, page_max)
{
var multiSubreddit = subreddit.split(',');
var request_url = 'https://api.imgur.com/3/gallery/r/' + multiSubreddit[getRandomInt(0, multiSubreddit.length-1)] + '/time/' + getRandomInt(1,page_max);
var req = new XMLHttpRequest();
var returnText = "";
req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
if(req.responseText != "Not found")
{
var json = JSON.parse(req.responseText);
returnText = json.data[getRandomInt(0,json.data.length-1)].link;
}
}
}
req.open("GET", request_url, false);
req.setRequestHeader('Authorization', 'Client-ID ' + auth.imgur);
req.send();
return returnText;
}
// subreddit request
function subredditRequest(subreddit)
{
var multiSubreddit = subreddit.split(',');
var redditURL = "https://www.reddit.com/r/" + multiSubreddit[getRandomInt(0, multiSubreddit.length-1)] + "/.json?show=all&count=25&limit=100";
var req = new XMLHttpRequest();
var returnText = "";
req.onreadystatechange = function()
{
if(req.readyState == 4 && req.status == 200)
{
var json = JSON.parse(req.responseText);
while(true)
{
var i = getRandomInt(0,json.data.children.length-1);
try
{
if(json.data.children[i].data.post_hint === "image" || json.data.children[i].data.post_hint === "link" || json.data.children.data.post_hint === "rich:video")
{
returnText = json.data.children[i].data.url;
break;
}
}
catch (error) // error parsing json
{
continue;
}
}
}
}
req.open("GET", redditURL, false);
req.send();
return returnText;
}
// redditbooru request
function redditbooruRequest(subreddit)
{
var multiSubreddit = subreddit.split(',');
var url = "https://" + multiSubreddit[getRandomInt(0, multiSubreddit.length-1)] + ".redditbooru.com/images/?limit=1000";
var req = new XMLHttpRequest();
var returnText = "";
req.onreadystatechange = function()
{
if(req.readyState == 4 && req.status == 200)
{
var json = JSON.parse(req.responseText);
var imageID = getRandomInt(0, json.length);
returnText = json[imageID].cdnUrl;
}
}
req.open("GET", url, false);
req.send();
return returnText;
}
// wolframalpha computation
function calculate(message)
{
var url = "http://api.wolframalpha.com/v1/result?appid=" + auth.wolframalpha + "&i=" + encodeURIComponent(message);
var req = new XMLHttpRequest();
var returnText = "";
req.onreadystatechange = function()
{
if(req.readyState == 4 && req.status == 200)
{
returnText = req.responseText;
}
}
req.open("GET", url, false);
req.send();
return returnText;
}
// get random number with starting and ending number
function getRandomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// voice setup
function setupVoice(file, volume, voice_channel)
{
playQueue.unshift(file);
playVolume.unshift(volume);
if(!channel)
{
channel = voice_channel;
}
if(channel instanceof Discord.VoiceChannel)
{
if(!queueExists)
{
queueExists = true;
channel.join().then(connection =>
{
handlePlayQueue(connection);
});
}
}
else
{
playQueue.pop();
playVolume.pop();
channel = null;
}
}
// voice queue
function handlePlayQueue(connection)
{
if (playQueue.length == 0)
{
return;
}
currentCommand = playQueue[0];
const dispatcher = connection.playFile(currentCommand)
dispatcher.on("end", () =>
{
playQueue.pop();
playVolume.pop();
if(playQueue.length == 0)
{
queueExists = false;
if(channel != null)
{
channel.leave();
channel = null;
}
}
else
{
handlePlayQueue(connection);
}
});
dispatcher.setVolume(playVolume[0]);
}
// discord setup
const client = new Discord.Client();
client.on('ready', () => {
console.log('Bot is ready');
});
// commands
client.on('message', message => {
if (message.content.substring(0, 2) == 'b.' || message.content.substring(0, 2) == 'B.') {
var args = message.content.substring(2).split(' ');
var cmd = args[0];
switch(cmd)
{
// Reply commands
case 'command':
case 'commands':
message.channel.send(availableCommands());
break;
case 'help':
var message_content = message.content.substring(2).split(' ');
if(message_content[1] === undefined)
{
message.channel.send(availableCommands());
}
else
{
message.channel.send(commandsHelp(message_content[1]));
}
break;
case 'coinflip':
case 'coin':
message.channel.send(message.author + ' flipped a coin, it landed on **' + coinFlip() + '!**');
break;
case '8ball':
if(message.content.substring(8).length > 0)
{
message.channel.send(message.author + ' asked: *' + message.content.substring(8) + '*\n' +
'The magic 8 ball says: **' + eightBall() + '**');
}
else
{
message.reply(noQuestion());
}
break;
case 'compute':
case 'convert':
case 'calculate':
var message_content = message.content.substring(2).split(' ');
if(message_content[1].length > 0)
{
var response = calculate(message.content.substring(message.content.indexOf(' ')+1));
if(response == "")
{
response = stupidQuestion();
}
message.channel.send(message.author + ' wants to compute: *' + message.content.substring(message.content.indexOf(' ')+1) + '*\n' +
'The result is: **' + response + '**')
}
else
{
message.reply(noQuestion());
}
break;
// SFW Generic Image commands
case 'bun':
case 'bunny':
case 'bunnies':
message.channel.send(imgurRequest('rabbits',5));
break;
case 'dankmeme':
case 'dankmemes':
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");
break;
/*case 'testcommand':
message.channel.send(subredditRequest('Nekomimi', 5));
break;*/
// SFW Anime Image commands
case 'meirl':
message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('anime_irl') : imgurRequest('anime_irl', 5));
break;
case 'moe':
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));
break;
case 'hotguys':
message.channel.send((Math.floor(Math.random() * 2) == 0) ? redditbooruRequest('bishounen') : subredditRequest('bishounen'));
break;
case 'kgirls':
message.channel.send((Math.floor(Math.random() * 2) == 0) ? redditbooruRequest('kpics') : ((Math.floor(Math.random() * 2) == 0) ? imgurRequest('kpics', 5) : subredditRequest('kpopfap')));
break;
case 'kboys':
message.channel.send((Math.floor(Math.random() * 2) == 0) ? imgurRequest('kfanservice',1) : ((Math.floor(Math.random() * 2) == 0) ? imgurRequest('kpecs',1) : imgurRequest('cutekboys', 1)));
break;
// NSFW
case 'lewd':
message.channel.send(redditbooruRequest('pantsu'));
break;
case 'ecchi':
message.channel.send((Math.floor(Math.random() * 2) == 0) ? subredditRequest('ecchi') : imgurRequest('ecchi', 5));
break;
case 'hentai':
message.channel.send((Math.floor(Math.random() * 2) == 0) ? imgurRequest('hentai', 5) : ((Math.floor(Math.random() * 2) == 0) ? imgurRequest('Sukebei', 5) : subredditRequest('hentai', 5)));
break;
case 'yaoi':
message.channel.send(imgurRequest('yaoi', 5));
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));
break;
case 'hgifs':
message.channel.send(subredditRequest('nsfwanimegifs', 5));
break;
// Voice commands
case 'join':
channel = message.member.voiceChannel;
channel.join();
break;
case 'leave':
case 'stop':
case 'skip':
if(channel != null)
{
channel.leave();
channel = null;
}
playQueue = [];
break;
case 'airhorn':
setupVoice('voice/mlg-airhorn.mp3', 0.4, message.member.voiceChannel);
break;
case 'quiethorn':
setupVoice('voice/mlg-airhorn.mp3', 0.03, message.member.voiceChannel);
break;
case 'weed':
setupVoice('voice/smoke-weed.mp3', 0.2, message.member.voiceChannel);
break;
case 'damnson':
setupVoice('voice/damnson.mp3', 0.35, message.member.voiceChannel);
break;
case 'wombo':
setupVoice('voice/wombocombo.mp3', 0.06, message.member.voiceChannel);
break;
case 'cena':
setupVoice('voice/cena.mp3', 0.10, message.member.voiceChannel);
break;
case 'triple':
setupVoice('voice/triple.mp3', 0.20, message.member.voiceChannel);
break;
}
}
});
// login
client.login(auth.token);