diff options
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | bot.js | 47 | ||||
| -rw-r--r-- | commands/about.js | 28 | ||||
| -rw-r--r-- | commands/control.js | 26 | ||||
| -rw-r--r-- | commands/help.js | 21 | ||||
| -rw-r--r-- | commands/ping.js | 10 | ||||
| -rw-r--r-- | deploy-command.js | 18 | ||||
| -rw-r--r-- | package.json | 2 |
8 files changed, 144 insertions, 12 deletions
@@ -17,11 +17,15 @@ Make a new file called `config.json`. "botOwner": "your_user_id_here", "statusChannel": "channel_id", "voiceChannel": "voice_channel_id" + "guildID": "guild_id", + "clientID": "client_id", } ``` Add your own audio files using the mp3 file extension to the `music` folder. +Deploy the commands by doing `node deploy-command.js` + Launch the bot using `node bot.js` in terminal. # Help Command @@ -18,11 +18,11 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. * ***************************************************************************/ -const Discord = require('discord.js'); +const { Client, MessageEmbed, Collection, version } = require('discord.js'); const fs = require('fs'); const { join } = require('node:path'); const { createAudioPlayer, createAudioResource, joinVoiceChannel, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice'); -const bot = new Discord.Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']}); +const bot = new Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']}); const config = require('./config.json'); const player = createAudioPlayer(); let audio; @@ -44,7 +44,7 @@ function voiceInit() { }); connection.on(VoiceConnectionStatus.Destroyed, () => { - console.log('Stopping music...'); + console.log('Destroying beats...'); }); player.on('idle', () => { @@ -58,7 +58,6 @@ function voiceInit() { } function playAudio() { - let files = fs.readdirSync(join(__dirname,'music')); while (true) { @@ -81,7 +80,7 @@ function playAudio() { console.log(err); }); } - const statusEmbed = new Discord.MessageEmbed() + const statusEmbed = new MessageEmbed() .addField('Now Playing', `${audio}`) .setColor('#0066ff') @@ -91,20 +90,29 @@ function playAudio() { } -bot.on('ready', () => { +// Slash Command Handler + +bot.commands = new 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', () => { console.log('Bot is ready!'); console.log(`Logged in as ${bot.user.tag}!`); - console.log(`Running on Discord.JS ${Discord.version}`) + console.log(`Running on Discord.JS ${version}`) console.log(`Prefix: ${config.prefix}`); console.log(`Owner ID: ${config.botOwner}`); console.log(`Voice Channel: ${config.voiceChannel}`); console.log(`Status Channel: ${config.statusChannel}\n`); // Set bots' presence - bot.user.setPresence({ activities: [{ - name: `Music | ${config.prefix}help`, + name: 'some beats', type: 'LISTENING' }], status: 'online', @@ -114,7 +122,7 @@ bot.on('ready', () => { console.log(`Updated bot presence to "${activity.name}"`); // Send bots' status to channel - const readyEmbed = new Discord.MessageEmbed() + const readyEmbed = new MessageEmbed() .setAuthor({name:bot.user.username, iconURL:bot.user.avatarURL()}) .setDescription('Starting bot...') .setColor('#0066ff') @@ -127,6 +135,21 @@ bot.on('ready', () => { }); +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, player, audio); + } catch (error) { + console.error(error); + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } +}); + bot.on('messageCreate', async msg => { if (msg.author.bot) return; if (!msg.guild) return; @@ -137,7 +160,7 @@ bot.on('messageCreate', async msg => { // Public allowed commands if (command === 'help') { - const helpEmbed = new Discord.MessageEmbed() + const helpEmbed = new 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) @@ -216,7 +239,7 @@ bot.on('messageCreate', async msg => { console.log(err); }); } - const statusEmbed = new Discord.MessageEmbed() + const statusEmbed = new MessageEmbed() .setAuthor({name:bot.user.username, iconURL:bot.user.avatarURL()}) .setDescription(`That\'s all folks! Powering down ${bot.user.username}...`) .setColor('#0066ff') diff --git a/commands/about.js b/commands/about.js new file mode 100644 index 0000000..033c44f --- /dev/null +++ b/commands/about.js @@ -0,0 +1,28 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { MessageEmbed, version, MessageActionRow, MessageButton } = require("discord.js"); + +module.exports = { + data: new SlashCommandBuilder() + .setName('about') + .setDescription('Information about the bot'), + async execute(interaction, bot) { + const aboutEmbed = new MessageEmbed() + .setAuthor({name:`About ${bot.user.username}`, iconURL:bot.user.avatarURL()}) + .addField('Information', 'A Discord bot that plays local mp3 audio tracks.') + .addField('Original Creator', 'Andrew Lee (Alee#4277)') + .addField('Frameworks', `Discord.JS ${version} + Voice`) + .addField('License', 'GNU General Public License v3.0') + .setFooter({text:'© Copyright 2020-2022 Andrew Lee. Licensed with GPL-3.0.'}) + .setColor('#0066ff') + + const srcOrig = new MessageActionRow() + .addComponents( + new MessageButton() + .setStyle('LINK') + .setLabel('Original Source Code') + .setURL('https://github.com/Alee14/DLMP3'), + ); + + return interaction.reply({ embeds:[aboutEmbed], components:[srcOrig] }); + }, +};
\ No newline at end of file diff --git a/commands/control.js b/commands/control.js new file mode 100644 index 0000000..f5846e2 --- /dev/null +++ b/commands/control.js @@ -0,0 +1,26 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js"); + + +module.exports = { + data: new SlashCommandBuilder() + .setName('control') + .setDescription('Controlling the music'), + async execute(interaction, bot) { + const controlEmbed = new MessageEmbed() + .setAuthor({name:`${bot.user.username} Control Panel`, iconURL:bot.user.avatarURL()}) + .addField('Currently Playing', 'audio file here') + .addField('Next Music', '(a possible feature?)') + .setColor('#0066ff') + + const controlButtons = new MessageActionRow() + .addComponents( + new MessageButton() + .setStyle('PRIMARY') + .setLabel('Pause') + .setCustomId('soon') + ); + + return interaction.reply({embeds:[controlEmbed], components:[controlButtons]}); + }, +};
\ No newline at end of file diff --git a/commands/help.js b/commands/help.js new file mode 100644 index 0000000..67ed896 --- /dev/null +++ b/commands/help.js @@ -0,0 +1,21 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { MessageEmbed } = require("discord.js"); +const { AudioResource } = require("@discordjs/voice"); +const config = require("../config.json"); + +module.exports = { + data: new SlashCommandBuilder() + .setName('help') + .setDescription('Lists the commands'), + async execute(interaction, bot, audio) { + const helpEmbed = new MessageEmbed() + .setAuthor({name:`${bot.user.username} Help`, iconURL:bot.user.avatarURL()}) + .setDescription(`Currently playing \`${audio}\`.`) + .addField('Public Commands', `/help\n/ping\n/about\n`, true) + .addField('Bot Owner Only', `/join\n/control\n/stop\n`, true) + .setFooter({text:'© Copyright 2020-2022 Andrew Lee. Licensed with GPL-3.0.'}) + .setColor('#0066ff') + + return interaction.reply({ embeds: [helpEmbed]}); + }, +};
\ No newline at end of file diff --git a/commands/ping.js b/commands/ping.js new file mode 100644 index 0000000..c5b4885 --- /dev/null +++ b/commands/ping.js @@ -0,0 +1,10 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('ping') + .setDescription('Replies with Pong!'), + async execute(interaction) { + return interaction.reply('Pong!'); + }, +};
\ No newline at end of file diff --git a/deploy-command.js b/deploy-command.js new file mode 100644 index 0000000..e11458a --- /dev/null +++ b/deploy-command.js @@ -0,0 +1,18 @@ +const fs = require('node:fs'); +const { REST } = require('@discordjs/rest'); +const { Routes } = require('discord-api-types/v9'); +const config = require('./config.json'); + +const commands = []; +const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + commands.push(command.data.toJSON()); +} + +const rest = new REST({ version: '9' }).setToken(config.token); + +rest.put(Routes.applicationGuildCommands(config.clientID, config.guildID), { body: commands }) + .then(() => console.log('Successfully registered application commands.')) + .catch(console.error); diff --git a/package.json b/package.json index ccbd29d..b15340d 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ }, "dependencies": { "@discordjs/opus": "^0.3.2", + "@discordjs/rest": "^0.3.0", "@discordjs/voice": "^0.5.5", + "discord-api-types": "^0.30.0", "discord.js": "^13.6.0", "ffmpeg-static": "^4.2.5", "libsodium-wrappers": "^0.7.9" |
