Sunday, 19 April 2020

Building URL in function help

Hi All,Currently the variable url has a hard coded value at the top of the project: oG3oxsZwJ4cHO60V1dJvpqHPgW_XuLFFhDOMTKcWvkjcQpA. I need to replace that token in the url with the "id" coming back from the summonerInfo function. I think what I need to do is pass the url from the summonerInfo function into the rankedInfo function. I have been stuck on this for a few hours now and I am unsure how to pass that data correctly. Thanks!const { Client } = require('discord.js') const fetch = require('node-fetch') const { token, apiKey, prefix } = require('./helper/config') const client = new Client(); // This will be commented out const url = https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/oG3oxsZwJ4cHO60V1dJvpqHPgW_XuLFFhDOMTKcWvkjcQpA?api_key=${apiKey} client.once('ready', () => { console.log('Ready!') }) client.on('message', message => { const newContent = message.content.replace('!', '') if (message.content.startsWith('!')) { // The newUrl is being built off of user input const newUrl = https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/${newContent}?api_key=${apiKey} // summonerInfo function starts const summonerInfo = async () => { const response = await fetch(newUrl) const { id, summonerLevel } = await response.json() const data = { id: id, summonerLevel: summonerLevel } /* url is being built correctly and I can console.log it but I need to use this variable in the rankedInfo function */ const url = https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/${id}?api_key=${apiKey} console.log(url) return data } // rankedInfo function starts const rankedInfo = async () => { const response = await fetch(url) const result = await response.json() const data = await result.find(f => f.queueType === 'RANKED_SOLO_5x5') return data } // sumRankinfo function starts const sumRankInfo = async () => { const data = await rankedInfo(url) const newData = await summonerInfo(newUrl) const { id ,summonerName, summonerLevel, rank, tier, wins, losses, leaguePoints} = Object.assign({}, data, newData) const totalGames = wins + losses const winPercentage = wins * 100 / totalGames const total = winPercentage.toFixed() + '%' const finalData = [ 'ID: ' + id, 'SummonerName: ' + summonerName, 'SummonerLevel: ' + summonerLevel, `Rank: ${tier}${rank}`, 'Wins: ' + wins, 'Loss: ' + losses, 'WinRate: ' + total, 'LP: ' + leaguePoints ] console.log(finalData) message.channel.send(finalData) } return sumRankInfo() } }) client.login(token) ​

Submitted April 19, 2020 at 07:07PM by xPhanish

No comments:

Post a Comment