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
27 q.connection = getChannelConnection(channel);
28 return await q.addArray(videos);
29}
30/* Only useful for radio
31export async function playStream(url: string, channel: VoiceBasedChannel) {
32 if (!channel) {
33 console.log('Channel error:', channel);
34 return;
35 }
36 q.connection = getChannelConnection(channel);
37 q.add(createAudioResource(url, { inputType: StreamType.Opus }));
38}
39*/
40
41export async function playOutro(url: string, channel: VoiceBasedChannel) {
42 if (!channel) {
43 console.log('Channel error:', channel);
44 return;
45 }
46 q.connection = getChannelConnection(channel);
47 q.outro(url);
48}
49
50export async function getChannel(interaction: ChatInputCommandInteraction): Promise<string | VoiceBasedChannel>{
51 const member = interaction.member;
52 if (!member)
53 return 'Please use this in your current server.';
54
55 const vc_error = 'You\'re not in a voice channel.';
56
57 if (!("voice" in member)) return vc_error;
58 const channel: VoiceBasedChannel = member.voice.channel;
59 return channel ? channel : vc_error;
60}
61
62export async function stopMusic() {
63 return q.stop();
64}
65
66export async function skipMusic() {
67 return q.next();
68}
69
70export async function getQueue() {
71 return q.queue;
72}
73
74export function clearQueue() {
75 return q.clear();
76}