2020-07-19 15:32:22 -04:00
|
|
|
/*********************************************
|
|
|
|
*
|
|
|
|
* Project Jul-2020 Discord Bot
|
|
|
|
* By: Andrew Lee
|
|
|
|
*
|
|
|
|
* Licensed with GPL-3.0
|
|
|
|
*
|
|
|
|
*********************************************/
|
2020-07-18 19:28:42 -04:00
|
|
|
const Discord = require('discord.js');
|
2020-07-20 16:02:49 -04:00
|
|
|
const fs = require('fs');
|
2020-07-18 19:28:42 -04:00
|
|
|
const client = new Discord.Client();
|
2020-07-20 16:09:35 -04:00
|
|
|
const config = require('./config.json');
|
2020-07-20 17:12:55 -04:00
|
|
|
let dispatcher;
|
2020-07-20 19:58:06 -04:00
|
|
|
let audio;
|
2020-07-20 16:02:49 -04:00
|
|
|
|
|
|
|
client.login(config.token);
|
2020-07-18 19:28:42 -04:00
|
|
|
|
2020-07-19 13:42:52 -04:00
|
|
|
function playAudio() {
|
2020-07-20 16:09:35 -04:00
|
|
|
const channel = client.channels.cache.get(config.voiceChannel);
|
2020-07-20 16:02:49 -04:00
|
|
|
if (!channel) return console.error('The channel does not exist!');
|
2020-07-20 14:43:33 -04:00
|
|
|
|
2020-07-19 12:59:04 -04:00
|
|
|
channel.join().then(connection => {
|
2020-07-20 16:02:49 -04:00
|
|
|
let files = fs.readdirSync('./music');
|
2020-07-20 14:43:33 -04:00
|
|
|
|
2020-07-20 16:02:49 -04:00
|
|
|
while (true) {
|
|
|
|
audio = files[Math.floor(Math.random() * files.length)];
|
2020-07-20 19:58:06 -04:00
|
|
|
console.log('Searching .mp3 file...');
|
2020-07-20 16:02:49 -04:00
|
|
|
if (audio.endsWith('.mp3')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:12:55 -04:00
|
|
|
dispatcher = connection.play('./music/' + audio);
|
2020-07-20 20:07:03 -04:00
|
|
|
|
2020-07-20 19:58:06 -04:00
|
|
|
dispatcher.on('start', () => {
|
|
|
|
console.log('Now playing ' + audio);
|
2020-07-20 20:17:40 -04:00
|
|
|
let statusChannel = client.channels.cache.get(config.statusChannel);
|
|
|
|
if (!statusChannel) return console.error('The channel does not exist!');
|
|
|
|
statusChannel.send('**Project Jul-2020 Bot:**\nNow playing ' + audio);
|
2020-07-20 19:58:06 -04:00
|
|
|
});
|
2020-07-20 16:57:03 -04:00
|
|
|
|
2020-07-20 19:58:06 -04:00
|
|
|
dispatcher.on('error', console.error);
|
|
|
|
|
2020-07-20 16:57:03 -04:00
|
|
|
dispatcher.on('finish', () => {
|
|
|
|
playAudio();
|
|
|
|
});
|
|
|
|
|
2020-07-19 12:59:04 -04:00
|
|
|
}).catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
2020-07-20 16:22:28 -04:00
|
|
|
|
2020-07-19 13:42:52 -04:00
|
|
|
}
|
|
|
|
client.on('ready', () => {
|
2020-07-20 17:12:55 -04:00
|
|
|
console.log('Bot is ready!');
|
2020-07-19 13:42:52 -04:00
|
|
|
console.log(`Logged in as ${client.user.tag}!`);
|
2020-07-20 16:09:35 -04:00
|
|
|
console.log(`Prefix: ${config.prefix}`);
|
2020-07-20 17:12:55 -04:00
|
|
|
console.log(`Owner ID: ${config.botOwner}`);
|
2020-07-20 20:26:26 -04:00
|
|
|
console.log(`Voice Channel: ${config.voiceChannel}`);
|
|
|
|
console.log(`Status Channel: ${config.statusChannel}\n`)
|
2020-07-20 16:57:03 -04:00
|
|
|
|
2020-07-20 16:02:49 -04:00
|
|
|
client.user.setStatus('invisible');
|
2020-07-20 19:58:06 -04:00
|
|
|
console.log('Connected to the voice channel.');
|
2020-07-19 13:42:52 -04:00
|
|
|
playAudio();
|
2020-07-18 19:28:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
client.on('message', async msg => {
|
2020-07-20 16:22:28 -04:00
|
|
|
if (msg.author.bot) return;
|
|
|
|
if (!msg.guild) return;
|
|
|
|
if (!msg.content.startsWith(config.prefix)) return;
|
|
|
|
let command = msg.content.split(' ')[0];
|
|
|
|
command = command.slice(config.prefix.length);
|
2020-07-18 19:28:42 -04:00
|
|
|
|
2020-07-20 16:16:19 -04:00
|
|
|
// Public allowed commands
|
|
|
|
|
|
|
|
if (command == 'help') {
|
2020-07-20 19:58:06 -04:00
|
|
|
const helpEmbed = new Discord.MessageEmbed()
|
|
|
|
.addField('Bot Help', `${config.prefix}help\n${config.prefix}ping\n${config.prefix}git\n${config.prefix}about`)
|
|
|
|
.setFooter('© Copyright 2020, Andrew Lee. Licensed with GPL-3.0.')
|
|
|
|
.setColor('#0066ff')
|
|
|
|
|
|
|
|
msg.channel.send(helpEmbed);
|
2020-07-20 16:22:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (command == 'ping') {
|
|
|
|
msg.reply('Pong!');
|
2020-07-20 16:16:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (command == 'git') {
|
2020-07-20 20:07:03 -04:00
|
|
|
msg.reply('This is the source code of this project.\nhttps://github.com/Alee14/PJ2020-Discord-Bot');
|
2020-07-20 16:16:19 -04:00
|
|
|
}
|
2020-07-20 16:22:28 -04:00
|
|
|
|
|
|
|
if (command == 'about') {
|
2020-07-20 20:07:03 -04:00
|
|
|
msg.channel.send('This bot was created by Andrew Lee. Written in Discord.JS and licensed with GPL-3.0.');
|
2020-07-18 19:28:42 -04:00
|
|
|
}
|
2020-07-18 20:45:06 -04:00
|
|
|
|
2020-07-20 16:16:19 -04:00
|
|
|
if (![config.botOwner].includes(msg.author.id)) return;
|
|
|
|
|
|
|
|
// Bot owner exclusive
|
|
|
|
|
2020-07-18 20:45:06 -04:00
|
|
|
|
|
|
|
if (command == 'join') {
|
2020-07-20 16:02:49 -04:00
|
|
|
msg.reply('Joining voice channel.');
|
2020-07-20 19:58:06 -04:00
|
|
|
console.log('Connected to the voice channel.');
|
2020-07-20 16:02:49 -04:00
|
|
|
playAudio();
|
2020-07-18 19:28:42 -04:00
|
|
|
}
|
2020-07-20 16:09:35 -04:00
|
|
|
|
2020-07-20 17:12:55 -04:00
|
|
|
if (command == 'skip') {
|
2020-07-20 20:07:03 -04:00
|
|
|
msg.reply('Skipping `' + audio + '`...')
|
2020-07-20 17:12:55 -04:00
|
|
|
dispatcher.pause();
|
|
|
|
dispatcher = null
|
|
|
|
playAudio();
|
|
|
|
}
|
|
|
|
|
2020-07-18 20:45:06 -04:00
|
|
|
if (command == 'leave') {
|
2020-07-20 16:09:35 -04:00
|
|
|
const channel = client.channels.cache.get(config.voiceChannel);
|
2020-07-20 16:02:49 -04:00
|
|
|
if (!channel) return console.error('The channel does not exist!');
|
|
|
|
msg.reply('Leaving voice channel.')
|
|
|
|
console.log('Leaving voice channel.');
|
2020-07-19 22:38:10 -04:00
|
|
|
channel.leave();
|
2020-07-18 19:28:42 -04:00
|
|
|
}
|
2020-07-20 00:11:36 -04:00
|
|
|
|
2020-07-20 16:22:28 -04:00
|
|
|
if (command == 'stop') {
|
|
|
|
await msg.reply('Powering off...')
|
|
|
|
console.log('Powering off...');
|
|
|
|
client.destroy();
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:02:49 -04:00
|
|
|
});
|