src/functions/music.ts (view raw)
1import { createAudioResource, joinVoiceChannel, AudioPlayerStatus, CreateAudioResourceOptions, StreamType } from '@discordjs/voice';
2import MyQueue from './myqueue';
3
4const q = new MyQueue();
5
6export function getChannelConnection(channel) {
7 const guild = channel.guild;
8 return joinVoiceChannel({
9 channelId: channel.id,
10 guildId: guild.id,
11 adapterCreator: guild.voiceAdapterCreator
12 });
13}
14
15export async function playUrls(urls, channel) {
16 if (!channel) {
17 console.log('Channel error:', channel);
18 return;
19 }
20
21 q.connection = getChannelConnection(channel);
22 return q.addArray(urls);
23}
24
25export async function playStream(url, channel) {
26 if (!channel) {
27 console.log('Channel error:', channel);
28 return;
29 }
30 q.connection = getChannelConnection(channel);
31 q.add(createAudioResource(url, { inputType: StreamType.Opus }));
32}
33
34export async function playOutro(url, channel) {
35 if (!channel) {
36 console.log('Channel error:', channel);
37 return;
38 }
39 q.connection = getChannelConnection(channel);
40 q.outro(url);
41}
42
43export async function getChannel(interaction) {
44 const member = interaction.member;
45 if (!member)
46 return 'Please use this in your current server.';
47
48 const channel = member.voice.channel;
49 if (!channel)
50 return 'You\'re not in a voice channel.';
51
52 return channel;
53}
54
55export async function stopMusic() {
56 return q.stop();
57}
58
59export async function skipMusic() {
60 return q.next();
61}
62
63export async function getQueue() {
64 return q.queue;
65}
66
67export function clearQueue() {
68 return q.clear();
69}