Skip to content

Commit

Permalink
add prettier-eslint, code style
Browse files Browse the repository at this point in the history
  • Loading branch information
reboxer committed Dec 21, 2021
1 parent b0421f4 commit f891bdd
Show file tree
Hide file tree
Showing 11 changed files with 1,035 additions and 106 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true
4 changes: 1 addition & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"ecmaVersion": 2020
},
"extends": "eslint:recommended",
"ignorePatterns": ["lib/eris"],
"rules": {
"semi": "error",
"no-var": "error",
Expand All @@ -17,16 +18,13 @@
"brace-style": ["error", "stroustrup"],
"comma-dangle": ["error", "always-multiline"],
"object-curly-spacing": ["error", "always"],

"quotes": ["error", "double", {
"allowTemplateLiterals": true
}],

"indent": ["error", "tab", {
"SwitchCase": 1,
"flatTernaryExpressions": true
}],

"object-curly-newline": ["error", {
"ExportDeclaration": "never",
"ImportDeclaration": "always"
Expand Down
1 change: 1 addition & 0 deletions .eslintignore → .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
lib/eris
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"useTabs": true,
"trailingComma": "all",
"tabWidth": 4,
"semi": true,
"singleQuote": false
}
3 changes: 0 additions & 3 deletions lib/eris/errors/DiscordHTTPError.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* eslint-disable no-prototype-builtins */

"use strict";

class DiscordHTTPError extends Error {
Expand Down
3 changes: 0 additions & 3 deletions lib/eris/errors/DiscordRESTError.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* eslint-disable no-prototype-builtins */

"use strict";

class DiscordRESTError extends Error {
Expand Down
3 changes: 1 addition & 2 deletions lib/eris/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

"use strict";

const DiscordHTTPError = require("../errors/DiscordHTTPError");
Expand Down Expand Up @@ -145,7 +144,7 @@ class RequestHandler extends EventEmitter {
this.latencyRef.timeOffset = ~~(this.latencyRef.timeOffset - this.latencyRef.timeOffsets.shift() / 10 + timeOffset / 10);
this.latencyRef.timeOffsets.push(timeOffset);
}

resp.once("aborted", () => {
cb();
reqError = reqError || new Error(`Request aborted by server on ${method} ${url}`);
Expand Down
1 change: 0 additions & 1 deletion lib/eris/util/SequentialBucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

"use strict";

/**
Expand Down
103 changes: 63 additions & 40 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const RequestHandler = require("./eris/rest/RequestHandler");
/**
* Make requests to discord's OAuth2 API
* @extends requestHandler
*/
*/
class OAuth extends RequestHandler {
/**
*
*
* @arg {Object} opts
* @arg {String?} opts.version The version of the Discord API to use. Defaults to v7.
* @arg {Number} [opts.requestTimeout=15000] A number of milliseconds before requests are considered timed out
Expand All @@ -18,13 +18,13 @@ class OAuth extends RequestHandler {
* @arg {String?} opts.clientSecret Your application's client secret
* @arg {String?} opts.redirectUri Your URL redirect uri
* @arg {String?} opts.credentials Base64 encoding of the UTF-8 encoded credentials string of your application
*/
*/
constructor(opts = {}) {
super({
version: opts.version || "v7",
requestTimeout: opts.requestTimeout || 15000,
latencyThreshold: opts.latencyThreshold || 30000,
ratelimiterOffset: opts.ratelimiterOffset || 0,
ratelimiterOffset: opts.ratelimiterOffset || 0,
});

this.client_id = opts.clientId;
Expand All @@ -39,7 +39,9 @@ class OAuth extends RequestHandler {

for (const [key, value] of Object.entries(obj)) {
if (!value) continue;
string += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
string += `&${encodeURIComponent(key)}=${encodeURIComponent(
value,
)}`;
}

return string.substring(1);
Expand All @@ -58,7 +60,7 @@ class OAuth extends RequestHandler {
* @arg {String?} opts.redirectUri Your URL redirect uri
* @arg {String} opts.scope The scopes requested in your authorization url, space-delimited
* @returns {Promise<Object>}
*/
*/
tokenRequest(opts = {}) {
const obj = {
client_id: opts.clientId || this.client_id,
Expand All @@ -67,7 +69,8 @@ class OAuth extends RequestHandler {
code: undefined,
refresh_token: undefined,
redirect_uri: opts.redirectUri || this.redirect_uri,
scope: opts.scope instanceof Array ? opts.scope.join(" ") : opts.scope,
scope:
opts.scope instanceof Array ? opts.scope.join(" ") : opts.scope,
};

if (opts.grantType === "authorization_code") {
Expand All @@ -78,7 +81,10 @@ class OAuth extends RequestHandler {
obj.refresh_token = opts.refreshToken;
obj.grant_type = opts.grantType;
}
else throw new Error("Invalid grant_type provided, it must be either authorization_code or refresh_token");
else
throw new Error(
"Invalid grant_type provided, it must be either authorization_code or refresh_token",
);

const encoded_string = this._encode(obj);

Expand All @@ -92,24 +98,30 @@ class OAuth extends RequestHandler {
* @arg {String} access_token The user access token
* @arg {String} credentials Base64 encoding of the UTF-8 encoded credentials string of your application
* @returns {Promise<String>}
*/
*/
revokeToken(access_token, credentials) {
if (!credentials && !this.credentials) throw new Error("Missing credentials for revokeToken method");
return this.request("POST", "/oauth2/token/revoke", `token=${access_token}`, {
auth: {
type: "Basic",
creds: credentials || this.credentials,
if (!credentials && !this.credentials)
throw new Error("Missing credentials for revokeToken method");
return this.request(
"POST",
"/oauth2/token/revoke",
`token=${access_token}`,
{
auth: {
type: "Basic",
creds: credentials || this.credentials,
},
contentType: "application/x-www-form-urlencoded",
},
contentType: "application/x-www-form-urlencoded",
});
);
}

/**
* Request basic user data
* Requires the `identify` scope
* @arg {String} access_token The user access token
* @returns {Promise<Object>}
*/
*/
getUser(access_token) {
return this.request("GET", "/users/@me", undefined, {
auth: {
Expand All @@ -125,7 +137,7 @@ class OAuth extends RequestHandler {
* Requires the `guilds` scope
* @arg {String} access_token The user access token
* @returns {Promise<Object[]>}
*/
*/
getUserGuilds(access_token) {
return this.request("GET", "/users/@me/guilds", undefined, {
auth: {
Expand All @@ -141,7 +153,7 @@ class OAuth extends RequestHandler {
* Requires the `connections` scope
* @arg {String} access_token The user access token
* @returns {Promise<Object[]>}
*/
*/
getUserConnections(access_token) {
return this.request("GET", "/users/@me/connections", undefined, {
auth: {
Expand All @@ -167,19 +179,24 @@ class OAuth extends RequestHandler {
* @returns {Promise<Object | String>}
*/
addMember(opts) {
return this.request("PUT", `/guilds/${opts.guildId}/members/${opts.userId}`, {
deaf: opts.deaf,
mute: opts.mute,
nick: opts.nickname,
roles: opts.roles,
access_token: opts.accessToken,
}, {
auth: {
type: "Bot",
creds: opts.botToken,
return this.request(
"PUT",
`/guilds/${opts.guildId}/members/${opts.userId}`,
{
deaf: opts.deaf,
mute: opts.mute,
nick: opts.nickname,
roles: opts.roles,
access_token: opts.accessToken,
},
contentType: "application/json",
});
{
auth: {
type: "Bot",
creds: opts.botToken,
},
contentType: "application/json",
},
);
}

/**
Expand All @@ -191,17 +208,22 @@ class OAuth extends RequestHandler {
* @returns {Promise<Object>}
*/
getGuildMember(accessToken, guildId) {
return this.request("GET", `/users/@me/guilds/${guildId}/member`, undefined, {
auth: {
type: "Bearer",
creds: accessToken,
return this.request(
"GET",
`/users/@me/guilds/${guildId}/member`,
undefined,
{
auth: {
type: "Bearer",
creds: accessToken,
},
contentType: "application/json",
},
contentType: "application/json",
});
);
}

/**
*
*
* @arg {Object} opts
* @arg {String} opts.clientId Your application's client id
* @arg {String?} opts.prompt Controls how existing authorizations are handled, either consent or none (for passthrough scopes authorization is always required).
Expand All @@ -213,14 +235,15 @@ class OAuth extends RequestHandler {
* @arg {String?} opts.guildId The guild id to pre-fill the bot invite (only with bot scope)
* @arg {Boolean?} opts.disableGuildSelect Disallows the user from changing the guild for the bot invite, either true or false (only with bot scope)
* @returns {String}
*/
*/
generateAuthUrl(opts = {}) {
const obj = {
client_id: opts.clientId || this.client_id,
prompt: opts.prompt,
redirect_uri: opts.redirectUri || this.redirect_uri,
response_type: opts.responseType || "code",
scope: opts.scope instanceof Array ? opts.scope.join(" ") : opts.scope,
scope:
opts.scope instanceof Array ? opts.scope.join(" ") : opts.scope,
permissions: opts.permissions,
guild_id: opts.guildId,
disable_guild_select: opts.disableGuildSelect,
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Easily interact with discord's oauth2 API",
"main": "index.js",
"scripts": {
"lint": "eslint --ext .js ./"
"lint": "prettier-eslint ./**/*.js --list-different",
"fix": "prettier-eslint ./**/*.js --write"
},
"repository": {
"type": "git",
Expand All @@ -23,6 +24,8 @@
},
"homepage": "https://github.com/reboxer/discord-oauth2#readme",
"devDependencies": {
"eslint": "7.32.0"
"eslint": "7.32.0",
"prettier": "^2.5.1",
"prettier-eslint-cli": "^5.0.1"
}
}
Loading

0 comments on commit f891bdd

Please sign in to comment.