Skip to content

Commit

Permalink
Merge pull request #89 from C0casio45/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
C0casio45 authored Jun 23, 2022
2 parents fe00437 + b04722f commit aa490e8
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 109 deletions.
77 changes: 40 additions & 37 deletions bot_modules/faceit.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,49 +140,52 @@ module.exports = {
callback(true);
}
},
async RemoveBan(userLink, callback) {
// DELETE https://api.faceit.com/hubs/v1/hub/{hubId}/ban/{userId}
// Authorization: Bearer {userToken}
let userId = await this.GetUserToken(userLink);
//Need to wait for response
let modToken = faceit.token;
async RemoveBan(userLink) {
return new Promise(async (resolve, rejects) => {
// DELETE https://api.faceit.com/hubs/v1/hub/{hubId}/ban/{userId}
// Authorization: Bearer {userToken}
let userId = await this.GetUserToken(userLink);
//Need to wait for response
let modToken = faceit.token;

const https = require("https");
const options = {
hostname: "api.faceit.com",
port: 443,
path: `/hubs/v1/hub/${faceit.hubId}/ban/${userId}`,
method: "DELETE",
headers: {
Authorization: `Bearer ${modToken}`,
"Content-Type": "application/json",
},
};
const https = require("https");
const options = {
hostname: "api.faceit.com",
port: 443,
path: `/hubs/v1/hub/${faceit.hubId}/ban/${userId}`,
method: "DELETE",
headers: {
Authorization: `Bearer ${modToken}`,
"Content-Type": "application/json",
},
};

try {
const req = https.request(options, (res) => {
let chunks = [];
try {
const req = https.request(options, (res) => {
let chunks = [];

res.on("data", (chunk) => chunks.push(chunk));
res.on("end", (_d) => {
try {
const json = Buffer.concat(chunks).toString();
if (json) {
const response = JSON.parse(json);
if (response.errors) throw response.errors[0].message;
res.on("data", (chunk) => chunks.push(chunk));
res.on("end", (_d) => {
try {
const json = Buffer.concat(chunks).toString();
if (json) {
const response = JSON.parse(json);
if (response.errors) throw response.errors[0].message;
}
resolve(json);
} catch (exception) {
rejects(exception);
}
callback(false);
} catch (exception) {
callback(true, exception);
}
});
});
});

req.on("error", (error) => callback(true, error));
req.end();
} catch (exception) {
callback(true, exception);
}
req.on("error", (error) => callback(true, error));
req.end();
} catch (exception) {
rejects(exception);
}

});
},
SpecificBan() {
// GET https://api.faceit.com/hubs/v1/hub/{hubId}/ban?userNickname={nickname}&offset=0&limit=1
Expand Down
34 changes: 13 additions & 21 deletions bot_modules/rappelUnban.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const faceit = require("./faceit.js");
const { FaceitRepository } = require("./repository/faceit_repository");
const Message = require("../utils/embeds/MessagesLibrary");
const db = require("../utils/db/dbLibrary.js");

Expand Down Expand Up @@ -34,28 +34,20 @@ module.exports = {
const unbanChannel = client.channels.cache.find(
(channel) => channel.name == "rappel-unban"
);
const isUnbanned = new FaceitRepository().unbanPlayerByNickname(unban.Pseudo).catch((err) => {
if (err)
unbanChannel.send({
embeds: [Message.error(`${err}`)],
});
});

faceit.RemoveBan(
`https://www.faceit.com/fr/players/${unban.Pseudo}`,
(failed, error = null) => {
if (failed)
unbanChannel.send({
embeds: [Message.error(`${error}`)],
});
else
unbanChannel.send({
embeds: [
Message.success(`Joueur ${unban.Pseudo} unban avec succès.`),
],
});
}
);

db.unbanUser(unban.id, unban.idT);
if (isUnbanned) {
db.unbanUser(unban.id, unban.idT);

unbanChannel.send({
embeds: [Message.unbanLog(unban.Pseudo, unban.duree, Fdate)],
});
unbanChannel.send({
embeds: [Message.unbanLog(unban.Pseudo, unban.duree, Fdate)],
});
}
});
} else if (result[0].length == 0) {
return interaction.reply(
Expand Down
103 changes: 103 additions & 0 deletions bot_modules/repository/base_repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const https = require("https");
const { faceit } = require("../../config.json");

class BaseRepository {

/**
*
* @param {string} hostname - The hostname of the server
* @param {number} port - The port of the server
* @param {string} path - The path of the server
* @param {string} method - The method of the request
* @param {object} headers - The headers of the request
*/
constructor({ hostname = "hostname", port = 443, path = "path", method = "method", headers = {
"Content-Type": "application/json",
} } = {}) {
if (this.constructor == BaseRepository) throw new Error("Object of Abstract Class cannot be created")
this.options = {
hostname: hostname,
port: port,
path: path,
method: method,
headers: headers,
};
}

/**
*
* @returns {Promise<string>}
*/
async create(path) {
if (this.constructor == BaseRepository) throw new Error("Abstract Method has no implementation");
this.options.path = path;
return this.worker("CREATE");
}

/**
*
* @param {string} path - The path of the server
* @param {*} data - The data of the request
* @returns {Promise<string>}
*/
async post(path, data) {
if (this.constructor == BaseRepository) throw new Error("Abstract Method has no implementation");
this.options.path = path;
this.options.headers["Content-Length"] = data.length;
return this.worker("POST", data);

}

/**
*
* @param {string} path - The path of the server
* @returns {Promise<string>}
*/
async get(path) {
if (this.constructor == BaseRepository) throw new Error("Abstract Method has no implementation");
this.options.path = path;
return this.worker("GET");
}

/**
*
* @param {string} path - The path of the server
* @param {*} data - The data of the request
* @returns {Promise<string>}
*/
update(path, data) {
if (this.constructor == BaseRepository) throw new Error("Abstract Method has no implementation");
this.options.path = path;
return this.worker("UPDATE", data);
}

/**
*
* @param {string} path - The path of the server
* @returns {Promise<string>}
*/
async delete(path) {
if (this.constructor == BaseRepository) throw new Error("Abstract Method has no implementation");
this.options.path = path;
return this.worker("DELETE");
}

worker(method, data) {
return new Promise((resolve, rejects) => {
this.options.method = method;
const req = https.request(this.options, (res) => {
let chunks = [];

res.on("data", (chunk) => chunks.push(chunk));
res.on("end", (_d) => {
resolve(Buffer.concat(chunks).toString());
});
});
req.on("error", (error) => rejects(error));
if (data != undefined) req.write(data);
req.end();
});
}
}

module.exports = { BaseRepository };
Loading

0 comments on commit aa490e8

Please sign in to comment.