Skip to content

Commit

Permalink
Add non-working fast-forward
Browse files Browse the repository at this point in the history
See #15
  • Loading branch information
callunaborealis committed Apr 5, 2021
1 parent cdd61df commit 4861ac6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ import {
playYouTubeLinkPrefixCommandPatterns,
playYoutubeURLRequests,
} from './music/play/youtube/constants';
import { fastForwardPrefixCommandPatterns } from './music/ff/constants';
import { fastForward } from './music/ff';

const djBotus = new Client();

Expand Down Expand Up @@ -346,6 +348,16 @@ djBotus.on('message', async (message) => {
}
}

if (requestDetails.style === MsgBotRequestStyle.Prefix) {
const fastForwardTrackPrefixDetails = identifyRequest(
messageContent,
fastForwardPrefixCommandPatterns,
);
if (fastForwardTrackPrefixDetails.index !== -1) {
return fastForward(message);
}
}

// Music: Playlist Management
if (interpretRequest(message, playYoutubeURLRequests)) {
return playAndOrAddYoutubeToPlaylist(message);
Expand Down
11 changes: 11 additions & 0 deletions src/music/ff/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { prefixCommandTerminatorPatternStr } from '../../constants';

export const fastForwardPrefixKeywords = ['fast forward', 'fwd', 'ff'];
const fastForwardPrefixCommands: string[] = [
`(?:${fastForwardPrefixKeywords
.map((k) => `(?:${k})`)
.join('|')})${prefixCommandTerminatorPatternStr}`,
];
export const fastForwardPrefixCommandPatterns = fastForwardPrefixCommands.map(
(prefixCommand) => new RegExp(prefixCommand, 'gim'),
);
14 changes: 13 additions & 1 deletion src/music/ff/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Message } from 'discord.js';
import floor from 'lodash/floor';
import isNil from 'lodash/isNil';

import logger from '../../logger';
import { reactWithEmoji } from '../../social';

import { songScaffold } from '../constants';
import { play } from '../play/youtube';
import { defaultPlaylistName, getPlaylist } from '../playlist';

export const fastForward = (message: Message) => {
Expand All @@ -28,4 +30,14 @@ export const fastForward = (message: Message) => {
});
return;
}
const streamTime = playlist.connection?.dispatcher?.streamTime ?? 0;
const streamTotalSecs = floor(streamTime / 1000);
const duration = floor(playlist.currentSong.duration);
const ffDuration = (() => {
if (streamTotalSecs + 10 < duration) {
return streamTotalSecs + 5;
}
return 0;
})();
play(message, { ff: ffDuration, track: playlist.currentSong });
};
Empty file removed src/music/ff/types.ts
Empty file.
14 changes: 12 additions & 2 deletions src/music/play/youtube/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ export const onConnectionFinish = (message: Message) => {
export const play = (
message: Message,
options: {
ff?: number;
track: SongShape;
},
) => {
const { track } = options;
const { ff, track } = options;
if (!message.guild?.id) {
reactWithEmoji.failed(message);
logger.log({
Expand Down Expand Up @@ -109,8 +110,17 @@ export const play = (
return;
}

const trackUrl = (() => {
if (ff) {
const url = new URL(track.url);
url.searchParams.set('t', `${ff}s`);
return url.toString();
}
return track.url;
})();

const dispatcher = playlist.connection
.play(ytdl(track.url, { filter: 'audioonly' }))
.play(ytdl(trackUrl, { filter: 'audioonly' }))
.on('debug', (info) => {
onDebug(message, { info });
})
Expand Down

0 comments on commit 4861ac6

Please sign in to comment.