-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorten.js
64 lines (59 loc) · 2.01 KB
/
shorten.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const request = require("request");
const Discord = require('discord.js');
class Shorten {
static url = "https://api.rebrandly.com/v1/links"
/**
* @constructor
* @param {String} url The url to shorten
*/
constructor(url) {
this.url = url
}
/**
*
* @param {String} url The url to shorten
* @returns an object with some properties
* - (destination) - The url
* - (domin) - The domain of the shortened url
*/
createRequestLinks = (url) => {
return {
destination: url.toString(),
domain: { fullName: "rebrand.ly" }
}
}
/**
* Fetched the the shortened url from the api and reply
* the user with an embed
*
* @param {String} apiKey The rebrand api key to request the server
* for a shortened url
* @param {MessageChannel} msg The discord message channel to send
* a reply to the user
*/
createShortenedUrl = (apiKey, msg) => {
request({
uri: "https://api.rebrandly.com/v1/links",
method: "POST",
body: JSON.stringify(this.createRequestLinks(this.url)),
headers: {
"Content-Type": "application/json",
"apikey": apiKey,
}
}, (error, response, body) => {
const image = "https://raw.githubusercontent.com/code-roller/package-manager/main/assets/shurl.jpg"
let link = JSON.parse(body)
const short = `https://${link.shortUrl}`
let Embed = new Discord.MessageEmbed()
.setColor('#7289DA')
.setTitle('Here is your url')
.setURL(short)
.setAuthor('Shurl', image, 'https://raw.githubusercontent.com/code-roller/package-manager/main/assets/shurl.jpg')
.setDescription(`Your shortened url-${short} :slight_smile:`)
.setThumbnail(image)
msg.reply(Embed)
return link.shortUrl
})
}
}
module.exports = Shorten