new-dblapi
Advanced tools
Comparing version 1.0.3 to 1.1.0
102
dblapi.js
const http = require('http') | ||
const https = require('https') | ||
const fastify = require('fastify')() | ||
const EventEmitter = require('events'); | ||
const EventEmitter = require('events') | ||
@@ -23,7 +23,9 @@ class DblAPI extends EventEmitter{ | ||
if(!this.options.delay) this.options.delay = 1800000 | ||
if(this.options.delay < 900000) throw new Error("Delay can not be less than 15 minutes (900000 ms).") | ||
this.postStats() | ||
setInterval(() => { | ||
this.postStats() | ||
}, this.options.delay) | ||
if(this.options.delay != 0){ | ||
if(this.options.delay < 900000) throw new Error("Delay can not be less than 15 minutes (900000 ms).") | ||
this.postStats() | ||
setInterval(() => { | ||
this.postStats() | ||
}, this.options.delay) | ||
} | ||
}else if(client){ | ||
@@ -36,3 +38,4 @@ throw new Error("Client provided is not a discord.js client.") | ||
auth, | ||
path | ||
path, | ||
voteEmbed | ||
} = this.options | ||
@@ -53,7 +56,22 @@ | ||
} | ||
if(voteEmbed){ | ||
if(!this.client) throw new Error("You must provide a client to use the voteEmbed feature") | ||
if(!voteEmbed.url) throw new Error("Webhook url must be provided when using voteEmbed") | ||
this.setWebhook(voteEmbed) | ||
this.voteHookOptions = voteEmbed | ||
} | ||
} | ||
request(opts){ | ||
async setWebhook(voteEmbed){ | ||
let hook = voteEmbed.url.split("/") | ||
let id = hook[hook.length - 2] | ||
let token = hook[hook.length - 1] | ||
let webhook = await this.client.fetchWebhook(id, token) | ||
this.voteHook = webhook | ||
console.info(`Vote embed working on webhook id: ${id}`) | ||
} | ||
async request(opts){ | ||
return new Promise((resolve, reject) => { | ||
let data = ''; | ||
let data = '' | ||
let request = https.request(opts, (res) => { | ||
@@ -145,12 +163,3 @@ if(res.statusCode == 401) throw new Error("Unauthorized, invalid DBL token.") | ||
onVote(req, res){ | ||
if(req.headers.authorization !== this.auth){ | ||
res.status(401).send("Unauthorized") | ||
}else{ | ||
let vote = req.body | ||
this.emit('vote', vote) | ||
} | ||
} | ||
getWidget(id, opts){ | ||
async getWidget(id, opts){ | ||
if(!id) id = this.client.user.id | ||
@@ -171,2 +180,55 @@ if(!id) throw new Error("getWidget requires a client OR a supplied id.") | ||
sendEmbed(content){ | ||
try{ | ||
this.voteHook.send({embeds: [content]}) | ||
}catch(e){console.error(e)} | ||
} | ||
async onVote(req, res){ | ||
if(req.headers.authorization !== this.auth){ | ||
res.status(401).send("Unauthorized") | ||
}else{ | ||
let vote = req.body | ||
this.emit('vote', vote) | ||
if(this.voteHook){ | ||
let voter = await this.getUser(vote.user) | ||
let fields = [] | ||
if(this.voteHookOptions.fields){ | ||
if(this.voteHookOptions.fields.length < 1) throw new Error("voteHook.fields must be an array with atleast 1 entry") | ||
for(let i in this.voteHookOptions.fields){ | ||
let cur = this.voteHookOptions.fields[i] | ||
let name = cur.name | ||
let value = cur.value | ||
if(!name || !value) throw new Error("One of the provided voteHook.fields does not have a name or value") | ||
name = name.toString() | ||
value = value.toString() | ||
name = name.replace(/{user}/g, voter.username) | ||
name = name.replace(/{id}/g, vote.user) | ||
value = value.replace(/{user}/g, voter.username) | ||
value = value.replace(/{id}/g, vote.user) | ||
fields.push({"name": name, "value": value}) | ||
} | ||
} | ||
if(voter.avatar){ | ||
var icon = `https://cdn.discordapp.com/avatars/${vote.user}/${voter.avatar}.png` | ||
}else{ | ||
var icon = `https://discordapp.com/assets/dd4dbc0016779df1378e7812eabaa04d.png` | ||
} | ||
let embed = { | ||
"author": { | ||
"name": voter.username || "Test", | ||
"icon_url": icon || "https://discordapp.com/assets/dd4dbc0016779df1378e7812eabaa04d.png" | ||
}, | ||
"title": this.voteHookOptions.title || "New Vote!", | ||
"color": parseInt(this.voteHookOptions.color, 16) || Math.floor(Math.random()*16777215), | ||
"thumbnail": { | ||
"url": this.voteHookOptions.thumbnail || "" | ||
}, | ||
"fields": fields | ||
} | ||
this.sendEmbed(embed) | ||
} | ||
} | ||
} | ||
postStats(){ | ||
@@ -240,2 +302,2 @@ if(!this.client) throw new Error("No client provided in constructor.") | ||
module.exports = DblAPI | ||
module.exports = DblAPI |
{ | ||
"name": "new-dblapi", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A new dbl api.", | ||
@@ -5,0 +5,0 @@ "main": "dblapi.js", |
# Simple, Easy, discordbots.org API wrapper. | ||
# ONLY WORKS WITH DISCORD.JS CLIENTS, IT IS NOT PLANNED TO BE MADE TO WORK WITH ERIS, sorry | ||
new-dblapi was made to be very easy to use. Is it better than the original? No idea, I made this out of pure boredom, **most** of it is tested to work. | ||
``` | ||
npm i new-dblapi | ||
``` | ||
```javascript | ||
@@ -32,3 +36,3 @@ const DBL = require('new-dblapi') | ||
# But what is options? | ||
Options is an object consisting of 4 things: delay (posting stats), port, auth and path. All 3, port, auth and path are for the webhook. **If you provide a port, you must provide an auth, but path is optional**. So what does it look like? | ||
Options is an object consisting of 4 main things: delay (posting stats), port, auth and path. All 3, port, auth and path are for the webhook. **If you provide a port, you must provide an auth, but path is optional**. So what does it look like? | ||
```javascript | ||
@@ -52,6 +56,19 @@ const DBL = require('new-dblapi') | ||
*** | ||
# Vote embed option | ||
So you want to send vote embeds? This makes that very easy. **Inside** of your options object, add a voteEmbed object, so it'd look like this `const dbl = new DBL('your-dbl-token', {delay: 4444444, port: 5555, auth: "6666", path: "77777", voteEmbed:{}}, client)` | ||
Inside of the vote embed, theres only 1 required parameter: a webhook URL called `url`. Apart from this, there are fields (an array of objects), title, color (a hex string, no '#' so green would be '00ff00') and thumbnail (a url). | ||
It'd look something like this: | ||
```javascript | ||
const DBL = require('new-dblapi') | ||
const dbl = new DBL(token, {port: 5000, auth: "StinkyAuthorization", path:"notmyvotes", voteEmbed:{url:"webhook url",fields:[{name:"name", value:"value"}],color:"00ff00"}}, client) | ||
``` | ||
**This does require a client. Fields must have a name and value.** You can use {user} or {id} in fields to replace it with the username or their id. | ||
# Other cool stuff | ||
What else can I do with this package? Theres some cool stuff you can do with this package, well bascially everything the original one can do. | ||
How do I post stats? This one's easy, just provide a client (**discord.js client**). If you dont provide a delay in `options`, it will automatically be 30 minutes, the minimum is 15 minutes. | ||
How do I post stats? This one's easy, just provide a client (**discord.js client**). If you dont provide a delay in `options`, it will automatically be 30 minutes, the minimum is 15 minutes (0 to disable). | ||
@@ -76,1 +93,4 @@ But, what are the cool methods? There are 5 whole methods, wow! So what do we got to work with? `getUser(id)`, `getBot([id[, votes]])` (votes is false by default, add true to get last 1000 votes), `checkVote(id)`, `getStats([id])` | ||
Do I use new-dblapi? Yes, I made it, so I'll use it. | ||
Is it better than the official library? Never tested it against it, I did skip over some things, however. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14796
279
94