Larger refactoring and added avoidlist

This commit is contained in:
Myx
2023-10-21 05:09:38 +02:00
parent ccf7a218fd
commit 8fd3eeae03
30 changed files with 753 additions and 213 deletions

View File

@@ -0,0 +1,20 @@
import * as fileSystem from 'fs';
import express from 'express';
import { loadAvoidList } from '../../helpers/load-avoid-list';
/**
* Adds a user to the avoid list.
* @param user - The user to add to the avoid list.
* @returns void
*/
export function AddUserToAvoidList(response: express.Response, request: express.Request) {
const avoidList = loadAvoidList();
if (avoidList.avoidUsers.includes(request.body.user)) {
response.send('User already in avoid list.');
} else {
avoidList.avoidUsers.push(request.body.user);
fileSystem.writeFileSync('avoid-list.json', JSON.stringify(avoidList, null, "\t"));
response.send('User added to avoid list.');
}
}

View File

@@ -0,0 +1,19 @@
import express from 'express';
import path from 'path';
import * as fileSystem from 'fs';
import { LoggerColors } from '../../helpers/logger-colors';
/**
* Deletes a file from the sounds folder by filename
*/
export function DeleteSoundFile(response: express.Response, request: express.Request) {
const directoryPath = path.join(__dirname, '../../sounds');
const filePath = directoryPath + '/' + request.params.filename;
fileSystem.unlink(filePath, (err: any) => {
if (err) {
return console.log(LoggerColors.Red, 'Unable to delete file: ' + err);
}
response.send('File deleted successfully.');
});
}

View File

@@ -0,0 +1,20 @@
import express from 'express';
import fs from 'fs';
import { loadAvoidList } from '../../helpers/load-avoid-list';
/**
* Removes a user from the avoid list.
* @param user - The user to remove from the avoid list.
* @returns void
*/
export function DeleteUserFromAvoidList(response: express.Response, request: express.Request) {
const avoidList = loadAvoidList();
if (avoidList.avoidUsers.includes(request.params.user)) {
avoidList.avoidUsers = avoidList.avoidUsers.filter(user => user !== request.params.user);
fs.writeFileSync('avoid-list.json', JSON.stringify(avoidList, null, "\t"));
response.send('User removed from avoid list.');
} else {
response.send('User not in avoid list.');
}
}

View File

@@ -0,0 +1,18 @@
import express from 'express';
import path from 'path';
import * as fileSystem from 'fs';
import { LoggerColors } from '../../helpers/logger-colors';
/**
* Returns a list of all sound files in the sounds directory.
* @returns string[] - An array of all sound files in the sounds directory.
*/
export function GetSoundFiles(response: express.Response) {
const directoryPath = path.join(__dirname, '../../sounds');
fileSystem.readdir(directoryPath, function (error: any, files: any[]) {
if (error) {
return console.log(LoggerColors.Red, 'Unable to scan directory: ' + error);
}
response.send(files);
});
}

15
client/handlers/index.ts Normal file
View File

@@ -0,0 +1,15 @@
import { AddUserToAvoidList } from './addUserToAvoidList';
import { DeleteSoundFile } from './deleteSoundFile';
import { GetSoundFiles } from './getSoundFiles';
import { UploadYouTubeFile } from './uploadYouTubeFile';
import { DeleteUserFromAvoidList } from './deleteUserFromAvoidList';
import { JoinChannel } from './joinChannel';
export class Handlers {
public static AddUserToAvoidList = AddUserToAvoidList;
public static DeleteSoundFile = DeleteSoundFile;
public static GetSoundFiles = GetSoundFiles;
public static UploadYouTubeFile = UploadYouTubeFile;
public static DeleteUserFromAvoidList = DeleteUserFromAvoidList;
public static JoinChannel = JoinChannel;
}

View File

@@ -0,0 +1,11 @@
import { joinRandomChannel } from "../../bot";
import express from 'express';
/**
* Joins a random channel and plays a random sound file.
* @returns void
*/
export function JoinChannel(response: express.Response) {
joinRandomChannel();
response.send('Joining random channel.');
}

View File

@@ -0,0 +1,60 @@
import express from 'express';
import path from 'path';
import ytdl from 'ytdl-core';
import * as fileSystem from 'fs';
import ffmpeg from 'fluent-ffmpeg';
import { generateFileName } from '../../helpers/generate-file-name';
/**
* Uploads a YouTube video as an mp3 file to the sounds folder.
* The video must be shorter than 10 seconds.
* @Body url - The YouTube video url.
*/
export async function UploadYouTubeFile(response: express.Response, request: express.Request) {
const url = request.body.url;
if (ytdl.validateURL(url)) {
const info = await ytdl.getInfo(url);
// remove special characters from the title and white spaces
const title = info.videoDetails.title.replace(/[^a-zA-Z ]/g, "").replace(/\s+/g, '-').toLowerCase();
// Create a temporary directory to store the uploaded file so validation can be done
const tempDir = fileSystem.mkdtempSync('temp');
const outputFilePath = path.resolve(tempDir, generateFileName(title));
const videoReadableStream = ytdl(url, { filter: 'audioonly' });
const fileWritableStream = fileSystem.createWriteStream(outputFilePath);
videoReadableStream.pipe(fileWritableStream);
fileWritableStream.on('finish', () => {
ffmpeg.ffprobe(outputFilePath, function (err, metadata) {
if (err) {
fileSystem.rmSync(tempDir, { recursive: true, force: true });
return response.status(500).send('Error occurred during processing.');
}
const duration = metadata.format.duration;
if (duration == undefined) {
fileSystem.rmSync(tempDir, { recursive: true, force: true });
return response.status(400).send('Something went wrong.');
}
if (duration > 10) {
fileSystem.rmSync(tempDir, { recursive: true, force: true });
return response.status(400).send('File is longer than 10 seconds.');
} else {
// Move the file from the temporary directory to its final destination
const finalFilePath = path.resolve(__dirname, '../../sounds/', generateFileName(title));
fileSystem.renameSync(outputFilePath, finalFilePath);
response.send('File uploaded successfully.');
}
// Remove the temporary directory and its contents once done
fileSystem.rmSync(tempDir, { recursive: true, force: true });
});
});
} else {
response.status(400).send('Invalid url provided.');
}
}