DLAP/bot.js

97 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-07-20 23:03:12 -04:00
/**************************************************************************
2022-07-09 22:14:38 -04:00
*
* DLAP Bot: A Discord bot that plays local audio tracks.
* (C) Copyright 2022
2022-07-09 22:14:38 -04:00
* Programmed by Andrew Lee
*
2020-07-20 22:59:08 -04:00
* 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/>.
2022-07-09 22:14:38 -04:00
*
2020-07-20 23:03:12 -04:00
***************************************************************************/
2022-07-09 22:14:38 -04:00
import { Client, MessageEmbed, Collection, version } from 'discord.js';
import { voiceInit } from './AudioBackend.js';
import { readdirSync, readFileSync } from 'node:fs';
// import config from './config.json' assert { type: 'json' } Not in ECMAScript yet
const config = JSON.parse(readFileSync('./config.json'));
2022-07-09 22:14:38 -04:00
const bot = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES'] });
2020-07-20 16:02:49 -04:00
bot.login(config.token);
/**
* Project Ideas:
2022-03-28 11:00:23 -04:00
* Shuffle or "Play by order" mode
* Audio streaming
*/
2022-03-26 17:53:07 -04:00
// Slash Command Handler
bot.commands = new Collection();
const commandFiles = readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const { default: command } = await import(`./commands/${file}`);
bot.commands.set(command.data.name, command);
}
bot.once('ready', async () => {
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}!`);
2022-07-09 22:14:38 -04:00
console.log(`Running on Discord.JS ${version}`);
console.log(`Voice Channel: ${config.voiceChannel}`);
2022-03-26 21:07:35 -04:00
console.log(`Status Channel: ${config.statusChannel}`);
// Set bots' presence
2020-07-23 13:10:06 -04:00
bot.user.setPresence({
activities: [{
name: 'some beats',
type: 'LISTENING'
}],
2022-07-09 22:14:38 -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
2022-03-26 17:53:07 -04:00
const readyEmbed = new MessageEmbed()
2022-07-09 22:14:38 -04:00
.setAuthor({ name: bot.user.username, iconURL: bot.user.avatarURL() })
.setDescription('Starting bot...')
.setColor('#0066ff');
2020-07-22 20:40:44 -04:00
2022-07-09 22:14:38 -04:00
const 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.');
2022-07-09 22:14:38 -04:00
await statusChannel.send({ embeds: [readyEmbed] });
await voiceInit(bot);
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 (e) {
console.error(e);
if (e == null) {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: `There was an error while executing this command!\nShare this to the bot owner!\n\nDetails:\`\`\`${e}\`\`\``, ephemeral: true });
}
}
});