-
Notifications
You must be signed in to change notification settings - Fork 2
/
MusicPlugin.ts
201 lines (188 loc) · 6.58 KB
/
MusicPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type { ChatEvent } from "../models/ChatEvent";
import type { JimmiApi } from "../models/JimmiApi";
import type { SearchEntry } from "../models/invidious/SearchEntry";
import type { VideoResponse } from "../models/invidious/VideoResponse";
import type { IJimmiEventMap } from "../models/JimmiPlugin";
import { JimmiPlugin } from "../models/JimmiPlugin";
import { Track } from "../models/Track";
export default class MusicPlugin extends JimmiPlugin {
readonly meta = {
id: "xyz.jimmi.music",
name: "Music",
version: "0.2.0",
};
readonly commands = {
"play": this.play,
"queue": this.queue,
"cue": this.queue, // alias :)
"track": this.track,
};
readonly events?: IJimmiEventMap | undefined;
readonly translations = {
en: {
description: "A music plugin that allows for audio playback of youtube videos",
commands: {
play: {
usage: "!play <url|searchTerm> - Play a youtube video by url or search term",
playingTrack: "Playing {title}"
},
queue: {
usage: `!queue - Show the current queue.
!queue <url|searchTerm> - Add a YouTube video to the queue.`,
isEmpty: "The queue is currently empty",
content: "Queue content",
addedTrack: "Added {title} to queue"
},
cue: {
usage: "!cue - Alias for !queue",
},
track: {
usage: `!track - Display current track
!track skip - Skip the current track
!track ++ or !track -- - Fast forward or rewind. Add more + or - signs to increase duration
!track +10, !track +20, !track -15 - Fast forward or rewind with specific duration in seconds`,
currentlyPlaying: "Currently playing {title}"
}
},
general: {
invalidArgs: "Invalid arguments. Usage:",
noVideo: "Sorry, I cannot find any video at the moment",
}
},
};
private chooseRandomBaseUrl(): string {
const instances = ["https://vid.puffyan.us", "https://invidious.flokinet.to", "https://inv.bp.projectsegfau.lt", "https://yt.artemislena.eu", "https://invidious.projectsegfau.lt", "https://invidious.lunar.icu"];
return instances[Math.floor(instances.length * Math.random())];
}
/**
* Internal fetch method that contructs the API url and formats the response as json
*
* @param endpoint - The endpoint to fetch
* @returns Promise of the given type wich describes the json response
*/
private async fetch<T>(endpoint: string): Promise<T> {
const res = await fetch(`${this.chooseRandomBaseUrl()}/api/v1/${endpoint}`, {
referrerPolicy: 'no-referrer',
});
return await res.json();
}
/**
* Retrieve youtube video ID from search term
*
* @param search - The term to search for
* @returns The ID of the first search result or null if nothing was found.
*/
private async searchYtVideo(search: string): Promise<string | null> {
if (new RegExp('https?:\/\/').test(search)) {
// search query is link. Try to get video param
const id = new URLSearchParams(search.split('?')[1]).get('v');
if (id) return id;
// id is not in query parameter. Return the last part of the url path
const paths = search.split('/');
return paths[paths.length - 1].split('?')[0];
} else {
const res = await this.fetch<SearchEntry[]>(`search/?q=${encodeURIComponent(search)}`);
if (res.length > 0) {
return res[0].videoId;
}
}
return null;
}
/**
* Retrieve details of a given youtube video
*
* @param videoId - The video ID
* @returns The resulting track
*/
private async getTrack(videoId: string): Promise<Track | null> {
const res = await this.fetch<VideoResponse>(`videos/${videoId}`);
if (res.error) {
return null;
}
return new Track(res);
}
private async query(params: string[]): Promise<Track | null> {
const query = params.join(' ');
const videoId = await this.searchYtVideo(query);
if (!videoId) {
return null;
}
return this.getTrack(videoId);
}
async play(event: ChatEvent): Promise<void> {
if (event.params.length === 0) {
event.respond(`:warning: ${this.$t('general.invalidArgs')}\n${this.$t('commands.play.usage')}`);
} else {
const track = await this.query(event.params);
if (!track) {
event.respond(`${this.$t('general.noVideo')} :worried:`)
return;
}
this.api.currentTrack = track;
this.api.conference.sendMessage(
`:notes: ${this.$t('commands.play.playingTrack', { values: { title: track.title } })}`
);
}
}
async queue(event: ChatEvent): Promise<void> {
if (event.params.length === 0) {
if (this.api.queue.length === 0) {
event.respond(`:notes: ${this.$t('commands.queue.isEmpty')}`);
} else {
let idx = 0;
const numRegex = new RegExp(/(\d)/ig)
// print human readable queue into chat
event.respond(this.api.queue.reduce((msg, track) =>
`${msg}\n${(++idx).toString().replaceAll(numRegex, ":$1:")} ${track.title}`,
`:notes: ${this.$t('commands.queue.content')}`
));
}
} else {
const track = await this.query(event.params);
if (!track) {
event.respond(`${this.$t('general.noVideo')} :worried:`)
return;
}
if (this.api.currentTrack) {
this.api.addToQueue(track);
} else {
this.api.currentTrack = track;
}
event.respond(
`:notes: ${this.$t('commands.queue.addedTrack', { values: { title: track.title } })}`
);
}
}
async track(event: ChatEvent) {
switch (event.params.length) {
case 0:
event.respond(
`:notes: ${this.$t('commands.track.currentlyPlaying', { values: { title: this.api.currentTrack?.title ?? 'nothing' } })}`
);
break;
case 1:
const param = event.params[0];
if (param === "skip") {
this.api.currentTrack = undefined;
return;
}
let seconds = 0;
const f = (x: number) => 2 * Math.E ^ (x - 1); // non-linear function to fast forward
if (/^\+\++$/.test(param)) {
seconds = f(param.length)
} else if (/^--+$/.test(param)) {
seconds = -f(param.length)
} else {
seconds = parseInt(param, 10);
}
if (!seconds) {
event.respond(`:warning: ${this.$t('general.invalidArgs')}\n${this.$t('commands.track.usage')}`);
} else {
this.api.forward(seconds);
}
break;
default:
event.respond(`:warning: ${this.$t('general.invalidArgs')}\n${this.$t('commands.track.usage')}`);
}
}
}