diff --git a/bot.ts b/bot.ts index 72f9739..1b7c7fd 100644 --- a/bot.ts +++ b/bot.ts @@ -17,14 +17,15 @@ import { import { ChannelType, Collection, GuildBasedChannel, Snowflake, VoiceChannel, VoiceState } from 'discord.js'; import * as dotenv from 'dotenv'; import * as schedule from 'node-schedule'; -import { loadAvoidList } from './helpers/load-avoid-list'; -import { LoggerColors } from './helpers/logger-colors'; -import { getRandomSoundFilePath } from './helpers/get-random-sound-file-path'; +import { loadAvoidList } from './helpers/loadAvoidList'; +import { LoggerColors } from './helpers/loggerColors'; +import { getRandomSoundFilePath } from './helpers/getRandomSoundFilePath'; import { logger } from './helpers/logger'; -import { SetupDiscordCLient } from './helpers/setup-discord-client'; +import { SetupDiscordCLient } from './helpers/setupDiscordClient'; import { convertHoursToMinutes, dateToString } from './helpers/converters'; import { AvoidList } from './models/avoid-list'; import { runServer } from './client/router'; +import { startDatabase } from './client/database'; dotenv.config(); @@ -40,6 +41,7 @@ discordClient.on('ready', async () => { joinRandomChannel(voiceChannelRetries); runServer(); + startDatabase(); }); /** diff --git a/client/controllers/index.ts b/client/controllers/index.ts index 7b1109a..6d38a26 100644 --- a/client/controllers/index.ts +++ b/client/controllers/index.ts @@ -1,8 +1,8 @@ import express from 'express'; import path from 'path'; -import { Handlers } from "../handlers/index"; +import { AddUserToAvoidList, DeleteUserFromAvoidList, JoinChannel } from "../handlers/index"; import { nextPlayBackTime } from '../../bot'; -import { loadAvoidList } from '../../helpers/load-avoid-list'; +import { loadAvoidList } from '../../helpers/loadAvoidList'; const router = express.Router(); @@ -27,15 +27,15 @@ router.get('/avoidlist', (_req, res) => { }); router.post('/avoidlist', (req, res) => { - Handlers.AddUserToAvoidList(res, req); + AddUserToAvoidList(res, req); }); router.delete('/avoidlist/:user', (req, res) => { - Handlers.DeleteUserFromAvoidList(res, req); + DeleteUserFromAvoidList(res, req); }); router.get('/join', (_req, res) => { - Handlers.JoinChannel(res); + JoinChannel(res); }); router.use(express.static(path.join(__dirname, "../web"))); diff --git a/client/controllers/sounds.ts b/client/controllers/sounds.ts index 388d62c..5b0120c 100644 --- a/client/controllers/sounds.ts +++ b/client/controllers/sounds.ts @@ -1,11 +1,11 @@ import express from 'express'; import path from 'path'; -import { Handlers } from "../handlers/index"; +import { DeleteSoundFile, GetSoundFiles } from "../handlers/index"; const router = express.Router(); router.delete('/sounds/:filename', (_req, res) => { - Handlers.DeleteSoundFile(res, _req); + DeleteSoundFile(res, _req); }); /** @@ -15,8 +15,6 @@ router.delete('/sounds/:filename', (_req, res) => { */ router.use('/sounds', express.static(path.join(__dirname, '../../sounds'))); -router.get('/sounds', (_req, res: express.Response) => { - return Handlers.GetSoundFiles(res); -}); +router.get('/sounds', (_req, res: express.Response) => GetSoundFiles(res)); export default router; \ No newline at end of file diff --git a/client/controllers/upload.ts b/client/controllers/upload.ts index 4da89ad..f36b400 100644 --- a/client/controllers/upload.ts +++ b/client/controllers/upload.ts @@ -1,44 +1,19 @@ import express from 'express'; -import multer, { diskStorage } from 'multer'; -import path from 'path'; -import { Handlers } from "../handlers/index"; -import { generateFileName } from '../../helpers/generate-file-name'; +import { UploadYouTubeFile } from "../handlers/index"; +import { UploadMp3File } from '../handlers/uploadMp3FIle'; const router = express.Router(); -const storage = diskStorage({ - destination: 'sounds/', - filename: function (_req, file, cb) { - cb(null, generateFileName(file.originalname)); - } -}); - -const upload = multer({ - storage: storage, - limits: { fileSize: 1 * 1024 * 1024 }, - fileFilter: function (_req, file, cb) { - if (path.extname(file.originalname) !== '.mp3') { - return cb(new Error('Only .mp3 files are allowed')); - } - - cb(null, true); - } -}); - /** * Uploads a file to the sounds folder. * @Body myFile - The file to upload. */ -router.post('/upload', upload.single('myFile'), async (req, res) => { - res.send('File uploaded successfully.'); -}); - -router.post('/youtube', async (req, res) => { - await Handlers.UploadYouTubeFile(res, req); +router.post('/upload', async (req, res) => { + await UploadMp3File(res, req); }); router.post('/upload-youtube', async (req, res) => { - await Handlers.UploadYouTubeFile(res, req); + await UploadYouTubeFile(res, req); }); export default router; \ No newline at end of file diff --git a/client/data/addSound.ts b/client/data/addSound.ts new file mode 100644 index 0000000..09f2fe1 --- /dev/null +++ b/client/data/addSound.ts @@ -0,0 +1,11 @@ +import { database } from "../database"; + +export function AddSoundToDatabase( + fileName: string, + guild: string = "unknown", + user: string = "unknown" +) { + database.serialize(async () => { + await database.run('INSERT INTO sounds (name, guild, user, date) VALUES (?,?,?,?)', [fileName, guild, user, new Date().toISOString()]); + }); +} \ No newline at end of file diff --git a/client/data/deleteSound.ts b/client/data/deleteSound.ts new file mode 100644 index 0000000..27f828b --- /dev/null +++ b/client/data/deleteSound.ts @@ -0,0 +1,9 @@ +import { database } from "../database"; + +export function DeleteSoundFromDatabase( + fileName: string +) { + database.serialize(async () => { + await database.run('DELETE FROM sounds WHERE name = ?', [fileName]); + }); +} \ No newline at end of file diff --git a/client/database.ts b/client/database.ts new file mode 100644 index 0000000..891c37f --- /dev/null +++ b/client/database.ts @@ -0,0 +1,9 @@ +import { Database } from 'sqlite3'; + +export const database = new Database('randomMemerDatabase.sqlite'); + +export function startDatabase() { + database.serialize(() => { + database.run('CREATE TABLE IF NOT EXISTS sounds (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, guild TEXT, user TEXT, date TEXT)'); + }); +} diff --git a/client/handlers/addUserToAvoidList.ts b/client/handlers/addUserToAvoidList.ts index de35e79..c9fd1e8 100644 --- a/client/handlers/addUserToAvoidList.ts +++ b/client/handlers/addUserToAvoidList.ts @@ -1,6 +1,6 @@ import * as fileSystem from 'fs'; import express from 'express'; -import { loadAvoidList } from '../../helpers/load-avoid-list'; +import { loadAvoidList } from '../../helpers/loadAvoidList'; /** * Adds a user to the avoid list. diff --git a/client/handlers/deleteSoundFile.ts b/client/handlers/deleteSoundFile.ts index cfec11b..3d83093 100644 --- a/client/handlers/deleteSoundFile.ts +++ b/client/handlers/deleteSoundFile.ts @@ -1,7 +1,8 @@ import express from 'express'; import path from 'path'; import * as fileSystem from 'fs'; -import { LoggerColors } from '../../helpers/logger-colors'; +import { LoggerColors } from '../../helpers/loggerColors'; +import { DeleteSoundFromDatabase } from '../data/deleteSound'; /** * Deletes a file from the sounds folder by filename @@ -16,4 +17,6 @@ export function DeleteSoundFile(response: express.Response, request: express.Req } response.send('File deleted successfully.'); }); + + DeleteSoundFromDatabase(request.params.filename); } \ No newline at end of file diff --git a/client/handlers/deleteUserFromAvoidList.ts b/client/handlers/deleteUserFromAvoidList.ts index 99382a0..1632793 100644 --- a/client/handlers/deleteUserFromAvoidList.ts +++ b/client/handlers/deleteUserFromAvoidList.ts @@ -1,6 +1,6 @@ import express from 'express'; import fs from 'fs'; -import { loadAvoidList } from '../../helpers/load-avoid-list'; +import { loadAvoidList } from '../../helpers/loadAvoidList'; /** * Removes a user from the avoid list. diff --git a/client/handlers/getSoundFiles.ts b/client/handlers/getSoundFiles.ts index 357a83c..4343fe1 100644 --- a/client/handlers/getSoundFiles.ts +++ b/client/handlers/getSoundFiles.ts @@ -1,7 +1,7 @@ import express from 'express'; import path from 'path'; import * as fileSystem from 'fs'; -import { LoggerColors } from '../../helpers/logger-colors'; +import { LoggerColors } from '../../helpers/loggerColors'; /** * Returns a list of all sound files in the sounds directory. diff --git a/client/handlers/index.ts b/client/handlers/index.ts index 31a0b54..2585c97 100644 --- a/client/handlers/index.ts +++ b/client/handlers/index.ts @@ -1,15 +1,6 @@ -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; -} \ No newline at end of file +export { AddUserToAvoidList } from './addUserToAvoidList'; +export { DeleteSoundFile } from './deleteSoundFile'; +export { GetSoundFiles } from './getSoundFiles'; +export { UploadYouTubeFile } from './uploadYouTubeFile'; +export { DeleteUserFromAvoidList } from './deleteUserFromAvoidList'; +export { JoinChannel } from './joinChannel'; diff --git a/client/handlers/uploadMp3FIle.ts b/client/handlers/uploadMp3FIle.ts new file mode 100644 index 0000000..ab7de0a --- /dev/null +++ b/client/handlers/uploadMp3FIle.ts @@ -0,0 +1,42 @@ +import express from "express"; +import { UploaderInformation } from "../../models/uploaderInformation"; +import multer, { diskStorage } from "multer"; +import { generateFileName } from "../../helpers/generateFileName"; +import { AddSoundToDatabase } from "../data/addSound"; +import path from "path"; +const destination = "sounds/"; + +var uploaderInformation : UploaderInformation; + +const storage = diskStorage({ + destination: destination, + filename: function (_req, file, cb) { + let fileName = generateFileName(file.originalname) + + cb(null, fileName); + + AddSoundToDatabase(fileName); + } +}); + +const upload = multer({ + storage: storage, + limits: { fileSize: 1 * 1024 * 1024 }, + fileFilter: function (_req, file, cb) { + if (path.extname(file.originalname) !== '.mp3') { + return cb(new Error('Only .mp3 files are allowed')); + } + + cb(null, true); + } +}); + +export async function UploadMp3File(response: express.Response, request: express.Request) { + upload.any()(request, response, function(err) { + if (err) { + return response.status(500).json({ error: err.message }); + } + uploaderInformation = request.body; + response.send('File uploaded successfully.'); + }); +} \ No newline at end of file diff --git a/client/handlers/uploadYouTubeFile.ts b/client/handlers/uploadYouTubeFile.ts index a28070b..115cc20 100644 --- a/client/handlers/uploadYouTubeFile.ts +++ b/client/handlers/uploadYouTubeFile.ts @@ -3,7 +3,7 @@ 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'; +import { generateFileName } from '../../helpers/generateFileName'; /** * Uploads a YouTube video as an mp3 file to the sounds folder. diff --git a/client/server.ts b/client/server.ts index b4fe320..00f442a 100644 --- a/client/server.ts +++ b/client/server.ts @@ -1,7 +1,7 @@ import https from 'https'; import fs from 'fs'; import path from 'path'; -import { LoggerColors } from '../helpers/logger-colors'; +import { LoggerColors } from '../helpers/loggerColors'; import ip from 'ip'; import { Express } from 'express'; diff --git a/helpers/generate-file-name.ts b/helpers/generateFileName.ts similarity index 92% rename from helpers/generate-file-name.ts rename to helpers/generateFileName.ts index 8025848..e4e4873 100644 --- a/helpers/generate-file-name.ts +++ b/helpers/generateFileName.ts @@ -14,5 +14,5 @@ export function generateFileName(name: string): string { .replace(/\s+/g, ' ') .replace('.mp3', ''); - return `${formattedName}-${randomHex}.mp3`; + return `${formattedName}-${randomHex}.mp3` } \ No newline at end of file diff --git a/helpers/get-random-sound-file-path.ts b/helpers/getRandomSoundFilePath.ts similarity index 100% rename from helpers/get-random-sound-file-path.ts rename to helpers/getRandomSoundFilePath.ts diff --git a/helpers/load-avoid-list.ts b/helpers/loadAvoidList.ts similarity index 100% rename from helpers/load-avoid-list.ts rename to helpers/loadAvoidList.ts diff --git a/helpers/logger.ts b/helpers/logger.ts index 2c91d65..016ba33 100644 --- a/helpers/logger.ts +++ b/helpers/logger.ts @@ -1,5 +1,5 @@ import { dateToString } from "./converters"; -import { LoggerColors } from "./logger-colors"; +import { LoggerColors } from "./loggerColors"; /** * Logs the wait time, current time, next join time, and cron schedule in the console. diff --git a/helpers/logger-colors.ts b/helpers/loggerColors.ts similarity index 100% rename from helpers/logger-colors.ts rename to helpers/loggerColors.ts diff --git a/helpers/setup-discord-client.ts b/helpers/setupDiscordClient.ts similarity index 100% rename from helpers/setup-discord-client.ts rename to helpers/setupDiscordClient.ts diff --git a/models/uploaderInformation.ts b/models/uploaderInformation.ts new file mode 100644 index 0000000..1a946a5 --- /dev/null +++ b/models/uploaderInformation.ts @@ -0,0 +1,4 @@ +export interface UploaderInformation { + guildId: string, + user: string +} \ No newline at end of file diff --git a/package.json b/package.json index ab2bf8c..1519bf8 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "multer": "~1.4.5-lts.1", "node-schedule": "~2.1.1", "sodium": "~3.0.2", + "sqlite3": "^5.1.7", "ts-node": "~10.9.1", "typescript": "~5.2.2", "ytdl-core": "^4.11.5"