This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
125 lines (99 loc) · 3.74 KB
/
index.js
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
const { Client, Intents } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { joinVoiceChannel, createAudioResource, createAudioPlayer, NoSubscriberBehavior, AudioPlayerStatus, } = require('@discordjs/voice');
const ytdl = require('ytdl-core');
const { token, dj, targetChannel, clientID, guildID } = require('./config.json');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES],
presence: {
activities: [{ name: 'https://github.com/lezetho/YouTube-Radio!', type: 'WATCHING' }],
},
});
const targetChannelId = targetChannel;
const allowedRoleId = dj;
const commands = [
{
name: 'play-radio',
description: 'Turn on the radio!',
},
];
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(`${clientID}`, `${guildID}`),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'play-radio') {
const member = interaction.member;
if (!member.roles.cache.has(allowedRoleId)) {
return interaction.reply('You do not have the required role to use this command.');
}
const voiceChannel = member.voice.channel;
if (!voiceChannel || (targetChannelId && voiceChannel.id !== targetChannelId)) {
return interaction.reply('You need to be in the specified voice channel first!');
}
try {
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: interaction.guildId,
adapterCreator: interaction.guild.voiceAdapterCreator,
});
const videoUrls = [
'https://www.youtube.com/watch?v=tPEE9ZwTmy0', // example URL
];
let currentIndex = 0;
while (true) {
const videoUrl = videoUrls[currentIndex];
try {
console.log(`Fetching video information for ${videoUrl}...`);
const stream = ytdl(videoUrl, { filter: 'audioonly' });
if (!stream) {
console.error(`Failed to get stream for ${videoUrl}. Skipping.`);
continue;
}
const resource = createAudioResource(stream);
const player = createAudioPlayer();
player.on(AudioPlayerStatus.Idle, () => {
console.log(`Finished playing ${videoUrl}`);
currentIndex = (currentIndex + 1) % videoUrls.length;
});
player.on('error', error => {
if (error.message.includes('Status code: 403')) {
console.error(`Error playing ${videoUrl}, skipping and playing a different video`);
} else {
console.error(`Error in player for ${videoUrl}:`, error);
}
currentIndex = (currentIndex + 1) % videoUrls.length;
});
connection.subscribe(player);
player.play(resource);
await new Promise(resolve => {
player.once(AudioPlayerStatus.Idle, () => {
resolve();
});
});
} catch (error) {
console.error(`Error processing video ${videoUrl}:`, error);
}
}
} catch (error) {
console.error('Error handling interaction:', error);
interaction.reply('Failed to join voice channel.');
}
}
});
client.login(token);