DLAP/bot.js

88 lines
2.3 KiB
JavaScript
Raw Normal View History

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();
const config = require('./config.json');
2020-07-20 16:02:49 -04:00
client.login(config.token);
2020-07-18 19:28:42 -04:00
function playAudio() {
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!');
channel.join().then(connection => {
2020-07-20 16:02:49 -04:00
console.log('Connected to the voice channel.');
let files = fs.readdirSync('./music');
let audio;
2020-07-20 16:02:49 -04:00
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');
2020-07-20 16:02:49 -04:00
serviceChannel.send('**Project Jul-2020 Bot:**\nNow playing ' + audio);
}).catch(e => {
console.error(e);
});
}
client.on('ready', () => {
2020-07-20 16:02:49 -04:00
console.log('Bot is ready!')
console.log(`Logged in as ${client.user.tag}!`);
console.log(`Prefix: ${config.prefix}`);
2020-07-20 16:02:49 -04:00
client.user.setStatus('invisible');
playAudio();
2020-07-18 19:28:42 -04:00
});
client.on('message', async msg => {
if (msg.author.bot) return;
if (!msg.guild) return;
if (!msg.content.startsWith(config.prefix)) return;
if (![config.botOwner].includes(msg.author.id)) return;
2020-07-20 16:02:49 -04:00
let command = msg.content.split(' ')[0];
command = command.slice(config.prefix.length);
2020-07-18 19:28:42 -04:00
if (command == 'ping') {
msg.reply('Pong!');
2020-07-18 19:28:42 -04:00
}
if (command == 'stop') {
2020-07-20 16:02:49 -04:00
await msg.reply('Powering off...')
console.log('Powering off...');
client.destroy();
process.exit(0);
2020-07-18 19:28:42 -04:00
}
if (command == 'join') {
2020-07-18 19:28:42 -04:00
// Only try to join the sender's voice channel if they are in one themselves
2020-07-20 16:02:49 -04:00
msg.reply('Joining voice channel.');
playAudio();
2020-07-18 19:28:42 -04:00
}
2020-07-18 21:08:54 -04:00
if (command == 'skip') {
2020-07-20 16:02:49 -04:00
2020-07-18 21:08:54 -04:00
}
if (command == 'leave') {
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.');
channel.leave();
2020-07-18 19:28:42 -04:00
}
2020-07-20 00:11:36 -04:00
2020-07-20 16:02:49 -04:00
});