Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/lyrics #31

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"express": "^4.18.2",
"ffmpeg": "^0.0.4",
"fluent-ffmpeg": "^2.1.2",
"genius-lyrics-ts": "^0.2.4",
"http": "^0.0.1-security",
"play-dl": "^1.9.6",
"songlyrics": "^2.4.5",
"winston": "^3.8.2",
"yt-search": "^2.10.4",
"ytdl-core": "^4.11.4"
Expand Down
33 changes: 33 additions & 0 deletions src/bot/commands/command-lyrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Message } from 'discord.js';
import { Command } from './command';
import { MarliMusic } from '../marli-music';
import { LyricsSearch } from '@/sources/source-lyrics';

export class ShowLyrics extends Command {
constructor(protected bot: MarliMusic) {
super(bot);
this.name = 'lyrics';
}
async execute(message: Message<boolean>): Promise<void> {
try {
this.validate(message, 'show lyrics');
const queue = this.getQueue();
const items = queue.getList(message.member.voice.channelId);
const current = items.shift();

const { userSearch, streamInfo } = current;

const search: LyricsSearch = {
artist: streamInfo.artist,
title: streamInfo.title,
userSearch,
};

const lyrics = await this.bot.sourceLyrics.getLyrics(search);
console.log({ lyrics });
message.channel.send(lyrics);
} catch (error) {
this.sendCommandError(error, message);
}
}
}
2 changes: 2 additions & 0 deletions src/bot/commands/command-map.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ShowLyrics } from './command-lyrics';
import { CommandHelp } from './command-help';
import { ListQueue } from './command-list-queue';
import { Pause } from './command-pause';
Expand All @@ -18,4 +19,5 @@ export const ALL_COMMANDS: Record<string, any> = {
queue: ListQueue,
help: CommandHelp,
shuffle: Shuffle,
lyrics: ShowLyrics,
};
4 changes: 3 additions & 1 deletion src/bot/commands/command-play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BOT_MESSAGES } from '../containts/default-messages';
import { MarliMusic } from '../marli-music';
import { Command } from './command';
import { PlayHook } from './hooks/command-play-hook';
import { ListQueue } from './command-list-queue';

export class Play extends Command {
constructor(bot: MarliMusic) {
Expand Down Expand Up @@ -62,8 +63,9 @@ export class Play extends Command {

replyContent = `${message.author.username} ${BOT_MESSAGES.CURRENT_PLAYING} ${firstSong.title} - ${firstSong.artist}`;
}

await message.channel.send(replyContent);
const listQueue = new ListQueue(this.bot);
await listQueue.execute(message);
} catch (err) {
await this.sendCommandError(err, message);
}
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { Command } from './command';
export { ListQueue } from './command-list-queue';
export { CommandHelp } from './command-help';
export { Shuffle } from './command-shuffle';
export { ShowLyrics } from './command-lyrics';
34 changes: 21 additions & 13 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as dotenv from 'dotenv';
import { LocalQueue } from '../queue/queue';
import { PlayDlSourceStream } from '../sources/play-dl-source/play-dl-source';
import { MarliMusic } from './marli-music';
import { GeniusLyricsSource } from '@/sources/genius-lyrics/genius-lyrics-source';

dotenv.config();

Expand All @@ -13,20 +14,27 @@ export function botStartup() {
};
const queue = new LocalQueue();
const sourceStream = new PlayDlSourceStream();
const sourceLyrics = new GeniusLyricsSource();

const marliMusic = new MarliMusic(botInfo, sourceStream, queue, {
intents: [
'Guilds',
'GuildMessages',
'MessageContent',
'GuildVoiceStates',
'DirectMessageReactions',
'GuildEmojisAndStickers',
'GuildMembers',
'GuildMessageTyping',
'GuildMessageReactions',
],
});
const marliMusic = new MarliMusic(
botInfo,
sourceStream,
sourceLyrics,
queue,
{
intents: [
'Guilds',
'GuildMessages',
'MessageContent',
'GuildVoiceStates',
'DirectMessageReactions',
'GuildEmojisAndStickers',
'GuildMembers',
'GuildMessageTyping',
'GuildMessageReactions',
],
}
);

return marliMusic;
}
2 changes: 2 additions & 0 deletions src/bot/marli-music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AudioPlayer } from '@discordjs/voice';
import { ALL_COMMANDS, Command } from './commands';
import { CommandHelp } from './commands/command-help';
import { BOT_MESSAGES } from './containts/default-messages';
import { SourceLyrics } from '@/sources/source-lyrics';

export interface BotInfo {
prefix: string;
Expand All @@ -24,6 +25,7 @@ export class MarliMusic extends Client {
constructor(
private botInfo: BotInfo,
public sourceStream: SourceStream,
public sourceLyrics: SourceLyrics,
public queue: Queue,
options?: ClientOptions
) {
Expand Down
26 changes: 26 additions & 0 deletions src/sources/genius-lyrics/genius-lyrics-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { LyricsSearch } from './../source-lyrics';
import genius from 'genius-lyrics-ts';
import { SongLyricsSource } from '../song-lyrics/song-lyrics-source';

export class GeniusLyricsSource implements SongLyricsSource {
public async getLyrics(search: LyricsSearch, retries = 2): Promise<string> {
try {
const { artist, title } = search;
console.log({ search, retries });
const response = await genius({ title, artist, optimizeQuery: false });
console.log({ response });
if (!response) throw new Error('LYRICS_NOT_FOUND');
return response;
} catch (error) {
console.log({ error });
if (retries > 1) {
return await this.getLyrics(
{
title: search.userSearch,
},
--retries
);
} else throw new Error('LYRICS_NOT_FOUND');
}
}
}
10 changes: 10 additions & 0 deletions src/sources/song-lyrics/song-lyrics-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import songLyrics from 'songlyrics';
import { LyricsSearch, SourceLyrics } from '../source-lyrics';

export class SongLyricsSource implements SourceLyrics {
public async getLyrics(search: LyricsSearch): Promise<string> {
const response = await songLyrics(search.artist + search.title);
console.log(response);
return response.lyrics;
}
}
9 changes: 9 additions & 0 deletions src/sources/source-lyrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface LyricsSearch {
title: string;
artist?: string;
userSearch?: string;
}

export interface SourceLyrics {
getLyrics(input: LyricsSearch): Promise<string>;
}
Loading