Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
added support for Discord webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
tycrek committed May 30, 2021
1 parent 5245cb6 commit 2c814d9
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 10 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- ✔️ Upload images, videos, gifs, files
- ✔️ Fancy embeds on Discord
- ✔️ Seamless inline video embeds on Discord
- ✔️ Upload log via Discord Webhooks
- ✔️ File deletion
- ✔️ Multiple access types
- **[ZWS](https://zws.im)**
Expand Down Expand Up @@ -111,6 +112,25 @@ You can insert certain metadata into your embeds with these placeholders:
| **`&filename`** | The original filename of the uploaded file |
| **`&timestamp`** | The timestamp of when the file was uploaded (example: `Oct 14, 1983, 1:30 PM`) |

### Webhooks

You may use Discord webhooks as an easy way to keep track of your uploads. The first step is to [create a new Webhook](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks). You only need to follow the first section, **Making a Webhook**. Once you are done that, click **Copy Webhook URL**. Next, paste your URL into a text editor. Extract these two values from the URL:

```
https://discord.com/api/webhooks/12345678910/T0kEn0fw3Bh00K
^^^^^^^^^^ ^^^^^^^^^^^^
Webhook ID Webhook Token
```

Once you have these, add the following HTTP headers to your ShareX config:

| Header | Purpose |
| ------ | ------- |
| **`X-Ass-Webhook-Client`** | The **Webhook ID** |
| **`X-Ass-Webhook-Token`** | The **Webhook Token** |

Webhooks will show the filename, mimetype, size, upload timestamp, thumbail, and a link to delete the file. To disable webhooks, simply remove the headers from your config.

## Contributing

No strict contributing rules at this time. I appreciate any Issues or Pull Requests.
39 changes: 33 additions & 6 deletions ass.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ const express = require('express');
const useragent = require('express-useragent');
const multer = require('multer');
const DateTime = require('luxon').DateTime;
const { WebhookClient, MessageEmbed } = require('discord.js');
const OpenGraph = require('./ogp');
const { path, saveData, log, verify, generateToken, generateId } = require('./utils');
const { path, saveData, log, verify, generateToken, generateId, formatBytes, randomHexColour } = require('./utils');
//#endregion

//#region Variables, module setup
const ASS_LOGO = 'https://cdn.discordapp.com/icons/848274994375294986/3437bfa0b32103d91e294d794ad1205d.png?size=1024'; // todo: change this to the logo eventually
const app = express();

// Configure filename and location settings
Expand Down Expand Up @@ -100,13 +102,38 @@ function startup() {
data[resourceId.split('.')[0]] = req.file;
saveData(data);

log(`Uploaded: ${req.file.originalname} (${req.file.mimetype})`);
// Log the upload
let logInfo = `${req.file.originalname} (${req.file.mimetype})`;
log(`Uploaded: ${logInfo}`);

// Build the URLs
let resourceUrl = `${getTrueHttp()}${trueDomain}/${resourceId}`;
let deleteUrl = `${getTrueHttp()}${trueDomain}/delete/${req.file.filename}`;

// Send the response
res.type('json').send({
resource: `${getTrueHttp()}${trueDomain}/${resourceId}`,
delete: `${getTrueHttp()}${trueDomain}/delete/${req.file.filename}`
});
res.type('json').send({ resource: resourceUrl, delete: deleteUrl })

// After we have sent the user the response, also send a Webhook to Discord (if headers are present)
.on('finish', () => {
if (req.headers['x-ass-webhook-client'] && req.headers['x-ass-webhook-token']) {

// Build the webhook client & embed
let whc = new WebhookClient(req.headers['x-ass-webhook-client'], req.headers['x-ass-webhook-token']);
let embed = new MessageEmbed()
.setTitle(logInfo)
.setDescription(`**Size:** \`${formatBytes(req.file.size)}\`\n**[Delete](${deleteUrl})**`)
.setThumbnail(resourceUrl)
.setColor(randomHexColour())
.setTimestamp(req.file.timestamp);

// Send the embed to the webhook, then delete the client after to free resources
whc.send(null, {
username: req.headers['x-ass-webhook-username'] || 'ass',
avatarURL: ASS_LOGO,
embeds: [embed]
}).then((_msg) => whc.destroy());
}
});
});

// View file
Expand Down
Loading

0 comments on commit 2c814d9

Please sign in to comment.