Skip to content

Commit 46f015e

Browse files
committed
Rework .audio identify
1 parent bbe6cf7 commit 46f015e

File tree

2 files changed

+155
-76
lines changed

2 files changed

+155
-76
lines changed

src/api/types.ts

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -486,29 +486,67 @@ export namespace RestResponses {
486486

487487

488488
export namespace RestResponsesRaw {
489+
export interface AudioToolsIdentifySong {
490+
album: {name: string},
491+
artists: Array<{name: string}>,
492+
duration: number,
493+
exids: string | null,
494+
genres: Array<string>,
495+
ids: {acrid: string, isrc: string, upc: string},
496+
label: string,
497+
platforms: {
498+
apple_music: AudioToolsIdentifySongPlatform | null,
499+
deezer: AudioToolsIdentifySongPlatform | null,
500+
musicbrainz: AudioToolsIdentifySongPlatformPartial | null,
501+
spotify: AudioToolsIdentifySongPlatform | null,
502+
youtube: AudioToolsIdentifySongPlatformPartial | null,
503+
raw: any,
504+
},
505+
release_date: string,
506+
result_from: number,
507+
score: number,
508+
timestamp: number,
509+
title: string,
510+
}
489511

490-
export interface AudioToolsIdentify {
491-
error: null | {
492-
error_code: number,
493-
error_message: string,
512+
export interface AudioToolsIdentifySongPlatform {
513+
album: {
514+
cover_url: string | null,
515+
id: string,
516+
name: string,
494517
},
495-
result: null | {
496-
album: string,
497-
artist: string,
498-
label: string,
499-
release_date: string,
500-
song_link: string,
501-
timecode: string,
502-
title: string,
503-
apple_music: any,
504-
deezer: any,
505-
musicbrainz: any,
506-
napster: any,
507-
spotify: any,
518+
artist: {
519+
cover_url?: string | null,
520+
id: string,
521+
name: string,
508522
},
509-
status: string,
523+
bpm: number,
524+
disc: number,
525+
duration: number,
526+
genres: Array<string>,
527+
id: string,
528+
isrc: string,
529+
metadata: {
530+
audio_locale?: string,
531+
audio_traits?: Array<string>,
532+
has_lyrics?: boolean,
533+
has_time_synced_lyrics?: boolean,
534+
is_mastered_for_itunes?: boolean,
535+
},
536+
preview_url: string | null,
537+
release_date: string | null,
538+
title: string,
539+
track: number,
540+
url: string,
510541
}
511542

543+
export interface AudioToolsIdentifySongPlatformPartial {
544+
id: string,
545+
url: string,
546+
}
547+
548+
export type AudioToolsIdentify = Array<AudioToolsIdentifySong>;
549+
512550
export type CreateGuildAllowlist = null;
513551
export type CreateGuildBlocklist = null;
514552
export type CreateGuildDisabledCommand = null;

src/utils/formatter/commands/audio.identify.ts

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createUserString,
1212
editOrReply,
1313
fetchMemberOrUserById,
14+
formatTime,
1415
} from '../../../utils';
1516

1617

@@ -23,72 +24,112 @@ export async function createMessage(
2324
args: CommandArgs,
2425
) {
2526
const isFromInteraction = (context instanceof Interaction.InteractionContext);
26-
const { error, result } = await audioToolsIdentify(context, {
27+
const songs = await audioToolsIdentify(context, {
2728
url: args.url,
2829
});
2930

30-
if (!result) {
31-
let content: string;
32-
if (error) {
33-
content = `Error: ${error.error_message}`;
34-
} else {
35-
content = 'Unable to identify any songs in the media provided';
36-
}
37-
return editOrReply(context, {content, flags: MessageFlags.EPHEMERAL});
38-
}
31+
if (songs.length) {
32+
const pageLimit = songs.length || 1;
33+
const paginator = new Paginator(context, {
34+
pageLimit,
35+
onPage: (page) => {
36+
const song = songs[page - 1];
3937

40-
const embed = (isFromInteraction) ? new Embed() : createUserEmbed(context.user);
41-
embed.setColor(EmbedColors.DEFAULT);
42-
embed.setFooter('Audd.io Result', EmbedBrands.AUDD);
43-
embed.setTitle(result.title);
44-
embed.setUrl(result.song_link);
45-
46-
{
47-
const description: Array<string> = [];
48-
description.push(`**Album**: ${result.album}`);
49-
description.push(`**Artist**: ${result.artist}`);
50-
if (result.label) {
51-
description.push(`**Label**: ${result.label}`);
52-
}
53-
description.push(`**Release Date**: ${result.release_date}`);
54-
description.push(`**Title**: ${result.title}`);
55-
embed.setDescription(description.join('\n'));
56-
}
38+
const embed = (isFromInteraction) ? new Embed() : createUserEmbed(context.user);
39+
embed.setColor(EmbedColors.DEFAULT);
40+
embed.setTitle(song.title);
5741

58-
{
59-
const urls: Array<string> = [Markup.url('lis.tn', result.song_link)];
60-
if (result.apple_music) {
61-
urls.push(Markup.url('Apple Music', result.apple_music.url));
62-
}
63-
if (result.deezer) {
64-
urls.push(Markup.url('Deezer', result.deezer.link));
65-
}
66-
if (result.napster) {
67-
68-
}
69-
if (result.spotify) {
70-
urls.push(Markup.url('Spotify', result.spotify.external_urls.spotify));
71-
}
72-
embed.addField('Direct Links', urls.join(', '));
73-
}
42+
{
43+
let footer: string;
44+
if (pageLimit === 1) {
45+
footer = 'Audio Recognition Result';
46+
} else {
47+
footer = `Page ${page}/${pageLimit} of Audio Recognition Results`;
48+
}
49+
embed.setFooter(footer, EmbedBrands.NOTSOBOT);
50+
}
51+
52+
let thumbnail: string | undefined;
53+
for (let key in song.platforms) {
54+
const platform = (song.platforms as any)[key];
55+
if (platform && platform.album && platform.album.cover_url) {
56+
thumbnail = platform.album.cover_url;
57+
break;
58+
}
59+
}
60+
61+
if (thumbnail) {
62+
embed.setThumbnail(thumbnail);
63+
}
7464

75-
{
76-
const urls: Array<string> = [];
77-
if (result.apple_music && result.apple_music.previews.length) {
78-
urls.push(Markup.url('Apple Music', result.apple_music.previews[0].url));
79-
}
80-
if (result.deezer && result.deezer.preview) {
81-
urls.push(Markup.url('Deezer', result.deezer.preview));
82-
}
83-
if (result.spotify) {
84-
urls.push(Markup.url('Spotify', result.spotify.preview_url || result.spotify.external_urls.spotify));
85-
}
86-
if (urls.length) {
87-
embed.addField('Preview Links', urls.join(', '));
88-
}
65+
{
66+
const description: Array<string> = [];
67+
description.push(`**Album**: ${song.album.name}`);
68+
description.push(`**Artist**: ${song.artists[0]!.name}`);
69+
description.push(`**Duration**: ${formatTime(song.duration)}`);
70+
if (song.label) {
71+
description.push(`**Label**: ${song.label}`);
72+
}
73+
if (song.genres.length) {
74+
description.push(`**Genres**: ${song.genres.join(', ')}`);
75+
}
76+
description.push(`**Match**: ${song.score}%`);
77+
if (song.release_date) {
78+
description.push(`**Release Date**: ${song.release_date}`);
79+
}
80+
if (song.timestamp) {
81+
description.push(`**Timestamp**: ${formatTime(song.timestamp)}`);
82+
}
83+
description.push(`**Title**: ${song.title}`);
84+
embed.setDescription(description.join('\n'));
85+
}
86+
87+
{
88+
const urls: Array<string> = [];
89+
if (song.platforms.apple_music && song.platforms.apple_music.url) {
90+
urls.push(Markup.url('Apple Music', song.platforms.apple_music.url));
91+
}
92+
if (song.platforms.deezer && song.platforms.deezer.url) {
93+
urls.push(Markup.url('Deezer', song.platforms.deezer.url));
94+
}
95+
if (song.platforms.musicbrainz && song.platforms.musicbrainz.url) {
96+
urls.push(Markup.url('MusicBrainz', song.platforms.musicbrainz.url));
97+
}
98+
if (song.platforms.spotify && song.platforms.spotify.url) {
99+
urls.push(Markup.url('Spotify', song.platforms.spotify.url));
100+
}
101+
if (song.platforms.youtube && song.platforms.youtube.url) {
102+
urls.push(Markup.url('YouTube', song.platforms.youtube.url));
103+
}
104+
embed.addField('Platform Links', urls.join(', '));
105+
}
106+
107+
{
108+
const urls: Array<string> = [];
109+
if (song.platforms.apple_music && song.platforms.apple_music.preview_url) {
110+
urls.push(Markup.url('Apple Music', song.platforms.apple_music.preview_url));
111+
}
112+
if (song.platforms.deezer && song.platforms.deezer.preview_url) {
113+
urls.push(Markup.url('Deezer', song.platforms.deezer.preview_url));
114+
}
115+
if (song.platforms.spotify && song.platforms.spotify.preview_url) {
116+
urls.push(Markup.url('Spotify', song.platforms.spotify.preview_url));
117+
}
118+
if (urls.length) {
119+
embed.addField('Preview Links', urls.join(', '));
120+
}
121+
}
122+
123+
return embed;
124+
},
125+
});
126+
return await paginator.start();
89127
}
90128

91-
return editOrReply(context, {embed});
129+
return editOrReply(context, {
130+
content: 'Couldn\'t identify any songs',
131+
flags: MessageFlags.EPHEMERAL,
132+
});
92133
/*
93134
const pages = [result.apple_music];
94135
const pageLimit = activities.length || 1;

0 commit comments

Comments
 (0)