Hey everyone! I’m working on a Discord bot and I’m stuck. I want it to change decimal numbers to hex, kind of like those online converters. Here’s what I’ve got so far:
Cmds.toHex = {
name: 'toHex',
info: 'Changes numbers to hex',
cooldown: 5,
rank: 0,
run: function (message, input) {
message.reply('Converting...').then((m) => {
m.edit(`<@${message.author.id}>, Your hex number is: ${input.toString(16).toUpperCase()}`)
})
}
}
But it’s not quite right. I want it to work for any decimal number, not just Steam IDs. Also, it should pad with zeros if needed. Any ideas on how to fix this? Thanks a bunch!
I’ve implemented a similar feature in one of my Discord bots. Here’s a suggestion that might help:
Instead of using parseInt, try using Number() to convert the input. It handles decimals better. Also, consider adding a check for negative numbers, as they can be tricky in hex conversion.
Here’s a modified version of your code:
Cmds.toHex = {
name: 'toHex',
info: 'Changes numbers to hex',
cooldown: 5,
rank: 0,
run: function (message, input) {
const num = Number(input);
if (isNaN(num)) {
message.reply('Please provide a valid number.');
return;
}
const hex = (num < 0 ? '-' : '') + Math.abs(num).toString(16).toUpperCase();
message.reply(`Your hex number is: ${hex}`);
}
}
This should handle a wider range of inputs, including decimals and negative numbers. Let me know if you need any clarification!
hey man, i had similar issue. try this:
Cmds.toHex = {
name: 'toHex',
info: 'Changes numbers to hex',
cooldown: 5,
rank: 0,
run: function (message, input) {
const num = parseFloat(input);
if (isNaN(num)) {
message.reply('gimme a real number dude');
return;
}
const hex = num.toString(16).toUpperCase();
message.reply(`ur hex is: ${hex}`);
}
}
this handles decimals n stuff. lmk if it works!
I’ve been working on similar bots and encountered a similar issue. What helped me was ensuring the input is correctly parsed as an integer and then formatting it as hexadecimal properly. In the code below, the input is first converted using parseInt, and if it’s not a valid number, a response is sent back. Once confirmed as valid, it’s converted to hex, uppercased, and padded with zeros. This ensures that single-digit numbers become two digits. The revised code should help you handle a broad range of decimal inputs effectively:
Cmds.toHex = {
name: 'toHex',
info: 'Changes decimal numbers to hex',
cooldown: 5,
rank: 0,
run: function (message, input) {
const decimal = parseInt(input);
if (isNaN(decimal)) {
message.reply('Please provide a valid decimal number.');
return;
}
const hex = decimal.toString(16).toUpperCase().padStart(2, '0');
message.reply(`Your hex number is: ${hex}`);
}
}
This modification reliably parses and formats any decimal number into a proper hexadecimal string. Hope this helps with your bot development!