Skip to content

Commit

Permalink
Add Discord Embed functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Feb 15, 2024
1 parent 150e788 commit fa30692
Show file tree
Hide file tree
Showing 11 changed files with 494 additions and 13 deletions.
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"nameHide": true,
"key": "API_KEY",
"maxFileSize": 104857600,
"allowConfigGen": true
"allowConfigGen": true,
"discordEmbed": false
}
25 changes: 16 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PORT, url, key, allowConfigGen, discordEmbed } from './config.json';
import { errorMessage, otherMessage, warnMessage } from './src/logger';
import { PORT, url, key, allowConfigGen } from './config.json';
import express, { Request, Response } from 'express';
import express, { json, Request, Response } from 'express';
import { loadEndpoints } from './src/functions';
import { existsSync, mkdirSync } from 'fs';
import fileUpload from 'express-fileupload';
Expand All @@ -16,7 +16,9 @@ try {
app.set('views', './src/views');
app.set('view engine', 'ejs');
app.use(fileUpload());
app.use(json());
global.generateKey = allowConfigGen ? crypto.randomUUID() : null;
global.discordEmbedKey = discordEmbed ? crypto.randomUUID() : null;
const result = await loadEndpoints(app);
if (result !== undefined) {
otherMessage(`Loaded ${result} endpoints`);
Expand All @@ -38,13 +40,18 @@ try {
if (key === 'API_KEY') {
warnMessage('The API Key is still the default key! It is recommended to change this in the config.json file.');
}
if (global.generateKey === null) return;
otherMessage(`Config is available to be generated @ ${url}/generate?key=${global.generateKey}`);
setTimeout(() => {
if (global.generateKey === null) return;
global.generateKey = null;
otherMessage(`Config is no longer available to be generated. Please restart to generate a new key.`);
}, 300000);
if (global.discordEmbedKey !== null) {
otherMessage(`Discord Embed is available to be customized @ ${url}/discord?key=${global.discordEmbedKey}`);
}

if (global.generateKey !== null) {
otherMessage(`Config is available to be generated @ ${url}/generate?key=${global.generateKey}`);
setTimeout(() => {
if (global.generateKey === null) return;
global.generateKey = null;
otherMessage(`Config is no longer available to be generated. Please restart to generate a new key.`);
}, 300000);
}
});
})();
} catch (error) {
Expand Down
11 changes: 11 additions & 0 deletions src/data/embed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"author": "Kathund",
"siteName": "Source Code",
"description": "ShareX-API Made by Kathund",
"authorURL": "https://github.com/kathund",
"siteNameURL": "https://github.com/kathund/ShareX-API",
"title": "ShareX-API",
"color": "#52a396",
"timestampState": true,
"randomColorState": false
}
28 changes: 28 additions & 0 deletions src/endpoints/discord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Application, Request, Response } from 'express';
import { discordEmbed } from '../../config.json';
import { errorMessage } from '../logger';
import { readFileSync } from 'fs';

export default (app: Application) => {
app.get('/discord', async (req: Request, res: Response) => {
try {
if (!discordEmbed || global.discordEmbedKey === null) {
return res.status(400).send({ success: false, message: 'Discord Embeds are disabled' });
}
const discordKey = req.query.key;
if (discordKey !== global.discordEmbedKey) {
errorMessage('Invalid Embed Key provided');
return res.status(400).send({ success: false, message: 'Invalid Embed Key provided' });
}

const data = JSON.parse(readFileSync('src/data/embed.json', 'utf-8'));
return res.status(200).render('pages/discord', {
data: data,
embedKey: discordKey,
});
} catch (err) {
errorMessage(err as string);
return res.status(500).send({ success: false, message: 'Internal server error' });
}
});
};
58 changes: 58 additions & 0 deletions src/endpoints/embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Application, Request, Response } from 'express';
import { apiMessage, errorMessage } from '../logger';
import { discordEmbed } from '../../config.json';
import { writeFileSync } from 'fs';

export default (app: Application) => {
app.post('/embed', async (req: Request, res: Response) => {
try {
apiMessage(req.path, 'User is trying to save embed');
if (!discordEmbed || global.discordEmbedKey === null) {
return res.status(400).send({ success: false, message: 'Discord Embeds are disabled' });
}
const discordKey = req.query.key;
if (discordKey !== global.discordEmbedKey) {
errorMessage('Invalid Embed Key provided');
return res.status(400).send({ success: false, message: 'Invalid Embed Key provided' });
}
const data = req.body;
if (typeof data !== 'object') {
errorMessage('User provided invalid data');
return res.status(400).send({ success: false, message: 'Invalid data provided' });
}
if (
!data.author ||
!data.siteName ||
!data.description ||
!data.authorURL ||
!data.siteNameURL ||
!data.title ||
!data.color
) {
errorMessage('User provided invalid data');
return res.status(400).send({ success: false, message: 'Invalid data provided' });
}
if (
typeof data.author !== 'string' ||
typeof data.siteName !== 'string' ||
typeof data.description !== 'string' ||
typeof data.authorURL !== 'string' ||
typeof data.siteNameURL !== 'string' ||
typeof data.title !== 'string' ||
typeof data.color !== 'string' ||
typeof data.timestampState !== 'boolean' ||
typeof data.randomColorState !== 'boolean'
) {
errorMessage('User provided invalid data');
return res.status(400).send({ success: false, message: 'Invalid data provided' });
}

writeFileSync('src/data/embed.json', JSON.stringify(data));
apiMessage(req.path, 'User updated embed data');
return res.status(200).send({ success: true, message: 'Embed data saved' });
} catch (err) {
errorMessage(err as string);
return res.status(500).send({ success: false, message: 'Internal server error' });
}
});
};
2 changes: 1 addition & 1 deletion src/endpoints/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { url, key, allowConfigGen } from '../../config.json';
import { Application, Request, Response } from 'express';
import { apiMessage, errorMessage } from '../logger';
import { url, key, allowConfigGen } from '../../config.json';

export default (app: Application) => {
app.get('/generate', async (req: Request, res: Response) => {
Expand Down
199 changes: 199 additions & 0 deletions src/public/css/discord.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url('fonts/Inter.woff2') format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329,
U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

*,
*::before,
*::after {
box-sizing: border-box;
padding: 0px;
margin: 0px;
}

body {
font-family: 'Inter', sans-serif;
background-color: #dcdcdc;
justify-content: center;
align-items: center;
font-size: 1.5rem;
color: #343434;
height: 100vh;
display: flex;
width: 100vw;
}

main {
filter: drop-shadow(#000000bf 20px 20px 30px);
background-color: #bfb9b9;
border-radius: 16px;
width: 900px;
height: 900px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

label {
font-size: 20px;
}

input {
font-size: 16px;
border-radius: 8px;
background: none;
color: white;
border: none;
width: 100%;
}

input:focus {
outline: none;
}

.options {
width: 836px;
height: 376px;
background-color: #03395a;
}

.inputs {
border: 2px solid #000000;
background-color: #4b4646;
width: 836px;
height: 192px;
display: flex;
}

.inputs > * > * {
border: 2px solid #000000;
width: 418px;
height: 64px;
}

.inputs > * {
width: 418px;
height: 192px;
}

.buttons {
margin-top: 32px;
width: 836px;
height: 152px;
display: flex;
flex-direction: column;
justify-content: space-between;
}

.buttons > * {
width: 836px;
height: 64px;
display: flex;
flex-direction: row;
justify-content: space-between;
}

.randomColor,
.color,
.timestamp {
background-color: #dcdcdc;
display: flex;
align-items: center;
justify-content: space-evenly;
font-size: 24px;
border-radius: 24px;
color: black;
}

.randomColorToggle,
.timestampToggle {
background-color: #fff;
width: 64px;
height: 32px;
border-radius: 32px;
display: flex;
align-items: center;
}

.randomColorSwitch,
.timestampSwitch {
position: absolute;
height: 23px;
width: 23px;
display: block;
border-radius: 30px;
border: 1px solid black;
background-color: #00000080;
transition: all 0.3s ease;
transform: translateX(4px);
}

.randomColorActive,
.timestampActive {
transform: translateX(calc(64px - 27px));
border: 1px solid #52a396;
background-color: #52a39680;
}

.randomColor {
width: 300px;
height: 64px;
}

.color {
width: 176px;
height: 64px;
}

.timestamp {
width: 300px;
height: 64px;
}

.save,
.reset,
.clear {
font-size: 24px;
width: 200px;
height: 64px;
border-radius: 24px;
border: none;
padding: 0px;
cursor: pointer;
}

.save {
background-color: #27d26b;
}

.reset {
background-color: #c94040;
width: 350px;
color: white;
}

.clear {
background-color: #c7abcf;
}

@media (prefers-color-scheme: dark) {
body {
background-color: #464646;
color: #dcdcdc;
}

a {
color: #dcdcdc;
}

main {
filter: drop-shadow(#ffffff1a 20px 20px 30px);
background-color: #363636;
}
}
Loading

0 comments on commit fa30692

Please sign in to comment.