Skip to content

Commit

Permalink
added prettyBytes function
Browse files Browse the repository at this point in the history
  • Loading branch information
TejasLamba2006 committed Jul 4, 2023
1 parent 5eb37f8 commit b88f51f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 47 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const sleep = require('./src/functions/utilities/sleep'),
cleanCode = require("./src/functions/utilities/cleanCode"),
generateActivity = require("./src/functions/utilities/generateActivity"),
disableButtons = require("./src/functions/utilities/disableButtons"),
{ quickExport, exportChat, rawExport, Transcript } = require('./src/functions/tickets/main.js');
{ quickExport, exportChat, rawExport, Transcript } = require('./src/functions/tickets/main.js'),
prettyBytes = require("./src/functions/utilities/prettyBytes")


checkUpdate()
Expand All @@ -24,5 +25,6 @@ module.exports = {
quickExport,
exportChat,
rawExport,
Transcript
Transcript,
prettyBytes
};
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"name": "visa2discord",
"version": "1.1.0",
"version": "1.1.1",
"description": "A package for your discord needs",
"main": "index.js",
"directories": {
"test": "test"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"dependencies": {
"discord.js": "^14.11.0",
"moment": "^2.29.4",
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ For your discord utilities
- [Transcripts](#transcripts)
- [Methods](#methods)
- [Classes](#classes)
- [Pretty Bytes](#pretty-bytes)
- [Contributing](#contributing)
- [License](#license)
- [Credits](#credits)
Expand Down Expand Up @@ -309,6 +310,19 @@ For your discord utilities
- `Transcript`
- The `Transcript` class represents a chat transcript export and extends the `TranscriptDAO` class.

## Pretty Bytes
- Formats a number of bytes into a human-readable string representation.
- Usage
```js
const { prettyBytes } = require('visa2discord');
const bytes = 1000000000;
const formatted = prettyBytes(bytes);
console.log('Formatted bytes:', formatted);
```
Example output
```
Formatted bytes: 1 GB
```
# Contributing
- If you want to contribute to this project, you can fork this repository and make a pull request.
Expand Down
36 changes: 36 additions & 0 deletions src/functions/utilities/prettyBytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Formats a number of bytes into a human-readable string representation.
*
* @param {number} bytes - The number of bytes to format.
* @param {Object} [options={}] - Formatting options.
* @param {boolean} [options.bits=false] - If true, format the bytes as bits.
* @param {boolean} [options.signed=false] - If true, include a plus sign for positive values when using the signed format.
* @param {string} [options.locale='en'] - The locale to use for formatting. Can be 'en' (English) or 'de' (German).
* @returns {string} The formatted byte size string.
*/

module.exports = (bytes, options = {}) => {
const { bits = false, signed = false, locale = "en" } = options;

const units = bits
? ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"]
: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];

const kilo = bits ? 1024 : 1000;
const isNegative = signed && bytes < 0;
const num = isNegative ? -bytes : bytes;

if (num < 1) {
return (isNegative ? "-" : "") + num + " " + units[0];
}

const exponent = Math.min(
Math.floor(Math.log10(num) / Math.log10(kilo)),
units.length - 1
);
const value = (num / Math.pow(kilo, exponent)).toFixed(2);

const formattedValue = locale === "en" ? value : value.replace(".", ",");

return (isNegative ? "-" : "") + formattedValue + " " + units[exponent];
};
83 changes: 42 additions & 41 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
//make a simple discord.js client
const Discord = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildModeration,
Discord.GatewayIntentBits.GuildEmojisAndStickers,
Discord.GatewayIntentBits.GuildWebhooks,
Discord.GatewayIntentBits.GuildInvites,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.AutoModerationConfiguration,
Discord.GatewayIntentBits.AutoModerationExecution,
]
});
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const test = require('../src/functions/tickets/main.js');
const channel = await client.channels.fetch('1123861563141533754');
const allMessages = new Discord.Collection();
let messages = await channel.messages.fetch({ limit: 100, cache: false,
force: true, });
// //make a simple discord.js client
// const Discord = require('discord.js');
// const client = new Discord.Client({
// intents: [
// Discord.GatewayIntentBits.Guilds,
// Discord.GatewayIntentBits.GuildMembers,
// Discord.GatewayIntentBits.GuildModeration,
// Discord.GatewayIntentBits.GuildEmojisAndStickers,
// Discord.GatewayIntentBits.GuildWebhooks,
// Discord.GatewayIntentBits.GuildInvites,
// Discord.GatewayIntentBits.GuildVoiceStates,
// Discord.GatewayIntentBits.GuildMessages,
// Discord.GatewayIntentBits.DirectMessages,
// Discord.GatewayIntentBits.MessageContent,
// Discord.GatewayIntentBits.AutoModerationConfiguration,
// Discord.GatewayIntentBits.AutoModerationExecution,
// ]
// });
// client.on('ready', async () => {
// console.log(`Logged in as ${client.user.tag}!`);
// const test = require('../src/functions/tickets/main.js');
// const channel = await client.channels.fetch('1123861563141533754');
// const allMessages = new Discord.Collection();
// let messages = await channel.messages.fetch({ limit: 100, cache: false,
// force: true, });

allMessages.concat(messages);
//concat doesnt do anytthing do something else
// console.log(allMessages)
while (messages.size === 100) {
const lastMessageId = messages.lastKey();
messages = await channel.messages.fetch({ limit: 100, before: lastMessageId, cache: false,
force: true, });
allMessages.concat(messages);
}

const trsa = await test.quickExport(channel).catch(console.error);
channel.send({ files: [new Discord.AttachmentBuilder(trsa, { name: `xd.html` })] })
});
//login to discord with your app's token
client.login('MTEwOTA2NTQ0MjMwNTI1MzM4Ng.GSKaL5.K6nUopgM92RcZ-_2X6lSUwWcTP_Aa6qtIu324o');
//when the bot receives a message
// allMessages.concat(messages);
// //concat doesnt do anytthing do something else
// // console.log(allMessages)
// while (messages.size === 100) {
// const lastMessageId = messages.lastKey();
// messages = await channel.messages.fetch({ limit: 100, before: lastMessageId, cache: false,
// force: true, });
// allMessages.concat(messages);
// }

// const trsa = await test.quickExport(channel).catch(console.error);
// channel.send({ files: [new Discord.AttachmentBuilder(trsa, { name: `xd.html` })] })
// });
// //login to discord with your app's token
// client.login('MTEwOTA2NTQ0MjMwNTI1MzM4Ng.GSKaL5.K6nUopgM92RcZ-_2X6lSUwWcTP_Aa6qtIu324o');
// //when the bot receives a message

const {prettyBytes} = require('../index.js')
console.log(prettyBytes(100000, {locale: 'de'}, {bits: true}))

0 comments on commit b88f51f

Please sign in to comment.