-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiFunctions.js
125 lines (100 loc) · 3.13 KB
/
apiFunctions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const { riotAPIKey } = require('./config.json');
const fetch = require('node-fetch');
const { disableValidators } = require('discord.js');
const tier = {
'IRON': 0,
'BRONZE': 401,
'SILVER': 802,
'GOLD': 1203,
'PLATINUM': 1604,
'DIAMOND': 2005,
'MASTER': 2406,
'GRANDMASTER': 2406,
'CHALLENGER': 2406
}
const rank = {
'I': 300,
'II': 200,
'III': 100,
'IV': 0
}
function generateUnrankedJSON(name) {
return {
queueType: 'RANKED_SOLO_5x5',
tier: 'UNRANKED',
rank: '',
summonerName: name,
leaguePoints: '-',
wins: '-',
losses: '-',
veteran: false,
inactive: false,
freshBlood: false,
hotStreak: false,
totalElo: 0
}
}
async function getPlayerByName(playerName, region) {
const url = 'https://' + region + '.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + playerName;
const response = await fetch(url, {
method: "GET",
headers: {
"X-Riot-Token": riotAPIKey
}
})
if (response.status != 200) return;
const data = await response.json();
return data;
};
async function getPlayerLeagues(playerList, interaction) {
const playerData = [];
let count = 1;
const totalPlayers = playerList.length;
for (const player of playerList) {
interaction.editReply('```\nSearching Player: ' + String(count) + ' of ' + String(totalPlayers) + '```');
const url = 'https://' + player.region_id + '.api.riotgames.com/lol/league/v4/entries/by-summoner/' + player.riot_id;
const response = await fetch(url, {
method: "GET",
headers: {
"X-Riot-Token": riotAPIKey
}
});
const data = await response.json();
if (data[0] == null) continue;
if (data.length == 0 || (data.length == 1 && data[0].queueType === 'RANKED_FLEX_SR')) {
playerData.push(generateUnrankedJSON(player.lol_username));
}
else {
rankedData = (data[0].queueType === 'RANKED_SOLO_5x5') ? data[0] : data[1];
rankedData.totalElo = tier[rankedData.tier] + rank[rankedData.rank] + rankedData.leaguePoints;
playerData.push(rankedData);
}
count += 1;
};
return playerData;
}
async function getRandomChampions(numberOfChampions, listOfTags) {
const url = 'http://ddragon.leagueoflegends.com/cdn/12.22.1/data/en_US/champion.json';
const response = await fetch(url, {
method: "GET"
});
const data = await response.json();
var validChampions = [];
for (const key in data['data']) {
let championTags = data['data'][key]['tags'];
let championName = data['data'][key]['name'] + ', ' + data['data'][key]['title'];
if (listOfTags.length) {
for (const tag of listOfTags) {
if (championTags.includes(tag)) {
validChampions.push(championName);
break;
}
}
}
else
validChampions.push(championName);
}
const randomizedChampions = validChampions.sort(() => 0.5 - Math.random());
return randomizedChampions.slice(0, numberOfChampions);
}
module.exports = { getPlayerByName, getPlayerLeagues, getRandomChampions }