DLAP/bot.js

257 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-07-20 23:03:12 -04:00
/**************************************************************************
2020-07-19 15:32:22 -04:00
*
2020-07-20 22:59:08 -04:00
* DLMP3 Bot: A Discord bot that plays local mp3 audio tracks.
* (C) Copyright 2022
2020-07-20 22:59:08 -04:00
* Programmed by Andrew Lee
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-07-20 23:03:12 -04:00
*
***************************************************************************/
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');
2022-03-25 19:36:44 -04:00
const { join } = require('node:path');
const { createAudioPlayer, createAudioResource, joinVoiceChannel, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice');
2022-03-25 19:36:44 -04:00
const bot = new Discord.Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']});
const config = require('./config.json');
2021-08-01 18:07:29 -04:00
const player = createAudioPlayer();
2020-07-20 19:58:06 -04:00
let audio;
2020-12-23 19:42:15 -05:00
let fileData;
let txtFile = true;
2020-07-20 16:02:49 -04:00
2020-07-22 20:21:32 -04:00
bot.login(config.token);
2020-07-18 19:28:42 -04:00
2022-03-25 22:24:49 -04:00
function voiceInit() {
2022-03-25 19:36:44 -04:00
bot.channels.fetch(config.voiceChannel).then(channel => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});
connection.on(VoiceConnectionStatus.Ready, () => {
console.log('Ready to blast music!');
2022-03-25 19:36:44 -04:00
});
connection.on(VoiceConnectionStatus.Destroyed, () => {
console.log('Destroying beats...');
});
player.on('idle', () => {
console.log("Music has finished playing, shuffling music...")
playAudio();
})
playAudio();
2022-03-25 19:36:44 -04:00
connection.subscribe(player);
})
}
function playAudio() {
2022-03-26 00:08:44 -04:00
let files = fs.readdirSync(join(__dirname,'music'));
2021-08-01 18:07:29 -04:00
while (true) {
audio = files[Math.floor(Math.random() * files.length)];
console.log('Searching .mp3 file...');
if (audio.endsWith('.mp3')) {
break;
2020-07-20 16:02:49 -04:00
}
2021-08-01 18:07:29 -04:00
}
2020-07-20 16:02:49 -04:00
2022-03-25 19:36:44 -04:00
let resource = createAudioResource(join(__dirname, 'music/' + audio));
2021-08-01 18:07:29 -04:00
player.play(resource);
2022-03-25 19:36:44 -04:00
console.log('Now playing: ' + audio);
if (txtFile === true) {
fileData = "Now Playing: " + audio;
fs.writeFile("now-playing.txt", fileData, (err) => {
if (err)
console.log(err);
});
}
2021-08-01 18:07:29 -04:00
const statusEmbed = new Discord.MessageEmbed()
2022-03-25 19:36:44 -04:00
.addField('Now Playing', `${audio}`)
.setColor('#0066ff')
2021-08-01 18:07:29 -04:00
let statusChannel = bot.channels.cache.get(config.statusChannel);
if (!statusChannel) return console.error('The status channel does not exist! Skipping.');
2022-03-25 19:36:44 -04:00
statusChannel.send({embeds: [statusEmbed]});
2020-07-20 19:58:06 -04:00
}
2020-07-21 16:04:20 -04:00
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.data.name, command);
}
bot.once('ready', () => {
2020-07-20 17:12:55 -04:00
console.log('Bot is ready!');
2020-07-22 20:21:32 -04:00
console.log(`Logged in as ${bot.user.tag}!`);
2021-08-01 18:07:29 -04:00
console.log(`Running on Discord.JS ${Discord.version}`)
console.log(`Prefix: ${config.prefix}`);
2020-07-20 17:12:55 -04:00
console.log(`Owner ID: ${config.botOwner}`);
console.log(`Voice Channel: ${config.voiceChannel}`);
2020-07-20 21:02:26 -04:00
console.log(`Status Channel: ${config.statusChannel}\n`);
// Set bots' presence
2020-07-23 13:10:06 -04:00
bot.user.setPresence({
activities: [{
name: 'some beats',
type: 'LISTENING'
}],
2020-07-23 13:10:06 -04:00
status: 'online',
2021-08-01 18:07:29 -04:00
});
2020-07-23 13:10:06 -04:00
const activity = bot.presence.activities[0];
console.log(`Updated bot presence to "${activity.name}"`);
// Send bots' status to channel
2020-07-22 20:40:44 -04:00
const readyEmbed = new Discord.MessageEmbed()
.setAuthor({name:bot.user.username, iconURL:bot.user.avatarURL()})
.setDescription('Starting bot...')
.setColor('#0066ff')
2020-07-22 20:40:44 -04:00
let statusChannel = bot.channels.cache.get(config.statusChannel);
if (!statusChannel) return console.error('The status channel does not exist! Skipping.');
2021-08-01 18:07:29 -04:00
statusChannel.send({ embeds: [readyEmbed]});
2022-03-25 22:24:49 -04:00
voiceInit();
2020-07-18 19:28:42 -04:00
});
bot.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = bot.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, bot);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
2021-08-01 18:07:29 -04:00
bot.on('messageCreate', 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
// Public allowed commands
2022-03-25 22:48:05 -04:00
if (command === 'help') {
2020-07-20 19:58:06 -04:00
const helpEmbed = new Discord.MessageEmbed()
.setAuthor({name:`${bot.user.username} Help`, iconURL:bot.user.avatarURL()})
.setDescription(`Currently playing \`${audio}\`.`)
.addField('Public Commands', `${config.prefix}help\n${config.prefix}ping\n${config.prefix}git\n${config.prefix}playing\n${config.prefix}about\n`, true)
.addField('Bot Owner Only', `${config.prefix}join\n${config.prefix}resume\n${config.prefix}pause\n${config.prefix}skip\n${config.prefix}leave\n${config.prefix}stop\n`, true)
.setFooter({text:'© Copyright 2022 Andrew Lee. Licensed with GPL-3.0.'})
.setColor('#0066ff')
2020-07-20 19:58:06 -04:00
2021-08-01 18:07:29 -04:00
msg.channel.send({ embeds: [helpEmbed]});
2022-03-25 13:57:05 -04:00
2020-07-20 16:22:28 -04:00
}
2022-03-25 22:48:05 -04:00
if (command === 'ping') {
2020-07-20 16:22:28 -04:00
msg.reply('Pong!');
}
2022-03-25 22:48:05 -04:00
if (command === 'git') {
2020-07-21 16:59:55 -04:00
msg.reply('This is the source code of this project.\nhttps://github.com/Alee14/DLMP3');
}
2020-07-22 20:05:19 -04:00
2022-03-25 22:48:05 -04:00
if (command === 'playing') {
2020-07-22 20:05:19 -04:00
msg.channel.send('Currently playing `' + audio + '`.');
}
2020-07-20 16:22:28 -04:00
2022-03-25 22:48:05 -04:00
if (command === 'about') {
2020-07-22 20:05:19 -04:00
msg.channel.send('The bot code was created by Andrew Lee (Alee#4277). Written in Discord.JS and licensed with GPL-3.0.');
2020-07-18 19:28:42 -04:00
}
if (![config.botOwner].includes(msg.author.id)) return;
// Bot owner exclusive
2022-03-25 22:48:05 -04:00
if (command === 'join') {
2020-07-20 16:02:49 -04:00
msg.reply('Joining voice channel.');
2022-03-25 22:24:49 -04:00
voiceInit();
2020-07-18 19:28:42 -04:00
}
2022-03-25 22:48:05 -04:00
if (command === 'resume') {
2020-07-22 20:19:45 -04:00
msg.reply('Resuming music.');
2022-03-25 19:36:44 -04:00
player.unpause();
2020-07-22 20:19:45 -04:00
}
2022-03-25 22:48:05 -04:00
if (command === 'pause') {
2020-07-22 20:19:45 -04:00
msg.reply('Pausing music.');
2022-03-25 19:36:44 -04:00
player.pause();
2020-07-22 20:19:45 -04:00
}
2022-03-25 22:48:05 -04:00
if (command === 'skip') {
2020-07-20 20:41:47 -04:00
msg.reply('Skipping `' + audio + '`...');
2022-03-25 19:36:44 -04:00
player.pause()
2020-07-20 17:12:55 -04:00
playAudio();
}
2022-03-25 22:48:05 -04:00
if (command === 'leave') {
2020-07-20 20:41:47 -04:00
msg.reply('Leaving voice channel.');
2020-07-20 16:02:49 -04:00
console.log('Leaving voice channel.');
2022-03-25 23:01:06 -04:00
if (txtFile === true) {
fileData = "Now Playing: Nothing";
fs.writeFile("now-playing.txt", fileData, (err) => {
if (err)
console.log(err);
});
}
2020-12-23 19:42:15 -05:00
audio = "Not Playing";
2021-08-01 18:07:29 -04:00
player.stop();
const connection = getVoiceConnection(msg.guild.id);
connection.destroy();
2020-07-18 19:28:42 -04:00
}
2020-07-20 00:11:36 -04:00
2022-03-25 22:48:05 -04:00
if (command === 'stop') {
2020-07-20 20:41:47 -04:00
await msg.reply('Powering off...');
if (txtFile === true) {
fileData = "Now Playing: Nothing";
await fs.writeFile("now-playing.txt", fileData, (err) => {
if (err)
console.log(err);
});
}
2020-07-22 20:31:44 -04:00
const statusEmbed = new Discord.MessageEmbed()
.setAuthor({name:bot.user.username, iconURL:bot.user.avatarURL()})
.setDescription(`That\'s all folks! Powering down ${bot.user.username}...`)
.setColor('#0066ff')
2020-07-22 20:31:44 -04:00
let statusChannel = bot.channels.cache.get(config.statusChannel);
2020-07-22 20:40:44 -04:00
if (!statusChannel) return console.error('The status channel does not exist! Skipping.');
2021-08-01 18:07:29 -04:00
await statusChannel.send({ embeds: [statusEmbed] });
2020-07-20 16:22:28 -04:00
console.log('Powering off...');
2021-08-01 18:07:29 -04:00
player.stop();
const connection = getVoiceConnection(msg.guild.id);
connection.destroy();
2020-07-22 20:21:32 -04:00
bot.destroy();
2020-07-20 16:22:28 -04:00
process.exit(0);
}
2020-07-21 16:59:55 -04:00
});