All files except node_modules

This commit is contained in:
alex_sze 2024-05-09 14:33:27 -04:00 committed by GitHub
parent d3252fad5d
commit 505e52701f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 1186 additions and 0 deletions

1020
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

11
package.json Normal file
View file

@ -0,0 +1,11 @@
{
"dependencies": {
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.17.0",
"discord.js": "^14.15.2",
"node-fetch": "^3.3.2",
"prism-media": "^1.3.5",
"sodium": "^3.0.2",
"ytdl-core": "^4.11.5"
}
}

101
src/bot.js Normal file
View file

@ -0,0 +1,101 @@
const config = require('./config.json')
const Discord = require('discord.js');
const dVC = require('@discordjs/voice');
const prism = require('prism-media');
const fs = require('fs');
const client = new Discord.Client({
intents: [
"Guilds",
"GuildMessages",
"MessageContent",
"GuildVoiceStates",
],
});
const prefix = '!';
client.once('ready', () => {
console.log('Bot is ready!');
});
client.on('messageCreate', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
console.log(`Received command: ${command}`); // Log the received command
if (command === 'livestream') {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply('You need to be in a voice channel to use this command!');
}
fs.readFile('./src/streams/stream.m3u', 'utf8', (err, data) => {
if (err) {
console.error(`Error occurred: ${err}`);
return;
}
// Split the file by newlines and filter out any empty lines
const lines = data.split('\n').filter(line => line.trim() !== '');
// Assume the first line is a URL to an M3U livestream
const streamURL = lines[0];
console.log(`Playing stream: ${streamURL}`);
JoinChannel(voiceChannel, 'https://icecast.thisisdax.com/ClassicFM', 2);
}
) }
});
function JoinChannel(channel, track, volume) {
const connection = dVC.joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: true
});
const resource = dVC.createAudioResource(track, {inlineVolume: true, silencePaddingFrames: 5});
const player = dVC.createAudioPlayer();
resource.volume.setVolume(2);
connection.subscribe(player)
player.play(resource);
connection.on(dVC.VoiceConnectionStatus.Ready, () => {console.log("ready"); player.play(resource);})
connection.on(dVC.VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
try {
console.log("Disconnected.")
await Promise.race([
dVC.entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
dVC.entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
]);
} catch (error) {
connection.destroy();
}
});
player.on('error', error => {
console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`);
player.stop();
});
player.on(dVC.AudioPlayerStatus.Playing, () => {
console.log('The audio player has started playing!');
});
player.on('idle', () => {
connection.destroy();
})
}
function LeaveVoiceChannel(channel) {
// Get resource, player, etc, and destroy them
const connection = dVC.getVoiceConnection(channel.guild.id);
if (connection) {
connection.destroy();
}
}
client.login(config.token).catch(error => console.error(`Login error: ${error}`)); // Log any login errors

View file

@ -0,0 +1,50 @@
const { Readable } = require('stream');
const { createReadStream } = require('fs');
const { join } = require('path');
const { createAudioResource, createAudioPlayer, joinVoiceChannel, AudioPlayerStatus } = require('@discordjs/voice');
module.exports = {
name: 'livestream',
description: 'Plays the audio from the m3u stream in a voice channel',
async execute(message) {
// Check if the user is in a voice channel
if (!message.member.voice.channel) {
return message.reply('You need to be in a voice channel to use this command!');
}
// Join the voice channel
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
// Read the m3u stream file
const streamPath = join(__dirname, '..', 'streams', 'stream.m3u');
const streamFile = createReadStream(streamPath);
// Create a readable stream from the file
const stream = new Readable();
stream._read = () => {};
stream.push(streamFile);
stream.push(null);
// Create an audio resource from the stream
const resource = createAudioResource(stream);
// Create an audio player and play the resource
const player = createAudioPlayer();
player.play(resource);
// Subscribe the player to the connection
connection.subscribe(player);
// Wait for the player to finish playing
await new Promise((resolve) => {
player.on(AudioPlayerStatus.Idle, () => {
connection.destroy();
resolve();
});
});
},
};

3
src/config.json Normal file
View file

@ -0,0 +1,3 @@
{
"token": "InsertTokenHere"
}

1
src/streams/stream.m3u Normal file
View file

@ -0,0 +1 @@
http://icecast.thisisdax.com/ClassicFMMP3.m3u