all repos — simple-discord-music-bot @ ee48e4f6561bef07c4fda9326fd805761a0030fd

A Discord bot making use of discord.js and play-yt.

src/functions/music.ts (view raw)

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