mirror of
https://github.com/Myxelium/RandomMemerBot.git
synced 2026-07-09 14:35:08 +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 { ChannelType, Collection, GuildBasedChannel, Snowflake, VoiceChannel, VoiceState } from 'discord.js';
|
||||||
import * as dotenv from 'dotenv';
|
import * as dotenv from 'dotenv';
|
||||||
import * as schedule from 'node-schedule';
|
import * as schedule from 'node-schedule';
|
||||||
import { loadAvoidList } from './helpers/load-avoid-list';
|
import { loadAvoidList } from './helpers/loadAvoidList';
|
||||||
import { LoggerColors } from './helpers/logger-colors';
|
import { LoggerColors } from './helpers/loggerColors';
|
||||||
import { getRandomSoundFilePath } from './helpers/get-random-sound-file-path';
|
import { getRandomSoundFilePath } from './helpers/getRandomSoundFilePath';
|
||||||
import { logger } from './helpers/logger';
|
import { logger } from './helpers/logger';
|
||||||
import { SetupDiscordCLient } from './helpers/setup-discord-client';
|
import { SetupDiscordCLient } from './helpers/setupDiscordClient';
|
||||||
import { convertHoursToMinutes, dateToString } from './helpers/converters';
|
import { convertHoursToMinutes, dateToString } from './helpers/converters';
|
||||||
import { AvoidList } from './models/avoid-list';
|
import { AvoidList } from './models/avoid-list';
|
||||||
import { runServer } from './client/router';
|
import { runServer } from './client/router';
|
||||||
|
import { startDatabase } from './client/database';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ discordClient.on('ready', async () => {
|
|||||||
|
|
||||||
joinRandomChannel(voiceChannelRetries);
|
joinRandomChannel(voiceChannelRetries);
|
||||||
runServer();
|
runServer();
|
||||||
|
startDatabase();
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { Handlers } from "../handlers/index";
|
import { AddUserToAvoidList, DeleteUserFromAvoidList, JoinChannel } from "../handlers/index";
|
||||||
import { nextPlayBackTime } from '../../bot';
|
import { nextPlayBackTime } from '../../bot';
|
||||||
import { loadAvoidList } from '../../helpers/load-avoid-list';
|
import { loadAvoidList } from '../../helpers/loadAvoidList';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@@ -27,15 +27,15 @@ router.get('/avoidlist', (_req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post('/avoidlist', (req, res) => {
|
router.post('/avoidlist', (req, res) => {
|
||||||
Handlers.AddUserToAvoidList(res, req);
|
AddUserToAvoidList(res, req);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.delete('/avoidlist/:user', (req, res) => {
|
router.delete('/avoidlist/:user', (req, res) => {
|
||||||
Handlers.DeleteUserFromAvoidList(res, req);
|
DeleteUserFromAvoidList(res, req);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/join', (_req, res) => {
|
router.get('/join', (_req, res) => {
|
||||||
Handlers.JoinChannel(res);
|
JoinChannel(res);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.use(express.static(path.join(__dirname, "../web")));
|
router.use(express.static(path.join(__dirname, "../web")));
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { Handlers } from "../handlers/index";
|
import { DeleteSoundFile, GetSoundFiles } from "../handlers/index";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.delete('/sounds/:filename', (_req, res) => {
|
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.use('/sounds', express.static(path.join(__dirname, '../../sounds')));
|
||||||
|
|
||||||
router.get('/sounds', (_req, res: express.Response) => {
|
router.get('/sounds', (_req, res: express.Response) => GetSoundFiles(res));
|
||||||
return Handlers.GetSoundFiles(res);
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
@@ -1,44 +1,19 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import multer, { diskStorage } from 'multer';
|
import { UploadYouTubeFile } from "../handlers/index";
|
||||||
import path from 'path';
|
import { UploadMp3File } from '../handlers/uploadMp3FIle';
|
||||||
import { Handlers } from "../handlers/index";
|
|
||||||
import { generateFileName } from '../../helpers/generate-file-name';
|
|
||||||
|
|
||||||
const router = express.Router();
|
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.
|
* Uploads a file to the sounds folder.
|
||||||
* @Body myFile - The file to upload.
|
* @Body myFile - The file to upload.
|
||||||
*/
|
*/
|
||||||
router.post('/upload', upload.single('myFile'), async (req, res) => {
|
router.post('/upload', async (req, res) => {
|
||||||
res.send('File uploaded successfully.');
|
await UploadMp3File(res, req);
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/youtube', async (req, res) => {
|
|
||||||
await Handlers.UploadYouTubeFile(res, req);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/upload-youtube', async (req, res) => {
|
router.post('/upload-youtube', async (req, res) => {
|
||||||
await Handlers.UploadYouTubeFile(res, req);
|
await UploadYouTubeFile(res, req);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
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 * as fileSystem from 'fs';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { loadAvoidList } from '../../helpers/load-avoid-list';
|
import { loadAvoidList } from '../../helpers/loadAvoidList';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a user to the avoid list.
|
* Adds a user to the avoid list.
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as fileSystem from 'fs';
|
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
|
* 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.');
|
response.send('File deleted successfully.');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
DeleteSoundFromDatabase(request.params.filename);
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { loadAvoidList } from '../../helpers/load-avoid-list';
|
import { loadAvoidList } from '../../helpers/loadAvoidList';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a user from the avoid list.
|
* Removes a user from the avoid list.
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as fileSystem from 'fs';
|
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.
|
* Returns a list of all sound files in the sounds directory.
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
import { AddUserToAvoidList } from './addUserToAvoidList';
|
export { AddUserToAvoidList } from './addUserToAvoidList';
|
||||||
import { DeleteSoundFile } from './deleteSoundFile';
|
export { DeleteSoundFile } from './deleteSoundFile';
|
||||||
import { GetSoundFiles } from './getSoundFiles';
|
export { GetSoundFiles } from './getSoundFiles';
|
||||||
import { UploadYouTubeFile } from './uploadYouTubeFile';
|
export { UploadYouTubeFile } from './uploadYouTubeFile';
|
||||||
import { DeleteUserFromAvoidList } from './deleteUserFromAvoidList';
|
export { DeleteUserFromAvoidList } from './deleteUserFromAvoidList';
|
||||||
import { JoinChannel } from './joinChannel';
|
export { 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;
|
|
||||||
}
|
|
||||||
|
|||||||
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 ytdl from 'ytdl-core';
|
||||||
import * as fileSystem from 'fs';
|
import * as fileSystem from 'fs';
|
||||||
import ffmpeg from 'fluent-ffmpeg';
|
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.
|
* Uploads a YouTube video as an mp3 file to the sounds folder.
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import https from 'https';
|
import https from 'https';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { LoggerColors } from '../helpers/logger-colors';
|
import { LoggerColors } from '../helpers/loggerColors';
|
||||||
import ip from 'ip';
|
import ip from 'ip';
|
||||||
import { Express } from 'express';
|
import { Express } from 'express';
|
||||||
|
|
||||||
|
|||||||
@@ -14,5 +14,5 @@ export function generateFileName(name: string): string {
|
|||||||
.replace(/\s+/g, ' ')
|
.replace(/\s+/g, ' ')
|
||||||
.replace('.mp3', '');
|
.replace('.mp3', '');
|
||||||
|
|
||||||
return `${formattedName}-${randomHex}.mp3`;
|
return `${formattedName}-${randomHex}.mp3`
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { dateToString } from "./converters";
|
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.
|
* 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",
|
"multer": "~1.4.5-lts.1",
|
||||||
"node-schedule": "~2.1.1",
|
"node-schedule": "~2.1.1",
|
||||||
"sodium": "~3.0.2",
|
"sodium": "~3.0.2",
|
||||||
|
"sqlite3": "^5.1.7",
|
||||||
"ts-node": "~10.9.1",
|
"ts-node": "~10.9.1",
|
||||||
"typescript": "~5.2.2",
|
"typescript": "~5.2.2",
|
||||||
"ytdl-core": "^4.11.5"
|
"ytdl-core": "^4.11.5"
|
||||||
|
|||||||
Reference in New Issue
Block a user