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 .
2020-07-20 22:37:44 -04:00
* ( C ) Copyright 2020
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' ) ;
2021-08-01 18:07:29 -04:00
const { createAudioPlayer , createAudioResource , joinVoiceChannel } = require ( '@discordjs/voice' ) ;
const bot = new Discord . Client ( { intents : [ 'GUILDS' , 'GUILD_MESSAGES' ] } ) ;
2020-07-20 16:09:35 -04:00
const config = require ( './config.json' ) ;
2021-08-01 18:07:29 -04:00
const player = createAudioPlayer ( ) ;
const connection = joinVoiceChannel ( {
channelId : config . voiceChannel ,
guildId : config . guildID ,
2022-03-25 13:57:05 -04:00
//adapterCreator: bot.guilds.client
2021-08-01 18:07:29 -04:00
} ) ;
2020-07-20 17:12:55 -04:00
let dispatcher ;
2020-07-20 19:58:06 -04:00
let audio ;
2020-07-21 17:13:52 -04:00
let voiceChannel ;
2020-12-23 19:42:15 -05:00
let fileData ;
2021-08-01 18:07:29 -04:00
let resource ;
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
2020-07-19 13:42:52 -04:00
function playAudio ( ) {
2021-08-01 18:07:29 -04:00
//voiceChannel = bot.channels.cache.get(config.voiceChannel);
//if (!voiceChannel) return console.error('The voice channel does not exist!\n(Have you looked at your configuration?)');
2020-07-20 14:43:33 -04:00
2021-08-01 18:07:29 -04:00
let files = fs . readdirSync ( './music' ) ;
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
2021-08-01 18:07:29 -04:00
resource = createAudioResource ( './music/' + audio ) ;
player . play ( resource ) ;
2022-03-25 13:57:05 -04:00
connection . subscribe ( player ) ;
2021-08-01 18:07:29 -04:00
console . log ( 'Now playing ' + audio ) ;
fileData = "Now Playing: " + audio ;
fs . writeFile ( "now-playing.txt" , fileData , ( err ) => {
if ( err )
console . log ( err ) ;
} ) ;
const statusEmbed = new Discord . MessageEmbed ( )
. addField ( 'Now Playing' , ` ${ audio } ` )
. setColor ( '#0066ff' )
let statusChannel = bot . channels . cache . get ( config . statusChannel ) ;
if ( ! statusChannel ) return console . error ( 'The status channel does not exist! Skipping.' ) ;
statusChannel . send ( { embeds : [ statusEmbed ] } ) ;
/ * d i s p a t c h e r . o n ( ' e r r o r ' , c o n s o l e . e r r o r ) ;
2020-07-20 19:58:06 -04:00
2021-08-01 18:07:29 -04:00
dispatcher . on ( 'finish' , ( ) => {
console . log ( 'Music has finished playing.' ) ;
playAudio ( ) ;
} ) ;
voiceChannel . join ( ) . then ( connection => {
2020-07-20 16:57:03 -04:00
2020-07-19 12:59:04 -04:00
} ) . catch ( e => {
console . error ( e ) ;
} ) ;
2021-08-01 18:07:29 -04:00
* /
2020-07-19 13:42:52 -04:00
}
2020-07-21 16:04:20 -04:00
2020-07-22 20:21:32 -04:00
bot . on ( '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 } ` )
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 } ` ) ;
2020-07-20 21:02:26 -04:00
console . log ( ` Status Channel: ${ config . statusChannel } \n ` ) ;
2020-07-21 15:29:57 -04:00
2020-07-23 13:10:06 -04:00
bot . user . setPresence ( {
activity : {
name : ` Music | ${ config . prefix } help `
} ,
status : 'online' ,
2021-08-01 18:07:29 -04:00
} ) ;
console . log ( ` Updated bot presence to " ${ bot . user . activity } " ` ) ;
2020-07-23 13:10:06 -04:00
2020-07-22 20:40:44 -04:00
const readyEmbed = new Discord . MessageEmbed ( )
2022-03-25 13:57:05 -04:00
. setAuthor ( { name : ` ${ bot . user . username } ` , url : bot . user . avatarURL ( ) } )
2020-07-22 20:40:44 -04:00
. setDescription ( 'Starting bot...' )
. setColor ( '#0066ff' )
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 13:57:05 -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
} ) ;
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
2020-07-20 16:16:19 -04:00
// Public allowed commands
if ( command == 'help' ) {
2022-03-25 13:57:05 -04:00
//if (!msg.guild.member(bot.user).hasPermission('EMBED_LINKS')) return msg.reply('**ERROR: This bot doesn\'t have the permission to send embed links please enable them to use the full help.**');
2020-07-20 19:58:06 -04:00
const helpEmbed = new Discord . MessageEmbed ( )
2022-03-25 13:57:05 -04:00
. setAuthor ( { name : ` ${ bot . user . username } Help ` , iconURL : bot . user . avatarURL ( ) } )
2020-07-22 20:05:19 -04:00
. setDescription ( ` Currently playing \` ${ audio } \` . ` )
2020-07-23 19:34:25 -04:00
. 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 )
2022-03-25 13:57:05 -04:00
. setFooter ( { text : '© Copyright 2022 Andrew Lee. Licensed with GPL-3.0.' } )
2020-07-20 19:58:06 -04:00
. setColor ( '#0066ff' )
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
}
if ( command == 'ping' ) {
msg . reply ( 'Pong!' ) ;
2020-07-20 16:16:19 -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-20 16:16:19 -04:00
}
2020-07-22 20:05:19 -04:00
if ( command == 'playing' ) {
msg . channel . send ( 'Currently playing `' + audio + '`.' ) ;
}
2020-07-20 16:22:28 -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
}
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-22 20:19:45 -04:00
if ( command == 'resume' ) {
msg . reply ( 'Resuming music.' ) ;
dispatcher . resume ( ) ;
}
if ( command == 'pause' ) {
msg . reply ( 'Pausing music.' ) ;
dispatcher . pause ( ) ;
}
2020-07-20 17:12:55 -04:00
if ( command == 'skip' ) {
2020-07-20 20:41:47 -04:00
msg . reply ( 'Skipping `' + audio + '`...' ) ;
2020-07-20 17:12:55 -04:00
dispatcher . pause ( ) ;
2020-07-20 21:00:08 -04:00
dispatcher = null ;
2020-07-20 17:12:55 -04:00
playAudio ( ) ;
}
2020-07-18 20:45:06 -04:00
if ( command == 'leave' ) {
2020-07-22 20:21:32 -04:00
voiceChannel = bot . channels . cache . get ( config . voiceChannel ) ;
2020-07-21 17:13:52 -04:00
if ( ! voiceChannel ) return console . error ( 'The voice channel does not exist!\n(Have you looked at your configuration?)' ) ;
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.' ) ;
2020-12-23 19:42:15 -05:00
fileData = "Now Playing: Nothing" ;
fs . writeFile ( "now-playing.txt" , fileData , ( err ) => {
if ( err )
console . log ( err ) ;
} ) ;
audio = "Not Playing" ;
2021-08-01 18:07:29 -04:00
player . stop ( ) ;
2020-07-21 17:13:52 -04:00
voiceChannel . 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' ) {
2020-07-20 20:41:47 -04:00
await msg . reply ( 'Powering off...' ) ;
2020-12-23 19:42:15 -05:00
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 ( )
2022-03-25 13:57:05 -04:00
. setAuthor ( { title : bot . user . username , url : bot . user . avatarURL ( ) } )
2020-07-22 22:54:51 -04:00
. setDescription ( ` That \' s all folks! Powering down ${ bot . user . username } ... ` )
2020-07-22 20:31:44 -04:00
. setColor ( '#0066ff' )
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 ( ) ;
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
} ) ;