aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Lee <alee14498@protonmail.com>2022-11-28 22:17:18 -0500
committerAndrew Lee <alee14498@protonmail.com>2022-11-28 22:17:18 -0500
commitc4300f9955c2757369fd800e1736c19a4b69b276 (patch)
tree95ea698c5e9afa048d839333fd5cbd4d460d1029
parent246dba3089e970f009c6579d405d8553ca441b7a (diff)
downloadDLAP-c4300f9955c2757369fd800e1736c19a4b69b276.tar.gz
DLAP-c4300f9955c2757369fd800e1736c19a4b69b276.tar.bz2
DLAP-c4300f9955c2757369fd800e1736c19a4b69b276.zip
Added metadata support
-rw-r--r--AudioBackend.js41
-rw-r--r--bot.js4
-rw-r--r--commands/status.js29
3 files changed, 63 insertions, 11 deletions
diff --git a/AudioBackend.js b/AudioBackend.js
index 1b6d762..6c646b8 100644
--- a/AudioBackend.js
+++ b/AudioBackend.js
@@ -28,6 +28,8 @@ import {
} from '@discordjs/voice';
import { EmbedBuilder } from 'discord.js';
import { readdirSync, readFileSync, writeFile } from 'node:fs';
+import { parseFile } from 'music-metadata';
+
// import config from './config.json' assert {type: 'json'}
const { voiceChannel, statusChannel, shuffle, txtFile } = JSON.parse(readFileSync('./config.json'));
@@ -41,6 +43,12 @@ export let currentTrack;
export let playerState;
export let isAudioStatePaused;
+export let metadataEmpty = false;
+
+export let audioTitle;
+export let audioArtist;
+export let audioYear;
+export let audioAlbum;
export async function voiceInit(bot) {
bot.channels.fetch(voiceChannel).then(async channel => {
@@ -138,6 +146,21 @@ export async function playAudio(bot) {
playerState = 'Playing';
isAudioStatePaused = false;
+ try {
+ const { common } = await parseFile('music/' + audio);
+ metadataEmpty = false;
+ if (common.title && common.artist && common.year && common.album) {
+ audioTitle = common.title;
+ audioArtist = common.artist;
+ audioYear = common.year;
+ audioAlbum = common.album;
+ } else {
+ metadataEmpty = true;
+ }
+ } catch (error) {
+ console.error(error.message);
+ }
+
audio = audio.split('.').slice(0, -1).join('.');
if (txtFile === true) {
@@ -147,10 +170,20 @@ export async function playAudio(bot) {
});
}
- const statusEmbed = new EmbedBuilder()
- .addFields({ name: 'Now Playing', value: audio })
- .setColor('#0066ff');
-
+ const statusEmbed = new EmbedBuilder();
+ if (metadataEmpty === true) {
+ statusEmbed.addFields({ name: 'Now Playing', value: audio });
+ statusEmbed.setColor('#0066ff');
+ } else {
+ statusEmbed.setTitle('Now Playing');
+ statusEmbed.addFields(
+ { name: 'Title:', value: audioTitle },
+ { name: 'Artist:', value: audioArtist },
+ { name: 'Year:', value: `${audioYear}` }
+ );
+ statusEmbed.setFooter({ text: `${audioAlbum}` });
+ statusEmbed.setColor('#0066ff');
+ }
const channel = bot.channels.cache.get(statusChannel);
if (!channel) return console.error('The status channel does not exist! Skipping.');
return await channel.send({ embeds: [statusEmbed] });
diff --git a/bot.js b/bot.js
index 4ef5542..abb893c 100644
--- a/bot.js
+++ b/bot.js
@@ -28,9 +28,7 @@ const bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.G
bot.login(token);
/**
- * TODO: - Metadata support
- * - m3u support
- * - Custom string support (Basically change what the bot is saying)
+ * TODO: - Custom string support (Basically change what the bot is saying)
* - Non repeat support
* - Modularizing AudioBackend
*/
diff --git a/commands/status.js b/commands/status.js
index 654b496..b564a88 100644
--- a/commands/status.js
+++ b/commands/status.js
@@ -20,7 +20,8 @@
***************************************************************************/
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
-import { audio, currentTrack, files, playerState } from '../AudioBackend.js';
+import { parseFile } from 'music-metadata';
+import { audio, currentTrack, files, playerState, audioTitle, metadataEmpty } from '../AudioBackend.js';
export default {
data: new SlashCommandBuilder()
@@ -31,19 +32,39 @@ export default {
audioID++;
let audioName = files[audioID];
+
if (audioName === undefined) {
audioName = 'Playlist Finished';
} else {
- audioName = audioName.split('.').slice(0, -1).join('.');
+ if (metadataEmpty === false) {
+ try {
+ const { common } = await parseFile('music/' + audioName);
+ audioName = common.title;
+ } catch (error) {
+ console.error(error.message);
+ }
+ } else {
+ audioName = audioName.split('.').slice(0, -1).join('.');
+ }
}
const controlEmbed = new EmbedBuilder()
.setAuthor({ name: `${bot.user.username} Status`, iconURL: bot.user.avatarURL() })
.addFields({ name: 'State', value: playerState })
.addFields({ name: 'Tracks', value: `${audioID}/${files.length}` })
- .addFields({ name: 'Currently Playing', value: audio })
- .addFields({ name: 'Up Next', value: audioName })
.setColor('#0066ff');
+
+ if (metadataEmpty === true) {
+ controlEmbed.addFields(
+ { name: 'Currently Playing', value: audio },
+ { name: 'Up Next', value: audioName }
+ );
+ } else {
+ controlEmbed.addFields(
+ { name: 'Currently Playing', value: audioTitle },
+ { name: 'Up Next', value: audioName }
+ );
+ }
interaction.reply({ embeds: [controlEmbed] });
}
};