src/functions/music.ts (view raw)
1import { joinVoiceChannel } from '@discordjs/voice';
2import { ChatInputCommandInteraction, VoiceBasedChannel } from 'discord.js'
3import MyQueue from './myqueue';
4import { YouTubeVideo } from 'play-dl';
5
6const q = new MyQueue();
7
8export function formatTitle(video: YouTubeVideo): string {
9 return `**${video.title}** (\`${video.durationRaw}\`)`
10}
11
12export function getChannelConnection(channel: VoiceBasedChannel) {
13 const guild = channel.guild;
14 return joinVoiceChannel({
15 channelId: channel.id,
16 guildId: guild.id,
17 adapterCreator: guild.voiceAdapterCreator
18 });
19}
20
21export async function playUrls(videos: YouTubeVideo[], channel: VoiceBasedChannel): Promise<YouTubeVideo[]> {
22 if (!channel) {
23 console.log('Channel error:', channel);
24 return;
25 }
26 if (videos.length == 0) return [];
27 q.connection = getChannelConnection(channel);
28 return await q.addArray(videos);
29}
30
31/* Only useful for radio
32export async function playStream(url: string, channel: VoiceBasedChannel) {
33 if (!channel) {
34 console.log('Channel error:', channel);
35 return;
36 }
37 q.connection = getChannelConnection(channel);
38 q.add(createAudioResource(url, { inputType: StreamType.Opus }));
39}
40*/
41
42export async function playOutro(url: string, channel: VoiceBasedChannel) {
43 if (!channel) {
44 console.log('Channel error:', channel);
45 return;
46 }
47 q.connection = getChannelConnection(channel);
48 q.outro(url);
49}
50
51export async function getChannel(interaction: ChatInputCommandInteraction): Promise<string | VoiceBasedChannel>{
52 const member = interaction.member;
53 if (!member)
54 return 'Please use this in your current server.';
55
56 const vc_error = 'You\'re not in a voice channel.';
57
58 if (!("voice" in member)) return vc_error;
59 const channel: VoiceBasedChannel = member.voice.channel;
60 return channel ? channel : vc_error;
61}
62
63export async function stopMusic() {
64 return q.stop();
65}
66
67export async function skipMusic() {
68 return q.next();
69}
70
71export async function getQueue() {
72 return q.queue;
73}
74
75export function clearQueue() {
76 return q.clear();
77}