2020-07-20 23:03:12 -04:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2022-07-09 22:14:38 -04:00
*
2022-03-30 22:01:18 -04:00
* DLAP Bot : A Discord bot that plays local audio tracks .
2022-03-25 18:28:20 -04:00
* ( 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-03-27 23:41:04 -04:00
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
2022-03-30 22:01:18 -04:00
bot . login ( config . token ) ;
2022-03-27 23:41:04 -04:00
/ * *
* Project Ideas :
2022-03-28 11:00:23 -04:00
* Shuffle or "Play by order" mode
2022-04-03 16:43:47 -04:00
* Audio streaming
2022-03-27 23:41:04 -04:00
* /
2022-03-26 17:53:07 -04:00
// Slash Command Handler
bot . commands = new Collection ( ) ;
2022-03-30 22:01:18 -04:00
const commandFiles = readdirSync ( './commands' ) . filter ( file => file . endsWith ( '.js' ) ) ;
2022-03-26 13:43:03 -04:00
for ( const file of commandFiles ) {
2022-03-27 23:41:04 -04:00
const { default : command } = await import ( ` ./commands/ ${ file } ` ) ;
2022-03-26 13:43:03 -04:00
bot . commands . set ( command . data . name , command ) ;
}
2022-03-29 20:41:38 -04:00
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 } ` ) ;
2020-07-20 20:26:26 -04:00
console . log ( ` Voice Channel: ${ config . voiceChannel } ` ) ;
2022-03-26 21:07:35 -04:00
console . log ( ` Status Channel: ${ config . statusChannel } ` ) ;
2020-07-21 15:29:57 -04:00
2022-03-25 18:28:20 -04:00
// Set bots' presence
2020-07-23 13:10:06 -04:00
bot . user . setPresence ( {
2022-03-25 18:28:20 -04:00
activities : [ {
2022-03-26 13:43:03 -04:00
name : 'some beats' ,
2022-03-25 18:28:20 -04:00
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
2022-03-25 18:28:20 -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 ] } ) ;
2022-03-25 18:28:20 -04:00
2022-03-29 20:41:38 -04:00
await voiceInit ( bot ) ;
2020-07-18 19:28:42 -04:00
} ) ;
2022-03-26 13:43:03 -04:00
bot . on ( 'interactionCreate' , async interaction => {
if ( ! interaction . isCommand ( ) ) return ;
const command = bot . commands . get ( interaction . commandName ) ;
if ( ! command ) return ;
try {
2022-03-27 23:41:04 -04:00
await command . execute ( interaction , bot ) ;
2022-07-05 18:16:06 -04:00
} catch ( e ) {
console . error ( e ) ;
if ( e == null ) {
2022-03-27 23:41:04 -04:00
await interaction . reply ( { content : 'There was an error while executing this command!' , ephemeral : true } ) ;
} else {
2022-07-07 23:49:37 -04:00
await interaction . reply ( { content : ` There was an error while executing this command! \n Share this to the bot owner! \n \n Details: \` \` \` ${ e } \` \` \` ` , ephemeral : true } ) ;
2022-03-27 23:41:04 -04:00
}
2022-03-26 13:43:03 -04:00
}
} ) ;