Skip to content

Commit

Permalink
Merge pull request #57 from JustDams/refacto/Errors
Browse files Browse the repository at this point in the history
Refacto/errors
  • Loading branch information
JustDams authored Sep 17, 2021
2 parents f30b7d4 + 328a067 commit db3d3c5
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [4.4.11](https://github.com/JustDams/faceitFinder/compare/v4.4.10...v4.4.11) (2021-09-17)

### [4.4.10](https://github.com/JustDams/faceitFinder/compare/v4.4.9...v4.4.10) (2021-09-17)

### [4.4.9](https://github.com/JustDams/faceitFinder/compare/v2.4.5...v4.4.9) (2021-09-16)

# Changelog
Expand Down
19 changes: 11 additions & 8 deletions commands/last.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const sendCardWithInfos = async (message = null, steamParam) => {
const playerDatas = await Player.getDatas(playerId)
const playerHistory = await Player.getHistory(playerId)


let lastMatchStats
if (playerHistory.items.length > 0) lastMatchStats = await Match.getMatchStats(playerHistory.items[0].match_id)
else return errorCard('Could not get your last match stats')
Expand All @@ -35,18 +34,20 @@ const sendCardWithInfos = async (message = null, steamParam) => {
ctx.drawImage(await Graph.getRankImage(faceitLevel, size), 0, 0)
filesAtt.push(new Discord.MessageAttachment(rankImageCanvas.toBuffer(), `${faceitLevel}.png`))

const eloDiff = playerDatas.games.csgo.faceit_elo - lastMatchElo[1].elo
let eloDiff = playerDatas.games.csgo.faceit_elo - lastMatchElo[1].elo
eloDiff = isNaN(eloDiff) ? '0' : eloDiff > 0 ? `+${eloDiff}` : eloDiff.toString()
const cards = []

let card = new Discord.MessageEmbed()
let mapThumbnail
lastMatchStats.rounds.forEach((r, key) => {
lastMatchStats.rounds.forEach(r => {
const card = new Discord.MessageEmbed()
let playerStats
for (const t of r.teams) {
const stats = t.players.filter(p => p.player_id === playerId)
if (stats.length > 0) playerStats = stats[0].player_stats
}

if (lastMatchStats.rounds.length > 1) card.addField({ name: 'round', value: key })
if (lastMatchStats.rounds.length > 1) card.addFields({ name: 'round', value: `${r.match_round}/${lastMatchStats.rounds.length}` })
mapThumbnail = `./images/maps/${r.round_stats.Map}.jpg`

card.setAuthor(playerDatas.nickname, playerDatas.avatar, `https://www.faceit.com/fr/players/${playerDatas.nickname}`)
Expand All @@ -60,24 +61,26 @@ const sendCardWithInfos = async (message = null, steamParam) => {
{ name: 'Kills', value: playerStats.Kills, inline: true },
{ name: 'Deaths', value: playerStats.Deaths, inline: true },
{ name: 'Assists', value: playerStats.Assists, inline: true },
{ name: 'Elo', value: eloDiff > 0 ? '+' + eloDiff : eloDiff.toString(), inline: true },
{ name: 'Elo', value: eloDiff.toString(), inline: true },
{ name: 'Date', value: new Date(lastMatchElo[0].date).toDateString(), inline: true })
.setThumbnail(`attachment://${faceitLevel}.png`)
.setImage(`attachment://${r.round_stats.Map}.jpg`)
.setColor(color.levels[faceitLevel].color)
.setFooter(`Steam: ${steamDatas.personaname}`)

if (fs.existsSync(mapThumbnail)) filesAtt.push(new Discord.MessageAttachment(mapThumbnail, `${r.round_stats.Map}.jpg`))

cards.push(card)
})

return {
embeds: [card],
embeds: cards,
files: filesAtt
}

} catch (error) {
console.log(error)
return errorCard('No players found')
return errorCard(error)
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const sendCardWithInfos = async (message, steamParam) => {

} catch (error) {
console.log(error)
return errorCard('No players found')
return errorCard(error)
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const sendCardWithInfos = async (message, steamParam) => {

} catch (error) {
console.log(error)
return errorCard('No players found')
return errorCard(error)
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const sendCardWithInfos = async (message, steamParam) => {
}
} catch (error) {
console.log(error)
return errorCard('No players found')
return errorCard(error)
}
}

Expand Down
2 changes: 1 addition & 1 deletion functions/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getCards = async (message, array, fn, mention = 0) => {
if (mention) {
const user = await User.exists(u.id)
if (user) return await fn(message, user.steamId)
else return errorCard('No players found')
else return errorCard('This user hasn\'t linked his profile')
} else return await fn(message, u)
})).then(msgs => msgs.forEach(msg => {
const data = { embeds: [], files: [], components: [] }
Expand Down
4 changes: 2 additions & 2 deletions functions/faceit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const headerFaceit = {
Authorization: `Bearer ${process.env.FACEIT_TOKEN}`
}

const fetchData = async (url) => await fetch(url, {
const fetchData = async (url, error) => await fetch(url, {
method: 'GET',
headers: headerFaceit
})
.then(res => {
if (res.status == 200) return res.json()
else throw 'An error has occured'
else throw error
})
.then(data => data)

Expand Down
2 changes: 1 addition & 1 deletion functions/ladder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Faceit = require('./faceit')

const getDatas = async (playerId, region, country) => await Faceit.fetchData(`https://open.faceit.com/data/v4/rankings/games/csgo/regions/${region}/players/${playerId}?limit=1${country ? `&country=${country}` : ''}`)
const getDatas = async (playerId, region, country) => await Faceit.fetchData(`https://open.faceit.com/data/v4/rankings/games/csgo/regions/${region}/players/${playerId}?limit=1${country ? `&country=${country}` : ''}`, 'Couldn\'t get the ladder')

module.exports = {
getDatas,
Expand Down
4 changes: 2 additions & 2 deletions functions/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const getMatchElo = async (playerId, limit = 20) => await fetch(`https://api.fac
})
.then(res => {
if (res.status == 200) return res.json()
else throw 'An error has occured'
else throw 'Couldn\'t not get last matches'
})
.then(data => data)

const getMatchStats = async (matchid) => await Faceit.fetchData(`https://open.faceit.com/data/v4/matches/${matchid}/stats`)
const getMatchStats = async (matchid) => await Faceit.fetchData(`https://open.faceit.com/data/v4/matches/${matchid}/stats`, 'Couldn\'t matches stats')

module.exports = {
getMatchElo,
Expand Down
8 changes: 4 additions & 4 deletions functions/player.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Faceit = require('./faceit')

const getId = async (steamId) => (await Faceit.fetchData(`https://open.faceit.com/data/v4/players?game=csgo&game_player_id=${steamId}`)).player_id
const getId = async (steamId) => (await Faceit.fetchData(`https://open.faceit.com/data/v4/players?game=csgo&game_player_id=${steamId}`, 'Faceit profile not found')).player_id

const getDatas = async (playerId) => await Faceit.fetchData(`https://open.faceit.com/data/v4/players/${playerId}`)
const getDatas = async (playerId) => await Faceit.fetchData(`https://open.faceit.com/data/v4/players/${playerId}`, 'Couldn\'t get faceit datas')

const getStats = async (playerId) => await Faceit.fetchData(`https://open.faceit.com/data/v4/players/${playerId}/stats/csgo`)
const getStats = async (playerId) => await Faceit.fetchData(`https://open.faceit.com/data/v4/players/${playerId}/stats/csgo`, 'Couldn\'t get faceit csgo stats')

const getHistory = async (playerId, limit = 1) => await Faceit.fetchData(`https://open.faceit.com/data/v4/players/${playerId}/history?game=csgo&offset=0&limit=${limit}`)
const getHistory = async (playerId, limit = 1) => await Faceit.fetchData(`https://open.faceit.com/data/v4/players/${playerId}/history?game=csgo&offset=0&limit=${limit}`, 'Couldn\'t get csgo history')

module.exports = {
getDatas,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "faceitfinder",
"version": "4.4.9",
"version": "4.4.11",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit db3d3c5

Please sign in to comment.