Sunday, 19 April 2020

DiscordAPIError: Cannot send an empty message (Discord Bot Help)

Hi All,I am still in the process of learning NodeJS and in my free time I have been working on a project where I am interacting with the Riot Games API and the Discord API to build a Discord Bot. I am wanting to return data from the Riot Games API based on User data and return that data to the discord API by the user typing in a prefix then their name. My console.log() is printing the correct data but when I am attempting to retrieve that data in Discord I receive the error message that was listed in the title. I believe the error lies with me attempting to return a promise which has not yet finished but I would like someone with a little more experience to help me clean this up. Thanks!const { Client } = require('discord.js') const fetch = require('node-fetch') const { token, apiKey, prefix } = require('./helper/config')const client = new Client();// URL for Ranked Info const url = https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/oG3oxsZwJ4cHO60V1dJvpqHPgW_XuLFFhDOMTKcWvkjcQpA?api_key=${apiKey}// URL for Summoner Info const newUrl = https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/UndeadPilot?api_key=${apiKey}// Ensures discord bot is communicating client.once('ready', () => { console.log('Ready!') })// I believe the issues lies in this code block client.on('message', message => { if (message.content === ${prefix}Undead Pilot) { const result = sumRankInfo() console.log('Result: ', result) message.channel.send(sumRankInfo())     } })// Find Object with solo queue data 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 }// Return Summoner Level from summoner data const summonerInfo = async () => { const response = await fetch(newUrl) const { summonerLevel }= await response.json() const data = { summonerLevel: summonerLevel } return data }// Combine data from both Endpoints into one object and retrieve finalData const sumRankInfo = async () => { const data = await rankedInfo(url) const newData = await summonerInfo(newUrl) const { 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 = [ 'SummonerName: ' + summonerName, 'SummonerLevel: ' + summonerLevel, Rank: ${tier}${rank}, 'Wins: ' + wins, 'Loss: ' + losses, 'WinRate: ' + total, 'LP: ' + leaguePoints     ] console.log(finalData) return finalData }client.login(token)

Submitted April 19, 2020 at 09:16AM by xPhanish

No comments:

Post a Comment