Move to ESM, use ytsr, implement caching

Closes #315
This commit is contained in:
Max Isom 2021-09-19 22:04:34 -04:00
parent efcdeb78c8
commit fd782219ef
No known key found for this signature in database
GPG key ID: 25C9B1A7F6798880
31 changed files with 314 additions and 158 deletions

View file

@ -2,12 +2,16 @@
"name": "muse", "name": "muse",
"version": "0.1.0", "version": "0.1.0",
"description": "🎧 a self-hosted Discord music bot that doesn't suck ", "description": "🎧 a self-hosted Discord music bot that doesn't suck ",
"main": "dist/index.js", "exports": "./dist/index.js",
"repository": "git@github.com:codetheweb/muse.git", "repository": "git@github.com:codetheweb/muse.git",
"author": "Max Isom <hi@maxisom.me>", "author": "Max Isom <hi@maxisom.me>",
"license": "MIT", "license": "MIT",
"private": true, "private": true,
"types": "dts/types", "types": "dts/types",
"type": "module",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"files": [ "files": [
"dist", "dist",
"dts" "dts"
@ -83,13 +87,15 @@
"node-emoji": "^1.10.0", "node-emoji": "^1.10.0",
"p-event": "^4.2.0", "p-event": "^4.2.0",
"p-limit": "^3.1.0", "p-limit": "^3.1.0",
"p-queue": "^7.1.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sequelize": "^5.22.4", "sequelize": "^5.22.4",
"sequelize-typescript": "^1.1.0", "sequelize-typescript": "^1.1.0",
"spotify-uri": "^2.2.0", "spotify-uri": "^2.2.0",
"spotify-web-api-node": "^5.0.2", "spotify-web-api-node": "^5.0.2",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"youtube.ts": "^0.2.0", "youtube.ts": "^0.2.2",
"ytdl-core": "^4.9.1" "ytdl-core": "^4.9.1",
"ytsr": "^3.5.3"
} }
} }

View file

@ -1,16 +1,16 @@
import {Client, Message, Collection} from 'discord.js'; import {Client, Message, Collection} from 'discord.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {TYPES} from './types'; import {TYPES} from './types.js';
import {Settings, Shortcut} from './models'; import {Settings, Shortcut} from './models/index.js';
import container from './inversify.config'; import container from './inversify.config.js';
import Command from './commands'; import Command from './commands/index.js';
import debug from './utils/debug'; import debug from './utils/debug.js';
import NaturalLanguage from './services/natural-language-commands'; import NaturalLanguage from './services/natural-language-commands.js';
import handleGuildCreate from './events/guild-create'; import handleGuildCreate from './events/guild-create.js';
import handleVoiceStateUpdate from './events/voice-state-update'; import handleVoiceStateUpdate from './events/voice-state-update.js';
import errorMsg from './utils/error-msg'; import errorMsg from './utils/error-msg.js';
import {isUserInVoice} from './utils/channels'; import {isUserInVoice} from './utils/channels.js';
import Config from './services/config'; import Config from './services/config.js';
@injectable() @injectable()
export default class { export default class {

View file

@ -1,7 +1,7 @@
import {Message} from 'discord.js';
import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import {Message} from 'discord.js';
import {TYPES} from '../types.js';
import PlayerManager from '../managers/player.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,7 +1,7 @@
import {TextChannel, Message, GuildChannel} from 'discord.js'; import {TextChannel, Message, GuildChannel} from 'discord.js';
import {injectable} from 'inversify'; import {injectable} from 'inversify';
import {Settings} from '../models'; import {Settings} from '../models/index.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,8 +1,8 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,9 +1,9 @@
import {Message, TextChannel} from 'discord.js'; import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,9 +1,9 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {injectable} from 'inversify'; import {injectable} from 'inversify';
import Command from '.'; import Command from '.';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {Settings} from '../models'; import {Settings} from '../models/index.js';
import container from '../inversify.config'; import container from '../inversify.config.js';
@injectable() @injectable()
export default class implements Command { export default class implements Command {

View file

@ -1,9 +1,9 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import {STATUS} from '../services/player'; import {STATUS} from '../services/player.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,15 +1,15 @@
import {TextChannel, Message} from 'discord.js'; import {TextChannel, Message} from 'discord.js';
import {URL} from 'url'; import {URL} from 'url';
import {Except} from 'type-fest'; import {Except} from 'type-fest';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {QueuedSong, STATUS} from '../services/player'; import {QueuedSong, STATUS} from '../services/player.js';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels'; import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels.js';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
import GetSongs from '../services/get-songs'; import GetSongs from '../services/get-songs.js';
@injectable() @injectable()
export default class implements Command { export default class implements Command {
@ -124,6 +124,7 @@ export default class implements Command {
if (song) { if (song) {
newSongs.push(song); newSongs.push(song);
} else { } else {
console.log(_);
await res.stop(errorMsg('that doesn\'t exist')); await res.stop(errorMsg('that doesn\'t exist'));
return; return;
} }

View file

@ -1,13 +1,13 @@
import {Message, MessageEmbed} from 'discord.js'; import {Message, MessageEmbed} from 'discord.js';
import {TYPES} from '../types';
import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player';
import {STATUS} from '../services/player';
import Command from '.';
import getProgressBar from '../utils/get-progress-bar';
import errorMsg from '../utils/error-msg';
import {prettyTime} from '../utils/time';
import getYouTubeID from 'get-youtube-id'; import getYouTubeID from 'get-youtube-id';
import {inject, injectable} from 'inversify';
import {TYPES} from '../types.js';
import PlayerManager from '../managers/player.js';
import {STATUS} from '../services/player.js';
import Command from '.';
import getProgressBar from '../utils/get-progress-bar.js';
import errorMsg from '../utils/error-msg.js';
import {prettyTime} from '../utils/time.js';
const PAGE_SIZE = 10; const PAGE_SIZE = 10;

View file

@ -1,11 +1,11 @@
import {Message, TextChannel} from 'discord.js'; import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
import {parseTime} from '../utils/time'; import {parseTime} from '../utils/time.js';
@injectable() @injectable()
export default class implements Command { export default class implements Command {

View file

@ -1,7 +1,7 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {injectable} from 'inversify'; import {injectable} from 'inversify';
import {Shortcut, Settings} from '../models'; import {Shortcut, Settings} from '../models/index.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,8 +1,8 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,10 +1,10 @@
import {Message, TextChannel} from 'discord.js'; import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import Command from '.'; import Command from '.';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
@injectable() @injectable()
export default class implements Command { export default class implements Command {

View file

@ -1,8 +1,8 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
@injectable() @injectable()

View file

@ -1,8 +1,8 @@
import {Guild, TextChannel, Message} from 'discord.js'; import {Guild, TextChannel, Message} from 'discord.js';
import emoji from 'node-emoji'; import emoji from 'node-emoji';
import pEvent from 'p-event'; import pEvent from 'p-event';
import {Settings} from '../models'; import {Settings} from '../models/index.js';
import {chunk} from '../utils/arrays'; import {chunk} from '../utils/arrays.js';
const DEFAULT_PREFIX = '!'; const DEFAULT_PREFIX = '!';

View file

@ -1,8 +1,8 @@
import {VoiceState} from 'discord.js'; import {VoiceState} from 'discord.js';
import container from '../inversify.config'; import container from '../inversify.config.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import {getSizeWithoutBots} from '../utils/channels'; import {getSizeWithoutBots} from '../utils/channels.js';
export default (oldState: VoiceState, _: VoiceState): void => { export default (oldState: VoiceState, _: VoiceState): void => {
const playerManager = container.get<PlayerManager>(TYPES.Managers.Player); const playerManager = container.get<PlayerManager>(TYPES.Managers.Player);

View file

@ -1,10 +1,10 @@
import makeDir from 'make-dir'; import makeDir from 'make-dir';
import path from 'path'; import path from 'path';
import container from './inversify.config'; import container from './inversify.config.js';
import {TYPES} from './types'; import {TYPES} from './types.js';
import Bot from './bot'; import Bot from './bot.js';
import {sequelize} from './utils/db'; import {sequelize} from './utils/db.js';
import Config from './services/config'; import Config from './services/config.js';
const bot = container.get<Bot>(TYPES.Bot); const bot = container.get<Bot>(TYPES.Bot);
@ -16,7 +16,7 @@ const bot = container.get<Bot>(TYPES.Bot);
await makeDir(config.CACHE_DIR); await makeDir(config.CACHE_DIR);
await makeDir(path.join(config.CACHE_DIR, 'tmp')); await makeDir(path.join(config.CACHE_DIR, 'tmp'));
await sequelize.sync({}); await sequelize.sync({alter: true});
await bot.listen(); await bot.listen();
})(); })();

View file

@ -1,33 +1,34 @@
import 'reflect-metadata'; import 'reflect-metadata';
import {Container} from 'inversify'; import {Container} from 'inversify';
import {TYPES} from './types'; import {TYPES} from './types.js';
import Bot from './bot'; import Bot from './bot.js';
import {Client} from 'discord.js'; import {Client} from 'discord.js';
import ConfigProvider from './services/config'; import ConfigProvider from './services/config.js';
// Managers // Managers
import PlayerManager from './managers/player'; import PlayerManager from './managers/player.js';
// Helpers // Helpers
import GetSongs from './services/get-songs'; import GetSongs from './services/get-songs.js';
import NaturalLanguage from './services/natural-language-commands'; import NaturalLanguage from './services/natural-language-commands.js';
// Comands // Comands
import Command from './commands'; import Command from './commands';
import Clear from './commands/clear'; import Clear from './commands/clear.js';
import Config from './commands/config'; import Config from './commands/config.js';
import Disconnect from './commands/disconnect'; import Disconnect from './commands/disconnect.js';
import ForwardSeek from './commands/fseek'; import ForwardSeek from './commands/fseek.js';
import Help from './commands/help'; import Help from './commands/help.js';
import Pause from './commands/pause'; import Pause from './commands/pause.js';
import Play from './commands/play'; import Play from './commands/play.js';
import QueueCommad from './commands/queue'; import QueueCommad from './commands/queue.js';
import Seek from './commands/seek'; import Seek from './commands/seek.js';
import Shortcuts from './commands/shortcuts'; import Shortcuts from './commands/shortcuts.js';
import Shuffle from './commands/shuffle'; import Shuffle from './commands/shuffle.js';
import Skip from './commands/skip'; import Skip from './commands/skip.js';
import Unskip from './commands/unskip'; import Unskip from './commands/unskip.js';
import ThirdParty from './services/third-party'; import ThirdParty from './services/third-party.js';
import CacheProvider from './services/cache.js';
let container = new Container(); let container = new Container();
@ -67,4 +68,6 @@ container.bind(TYPES.Config).toConstantValue(new ConfigProvider());
// Static libraries // Static libraries
container.bind(TYPES.ThirdParty).to(ThirdParty); container.bind(TYPES.ThirdParty).to(ThirdParty);
container.bind(TYPES.Cache).to(CacheProvider);
export default container; export default container;

View file

@ -1,8 +1,8 @@
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {TYPES} from '../types';
import Player from '../services/player';
import {Client} from 'discord.js'; import {Client} from 'discord.js';
import Config from '../services/config'; import {TYPES} from '../types.js';
import Player from '../services/player.js';
import Config from '../services/config.js';
@injectable() @injectable()
export default class { export default class {

15
src/models/cache.ts Normal file
View file

@ -0,0 +1,15 @@
import {Table, Column, PrimaryKey, Model} from 'sequelize-typescript';
import sequelize from 'sequelize';
@Table
export default class Cache extends Model<Cache> {
@PrimaryKey
@Column
key!: string;
@Column(sequelize.TEXT)
value!: string;
@Column
expiresAt!: Date;
}

View file

@ -1,7 +1,9 @@
import Settings from './settings'; import Cache from './cache.js';
import Shortcut from './shortcut'; import Settings from './settings.js';
import Shortcut from './shortcut.js';
export { export {
Cache,
Settings, Settings,
Shortcut Shortcut
}; };

52
src/services/cache.ts Normal file
View file

@ -0,0 +1,52 @@
import {injectable} from 'inversify';
import {Cache} from '../models/index.js';
import debug from '../utils/debug.js';
type Seconds = number;
type Options = {
expiresIn: Seconds;
key?: string;
};
const futureTimeToDate = (time: Seconds) => new Date(new Date().getTime() + (time * 1000));
@injectable()
export default class CacheProvider {
async wrap<T extends [...any[], Options], F>(func: (...options: any) => Promise<F>, ...options: T): Promise<F> {
if (options.length === 0) {
throw new Error('Missing cache options');
}
const functionArgs = options.slice(0, options.length - 1);
const {
key = JSON.stringify(functionArgs),
expiresIn
} = options[options.length - 1] as Options;
const cachedResult = await Cache.findByPk(key);
if (cachedResult) {
if (new Date() < cachedResult.expiresAt) {
debug(`Cache hit: ${key}`);
return JSON.parse(cachedResult.value);
}
await cachedResult.destroy();
}
debug(`Cache miss: ${key}`);
const result = await func(...options as any[]);
// Save result
await Cache.upsert({
key,
value: JSON.stringify(result),
expiresAt: futureTimeToDate(expiresIn)
});
return result;
}
}

View file

@ -2,37 +2,73 @@ import {URL} from 'url';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {toSeconds, parse} from 'iso8601-duration'; import {toSeconds, parse} from 'iso8601-duration';
import got from 'got'; import got from 'got';
import ytsr, {Video} from 'ytsr';
import spotifyURI from 'spotify-uri'; import spotifyURI from 'spotify-uri';
import Spotify from 'spotify-web-api-node'; import Spotify from 'spotify-web-api-node';
import YouTube, {YoutubePlaylistItem} from 'youtube.ts'; import YouTube, {YoutubePlaylistItem} from 'youtube.ts';
import pLimit from 'p-limit'; import PQueue from 'p-queue';
import shuffle from 'array-shuffle'; import shuffle from 'array-shuffle';
import {Except} from 'type-fest'; import {Except} from 'type-fest';
import {QueuedSong, QueuedPlaylist} from '../services/player'; import {QueuedSong, QueuedPlaylist} from '../services/player.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import {cleanUrl} from '../utils/url'; import {cleanUrl} from '../utils/url.js';
import ThirdParty from './third-party'; import ThirdParty from './third-party.js';
import Config from './config'; import Config from './config.js';
import CacheProvider from './cache.js';
type QueuedSongWithoutChannel = Except<QueuedSong, 'addedInChannelId'>; type QueuedSongWithoutChannel = Except<QueuedSong, 'addedInChannelId'>;
const ONE_HOUR_IN_SECONDS = 60 * 60;
const ONE_MINUTE_IN_SECONDS = 1 * 60;
@injectable() @injectable()
export default class { export default class {
private readonly youtube: YouTube; private readonly youtube: YouTube;
private readonly youtubeKey: string; private readonly youtubeKey: string;
private readonly spotify: Spotify; private readonly spotify: Spotify;
private readonly cache: CacheProvider;
constructor(@inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.Config) config: Config) { private readonly ytsrQueue: PQueue;
constructor(
@inject(TYPES.ThirdParty) thirdParty: ThirdParty,
@inject(TYPES.Config) config: Config,
@inject(TYPES.Cache) cache: CacheProvider) {
this.youtube = thirdParty.youtube; this.youtube = thirdParty.youtube;
this.youtubeKey = config.YOUTUBE_API_KEY; this.youtubeKey = config.YOUTUBE_API_KEY;
this.spotify = thirdParty.spotify; this.spotify = thirdParty.spotify;
this.cache = cache;
this.ytsrQueue = new PQueue({concurrency: 4});
} }
async youtubeVideoSearch(query: string): Promise<QueuedSongWithoutChannel|null> { async youtubeVideoSearch(query: string): Promise<QueuedSongWithoutChannel|null> {
try { try {
const {items: [video]} = await this.youtube.videos.search({q: query, maxResults: 1, type: 'video'}); const {items} = await this.ytsrQueue.add(async () => this.cache.wrap(
ytsr,
query,
{
limit: 10
},
{
expiresIn: ONE_HOUR_IN_SECONDS
}
));
return await this.youtubeVideo(video.id.videoId); let firstVideo: Video | undefined;
for (const item of items) {
if (item.type === 'video') {
firstVideo = item;
break;
}
}
if (!firstVideo) {
throw new Error('No video found.');
}
return await this.youtubeVideo(firstVideo.id);
} catch (_: unknown) { } catch (_: unknown) {
return null; return null;
} }
@ -40,7 +76,13 @@ export default class {
async youtubeVideo(url: string): Promise<QueuedSongWithoutChannel|null> { async youtubeVideo(url: string): Promise<QueuedSongWithoutChannel|null> {
try { try {
const videoDetails = await this.youtube.videos.get(cleanUrl(url)); const videoDetails = await this.cache.wrap(
this.youtube.videos.get,
cleanUrl(url),
{
expiresIn: ONE_HOUR_IN_SECONDS
}
);
return { return {
title: videoDetails.snippet.title, title: videoDetails.snippet.title,
@ -57,7 +99,13 @@ export default class {
async youtubePlaylist(listId: string): Promise<QueuedSongWithoutChannel[]> { async youtubePlaylist(listId: string): Promise<QueuedSongWithoutChannel[]> {
// YouTube playlist // YouTube playlist
const playlist = await this.youtube.playlists.get(listId); const playlist = await this.cache.wrap(
this.youtube.playlists.get,
listId,
{
expiresIn: ONE_MINUTE_IN_SECONDS
}
);
interface VideoDetailsResponse { interface VideoDetailsResponse {
id: string; id: string;
@ -75,7 +123,14 @@ export default class {
while (playlistVideos.length !== playlist.contentDetails.itemCount) { while (playlistVideos.length !== playlist.contentDetails.itemCount) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const {items, nextPageToken} = await this.youtube.playlists.items(listId, {maxResults: '50', pageToken: nextToken}); const {items, nextPageToken} = await this.cache.wrap(
this.youtube.playlists.items,
listId,
{maxResults: '50', pageToken: nextToken},
{
expiresIn: ONE_MINUTE_IN_SECONDS
}
);
nextToken = nextPageToken; nextToken = nextPageToken;
@ -84,11 +139,24 @@ export default class {
// Start fetching extra details about videos // Start fetching extra details about videos
videoDetailsPromises.push((async () => { videoDetailsPromises.push((async () => {
// Unfortunately, package doesn't provide a method for this // Unfortunately, package doesn't provide a method for this
const {items: videoDetailItems}: {items: VideoDetailsResponse[]} = await got('https://www.googleapis.com/youtube/v3/videos', {searchParams: { const {items: videoDetailItems} = await this.cache.wrap(
() => {
return got(
'https://www.googleapis.com/youtube/v3/videos',
{
searchParams: {
part: 'contentDetails', part: 'contentDetails',
id: items.map(item => item.contentDetails.videoId).join(','), id: items.map(item => item.contentDetails.videoId).join(','),
key: this.youtubeKey key: this.youtubeKey,
}}).json(); responseType: 'json'
}
}
).json();
},
{
expiresIn: ONE_MINUTE_IN_SECONDS
}
);
videoDetails.push(...videoDetailItems); videoDetails.push(...videoDetailItems);
})()); })());
@ -193,9 +261,7 @@ export default class {
tracks = shuffled.slice(0, 50); tracks = shuffled.slice(0, 50);
} }
// Limit concurrency so hopefully we don't get banned for searching let songs = await Promise.all(tracks.map(async track => this.spotifyToYouTube(track, playlist)));
const limit = pLimit(5);
let songs = await Promise.all(tracks.map(async track => limit(async () => this.spotifyToYouTube(track, playlist))));
let nSongsNotFound = 0; let nSongsNotFound = 0;
@ -215,14 +281,7 @@ export default class {
private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, _: QueuedPlaylist | null): Promise<QueuedSongWithoutChannel | null> { private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, _: QueuedPlaylist | null): Promise<QueuedSongWithoutChannel | null> {
try { try {
const {items} = await this.youtube.videos.search({q: `"${track.name}" "${track.artists[0].name}"`, maxResults: 10}); return await this.youtubeVideoSearch(`"${track.name}" "${track.artists[0].name}"`);
const videoResult = items[0];
if (!videoResult) {
throw new Error('No video found for query.');
}
return await this.youtubeVideo(videoResult.id.videoId);
} catch (_: unknown) { } catch (_: unknown) {
return null; return null;
} }

View file

@ -1,9 +1,9 @@
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {Message, Guild, GuildMember} from 'discord.js'; import {Message, Guild, GuildMember} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player.js';
import {QueuedSong} from '../services/player'; import {QueuedSong} from '../services/player.js';
import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels'; import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels.js';
@injectable() @injectable()
export default class { export default class {

View file

@ -7,7 +7,7 @@ import ytdl from 'ytdl-core';
import {WriteStream} from 'fs-capacitor'; import {WriteStream} from 'fs-capacitor';
import ffmpeg from 'fluent-ffmpeg'; import ffmpeg from 'fluent-ffmpeg';
import shuffle from 'array-shuffle'; import shuffle from 'array-shuffle';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg.js';
export interface QueuedPlaylist { export interface QueuedPlaylist {
title: string; title: string;

View file

@ -1,8 +1,8 @@
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import SpotifyWebApi from 'spotify-web-api-node'; import SpotifyWebApi from 'spotify-web-api-node';
import Youtube from 'youtube.ts'; import Youtube from 'youtube.ts/dist/youtube.js';
import {TYPES} from '../types'; import {TYPES} from '../types.js';
import Config from './config'; import Config from './config.js';
@injectable() @injectable()
export default class ThirdParty { export default class ThirdParty {
@ -12,7 +12,8 @@ export default class ThirdParty {
private spotifyTokenTimerId?: NodeJS.Timeout; private spotifyTokenTimerId?: NodeJS.Timeout;
constructor(@inject(TYPES.Config) config: Config) { constructor(@inject(TYPES.Config) config: Config) {
this.youtube = new Youtube(config.YOUTUBE_API_KEY); // Library is transpiled incorrectly
this.youtube = new ((Youtube as any).default)(config.YOUTUBE_API_KEY);
this.spotify = new SpotifyWebApi({ this.spotify = new SpotifyWebApi({
clientId: config.SPOTIFY_CLIENT_ID, clientId: config.SPOTIFY_CLIENT_ID,
clientSecret: config.SPOTIFY_CLIENT_SECRET clientSecret: config.SPOTIFY_CLIENT_SECRET

View file

@ -1,5 +1,6 @@
export const TYPES = { export const TYPES = {
Bot: Symbol('Bot'), Bot: Symbol('Bot'),
Cache: Symbol('Cache'),
Client: Symbol('Client'), Client: Symbol('Client'),
Config: Symbol('Config'), Config: Symbol('Config'),
Command: Symbol('Command'), Command: Symbol('Command'),

View file

@ -1,12 +1,12 @@
import {Sequelize} from 'sequelize-typescript'; import {Sequelize} from 'sequelize-typescript';
import path from 'path'; import path from 'path';
import {DATA_DIR} from '../services/config'; import {DATA_DIR} from '../services/config.js';
import {Settings, Shortcut} from '../models'; import {Cache, Settings, Shortcut} from '../models/index.js';
export const sequelize = new Sequelize({ export const sequelize = new Sequelize({
dialect: 'sqlite', dialect: 'sqlite',
database: 'muse', database: 'muse',
storage: path.join(DATA_DIR, 'db.sqlite'), storage: path.join(DATA_DIR, 'db.sqlite'),
models: [Settings, Shortcut], models: [Cache, Settings, Shortcut],
logging: false logging: false
}); });

View file

@ -2,7 +2,7 @@
"compilerOptions": { "compilerOptions": {
"lib": ["ES2019"], "lib": ["ES2019"],
"target": "es2018", "target": "es2018",
"module": "commonjs", "module": "ES2020",
"moduleResolution": "node", "moduleResolution": "node",
"declaration": true, "declaration": true,
"outDir": "dist", "outDir": "dist",

View file

@ -1153,6 +1153,11 @@ event-target-shim@^5.0.0:
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ== integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==
eventemitter3@^4.0.7:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
extend@~3.0.2: extend@~3.0.2:
version "3.0.2" version "3.0.2"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa" resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"
@ -1997,7 +2002,7 @@ mimic-response@^3.1.0:
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-3.1.0.tgz#2d1d59af9c1b129815accc2c46a022a5ce1fa3c9" resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-3.1.0.tgz#2d1d59af9c1b129815accc2c46a022a5ce1fa3c9"
integrity sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ== integrity sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==
miniget@^4.0.0: miniget@^4.0.0, miniget@^4.2.1:
version "4.2.1" version "4.2.1"
resolved "https://registry.yarnpkg.com/miniget/-/miniget-4.2.1.tgz#11a1c24817a059e292378eb9cff4328d9240c665" resolved "https://registry.yarnpkg.com/miniget/-/miniget-4.2.1.tgz#11a1c24817a059e292378eb9cff4328d9240c665"
integrity sha512-O/DduzDR6f+oDtVype9S/Qu5hhnx73EDYGyZKwU/qN82lehFZdfhoa4DT51SpsO+8epYrB3gcRmws56ROfTIoQ== integrity sha512-O/DduzDR6f+oDtVype9S/Qu5hhnx73EDYGyZKwU/qN82lehFZdfhoa4DT51SpsO+8epYrB3gcRmws56ROfTIoQ==
@ -2332,6 +2337,14 @@ p-locate@^5.0.0:
dependencies: dependencies:
p-limit "^3.0.2" p-limit "^3.0.2"
p-queue@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/p-queue/-/p-queue-7.1.0.tgz#c2bb28f8dc0ebf3fadb985b8706cf2ce5fe5f275"
integrity sha512-V+0vPJbhYkBqknPp0qnaz+dWcj8cNepfXZcsVIVEHPbFQXMPwrzCNIiM4FoxGtwHXtPzVCPHDvqCr1YrOJX2Gw==
dependencies:
eventemitter3 "^4.0.7"
p-timeout "^5.0.0"
p-timeout@^3.1.0: p-timeout@^3.1.0:
version "3.2.0" version "3.2.0"
resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-3.2.0.tgz#c7e17abc971d2a7962ef83626b35d635acf23dfe" resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-3.2.0.tgz#c7e17abc971d2a7962ef83626b35d635acf23dfe"
@ -2339,6 +2352,11 @@ p-timeout@^3.1.0:
dependencies: dependencies:
p-finally "^1.0.0" p-finally "^1.0.0"
p-timeout@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-5.0.0.tgz#0f5dc08422e7243f8317669c461734cd1257a8dc"
integrity sha512-z+bU/N7L1SABsqKnQzvAnINgPX7NHdzwUV+gHyJE7VGNDZSr03rhcPODCZSWiiT9k+gf74QPmzcZzqJRvxYZow==
package-json@^6.3.0: package-json@^6.3.0:
version "6.5.0" version "6.5.0"
resolved "https://registry.yarnpkg.com/package-json/-/package-json-6.5.0.tgz#6feedaca35e75725876d0b0e64974697fed145b0" resolved "https://registry.yarnpkg.com/package-json/-/package-json-6.5.0.tgz#6feedaca35e75725876d0b0e64974697fed145b0"
@ -3287,22 +3305,13 @@ yocto-queue@^0.1.0:
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
youtube.ts@^0.2.0: youtube.ts@^0.2.2:
version "0.2.0" version "0.2.2"
resolved "https://registry.yarnpkg.com/youtube.ts/-/youtube.ts-0.2.0.tgz#a6cbc153bda9aa712a10098b22060877fb2995f3" resolved "https://registry.yarnpkg.com/youtube.ts/-/youtube.ts-0.2.2.tgz#d068bd13da7d2a98743d78682d54535d9c1a2833"
integrity sha512-cRHN/7L9FO+DkdKTzvUAiB/hQo+ZtzLb4GfVywPTTtf9JBol87fqzLKf86cBWwuXLOJzJNLB/wvZmUmsxpbByQ== integrity sha512-2p5RGp66h77XlLlc2YyxSDU1Gql9cWDhq35TS1kAr5wSU/yIWxeXeuqKyKaFBW8uNqWUkQCiyUbn5tfqPgyzeQ==
dependencies: dependencies:
axios "^0.19.0" axios "^0.19.0"
ytdl-core "^4.0.3" ytdl-core "^4.9.1"
ytdl-core@^4.0.3:
version "4.8.3"
resolved "https://registry.yarnpkg.com/ytdl-core/-/ytdl-core-4.8.3.tgz#21570d1834db13dec7828cf87bbf4c83c0fe68d7"
integrity sha512-cWCBeX4FCgjcKmuVK384MT582RIAakpUSeMF/NPVmhO8cWiG+LeQLnBordvLolb0iXYzfUvalgmycYAE5Sy6Xw==
dependencies:
m3u8stream "^0.8.3"
miniget "^4.0.0"
sax "^1.1.3"
ytdl-core@^4.9.1: ytdl-core@^4.9.1:
version "4.9.1" version "4.9.1"
@ -3312,3 +3321,10 @@ ytdl-core@^4.9.1:
m3u8stream "^0.8.3" m3u8stream "^0.8.3"
miniget "^4.0.0" miniget "^4.0.0"
sax "^1.1.3" sax "^1.1.3"
ytsr@^3.5.3:
version "3.5.3"
resolved "https://registry.yarnpkg.com/ytsr/-/ytsr-3.5.3.tgz#88e8e2df11ce53c28b456b5510272495cb42ac3a"
integrity sha512-BEyIKbQULmk27hiVUQ1cBszAqP8roPBOQTWPZpBioKxjSZBeicfgF2qPIQoY7koodQwRuo1DmCFz3DyrXjADxg==
dependencies:
miniget "^4.2.1"