Need help with a discord bot that connects to hypixel api, getting an error
Archived 3 years ago
E
ezocy
``// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const fetch = require('node-fetch');
const key = 'api key';
const base = 'https://api.hypixel.net';
async function getPlayer(username) {
const method = `/player?key=${key}&name=${username}`;
const json = await fetch(base + method).then((r) => r.json());
if (json.success === true) return json.player;
return null;
}
async function getLevel(username) {
const base = 10000;
const growth = 2500;
const reversePqPrefix = -(base - 0.5 * growth) / growth;
const reverseConst = reversePqPrefix ** 2;
const player = await getPlayer(username);
if (player === null) return null;
const exp = player.networkExp;
return exp < 0
? 1
: Math.floor(
1 + reversePqPrefix + Math.sqrt(reverseConst + (2 / growth) * exp)
);
}
const Discord = require('discord.js');
const hypixel = require('./hypixel');
const client = new Discord.Client();
const prefix = '!';
client.on('ready', function () {
console.log(`Logged in as ${client.user.username}`);
});
client.on('message', async function (message) {
const content = message.content;
if (content.startsWith(prefix)) {
const pieces = content.split(' ');
const command = pieces.shift();
if (command === prefix + 'level') {
const username = pieces.shift();
if (!username) {
message.channel.send('Specify a player name!');
return;
}
const level = await hypixel.getLevel(username);
if (level === null) {
message.channel.send('Player not found!');
return;
}
message.channel.send(`Level of user: ${level}`);
}
}
}); ``
