Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the Weight Command #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The server is the server the Minecraft client should connect to, by default it w

#### Minecraft

The minecraft section includes a `username` and `password` option, if using a Mojang account these should be filled out with your Mojang username and password for the Minecraft account you plan on using, your Minecraft username is most likely the email it was created with. If using with a microsoft account change `accountType` to `microsoft`, `username` and `password` are not required and will be left blank as you will be directed to the [Microsoft Link page](https://www.microsoft.com/link). There is also a `lobbyHolder` option which is used in the `!guildlobby` command, this command will whisper the user specified in the config with a message using the `?tw <username>` format, for this command to do anything another bot needs to listen, and then act when receiving the message.
The minecraft section includes a `username` and `password` option, if using a Mojang account these should be filled out with your Mojang username and password for the Minecraft account you plan on using, your Minecraft username is most likely the email it was created with. If using with a microsoft account change `accountType` to `microsoft`, `username` and `password` are not required and will be left blank as you will be directed to the [Microsoft Link page](https://www.microsoft.com/link). There is also a `lobbyHolder` option which is used in the `!guildlobby` command, this command will whisper the user specified in the config with a message using the `?tw <username>` format, for this command to do anything another bot needs to listen, and then act when receiving the message. And there is a `apikey` option this option is for the Hypixel API key.

#### Discord

Expand Down
70 changes: 70 additions & 0 deletions src/minecraft/commands/WeightCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const MinecraftCommand = require('../../contracts/MinecraftCommand')
const fetch = require('node-fetch')
KlaasNiphaas marked this conversation as resolved.
Show resolved Hide resolved

class WeightCommand extends MinecraftCommand {
constructor(minecraft) {
super(minecraft)

this.name = 'weight'
this.aliases = ['we']
this.description = "Tells a player his weight"
}

async onCommand(username, message) {
let args = message.split(' ')
let ign = args[1]

if (args.length == 1) {
ign = `${username}`
KlaasNiphaas marked this conversation as resolved.
Show resolved Hide resolved
}

fetch(`https://api.mojang.com/users/profiles/minecraft/${ign}`)
.then(res => {
if (res.status != 200) {
return this.send(`/gc No MC account found for ${ign}`)
}
})

ign = await getTrueIgn(ign);

const apiData = await getApiData(ign);

if (apiData.status != 200) {
this.send(`/gc ${apiData.reason}`)
KlaasNiphaas marked this conversation as resolved.
Show resolved Hide resolved
}

if (apiData.data.skills.apiEnabled == false) this.send(`/gc This user has skill API disabled`)
KlaasNiphaas marked this conversation as resolved.
Show resolved Hide resolved

if (apiData.data.dungeons == null) this.send(`/gc ${ign} hasn't entered Dungeons`)
KlaasNiphaas marked this conversation as resolved.
Show resolved Hide resolved

this.send(`/gc ${ign} his weight for their ${apiData.data.name} profile is ${toFixed(apiData.data.weight)} + ${toFixed(apiData.data.weight_overflow)} Overflow ${toFixed(apiData.data.weight + apiData.data.weight_overflow)} Total`)
}
}

function toFixed(num) {
var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (0 || -1) + '})?');
return num.toString().match(re)[0];
}

async function getUUID(ign) {
const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${ign}`);
const result = await response.json();
return result.id;
}

async function getApiData(ign) {
delete require.cache[require.resolve('../../../config.json')];
const config = require('../../../config.json');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break Docker support as the module will not be found, if you wanted to add to be ability to change the config without restarting the bot you'd have to add a method to the Configuration class to refresh it every time the API key is used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed in the next commit.

Copy link
Author

@KlaasNiphaas KlaasNiphaas Aug 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed i think


const UUID = await getUUID(ign);
const response = await fetch(`https://hypixel-api.senither.com/v1/profiles/${UUID}/save?key=${config.minecraft.apikey}`);
return await response.json();
}

async function getTrueIgn(ign) {
const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${ign}`);
const result = await response.json();
return result.name;
}

module.exports = WeightCommand