mirror of
https://github.com/Myxelium/RandomMemerBot.git
synced 2026-04-09 08:59:39 +00:00
Add database
This commit is contained in:
10
bot.ts
10
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();
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -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")));
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
11
client/data/addSound.ts
Normal file
11
client/data/addSound.ts
Normal file
@@ -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()]);
|
||||
});
|
||||
}
|
||||
9
client/data/deleteSound.ts
Normal file
9
client/data/deleteSound.ts
Normal file
@@ -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]);
|
||||
});
|
||||
}
|
||||
9
client/database.ts
Normal file
9
client/database.ts
Normal file
@@ -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)');
|
||||
});
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
export { AddUserToAvoidList } from './addUserToAvoidList';
|
||||
export { DeleteSoundFile } from './deleteSoundFile';
|
||||
export { GetSoundFiles } from './getSoundFiles';
|
||||
export { UploadYouTubeFile } from './uploadYouTubeFile';
|
||||
export { DeleteUserFromAvoidList } from './deleteUserFromAvoidList';
|
||||
export { JoinChannel } from './joinChannel';
|
||||
|
||||
42
client/handlers/uploadMp3FIle.ts
Normal file
42
client/handlers/uploadMp3FIle.ts
Normal file
@@ -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.');
|
||||
});
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -14,5 +14,5 @@ export function generateFileName(name: string): string {
|
||||
.replace(/\s+/g, ' ')
|
||||
.replace('.mp3', '');
|
||||
|
||||
return `${formattedName}-${randomHex}.mp3`;
|
||||
return `${formattedName}-${randomHex}.mp3`
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
4
models/uploaderInformation.ts
Normal file
4
models/uploaderInformation.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface UploaderInformation {
|
||||
guildId: string,
|
||||
user: string
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user