Added support for seeking when playing youtube videos.

This commit is contained in:
moosecrab 2022-06-09 16:42:35 -07:00
parent d4f06c19ff
commit ea1c62b5b6

6
bot.js
View File

@ -7,6 +7,8 @@ const fs = require('fs');
const ytdl = require('ytdl-core-discord'); const ytdl = require('ytdl-core-discord');
// Include our local auth key, so that we don't leak keys // Include our local auth key, so that we don't leak keys
const auth = require('./auth.json'); const auth = require('./auth.json');
// Volume for playback (fraction)
var volume = 0.6;
// **** PROGRAM START **** // **** PROGRAM START ****
// This program is 2 lines of code, the rest is callback function definition, wherein the bot magic happens // This program is 2 lines of code, the rest is callback function definition, wherein the bot magic happens
@ -151,7 +153,7 @@ client.on('message', async message => {
const ytmatch = message.content.match(/(?<=!play (https?:\/\/)(www\.)?youtu((be\.com\/watch\?v=)|(\.be\/)))[\w-]{11}/); const ytmatch = message.content.match(/(?<=!play (https?:\/\/)(www\.)?youtu((be\.com\/watch\?v=)|(\.be\/)))[\w-]{11}/);
if (ytmatch && message.member.voice.channel) { if (ytmatch && message.member.voice.channel) {
const timematch = message.content.match(/t=(\d{1,2}h)?(\d{1,2}m)?\d{1,4}s?(?=$)/); const timematch = message.content.match(/t=(\d{1,2}h)?(\d{1,2}m)?\d{1,4}s?(?=$)/);
var timestamp = ''; var timestamp = '0';
if (timematch) { if (timematch) {
timestamp = timematch[0]; timestamp = timematch[0];
} }
@ -163,7 +165,7 @@ client.on('message', async message => {
// Play the video as requested // Play the video as requested
try { try {
//TODO: process the timestamp with ffmpeg or whatever //TODO: process the timestamp with ffmpeg or whatever
const dispatcher = connection.play(await ytdl(yturl), { type: 'opus', volume: 0.60 }); const dispatcher = connection.play(await ytdl(yturl), { type: 'opus', volume: 0.60, begin: timestamp});
dispatcher.on('start', () => { console.log(`Playing ${yturl}`); }); dispatcher.on('start', () => { console.log(`Playing ${yturl}`); });
dispatcher.on('finish', () => { console.log(`Finished ${yturl}`); }); dispatcher.on('finish', () => { console.log(`Finished ${yturl}`); });
dispatcher.on('error', console.error); dispatcher.on('error', console.error);