Skip to content

Commit

Permalink
refactor: utility functions to align with TypeScript best practices
Browse files Browse the repository at this point in the history
Updated utility methods to use `public` access modifier and enhanced type annotations for consistency and clarity. Adjusted variable naming for adherence to coding conventions and fixed minor typos in regex patterns. Improved interaction handling for pagination functionality.
  • Loading branch information
gabrielmaialva33 committed Jan 18, 2025
1 parent 5e58902 commit 61131d7
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
ButtonBuilder,
ButtonStyle,
CommandInteraction,
Message,
type TextChannel,
} from 'discord.js'

import type MahinaBot from '#common/mahina_bot'
import type Context from '#common/context'
import MahinaBot from '#common/mahina_bot'

export class Utils {
static formatTime(ms: number): string {
public static formatTime(ms: number): string {
const minuteMs = 60 * 1000
const hourMs = 60 * minuteMs
const dayMs = 24 * hourMs
Expand All @@ -21,7 +21,7 @@ export class Utils {
return `${Math.floor(ms / dayMs)}d ${Math.floor((ms % dayMs) / hourMs)}h`
}

static updateStatus(client: MahinaBot, guildId?: string): void {
public static updateStatus(client: MahinaBot, guildId?: string): void {
const { user } = client
if (user && client.env.GUILD_ID && guildId === client.env.GUILD_ID) {
const player = client.manager.getPlayer(client.env.GUILD_ID)
Expand All @@ -39,15 +39,16 @@ export class Utils {
}
}

static chunk(array: any[], size: number) {
const chunkedArr: any[][] = []
public static chunk(array: any[], size: number) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const chunked_arr: any[][] = []
for (let index = 0; index < array.length; index += size) {
chunkedArr.push(array.slice(index, size + index))
chunked_arr.push(array.slice(index, size + index))
}
return chunkedArr
return chunked_arr
}

static formatBytes(bytes: number, decimals = 2): string {
public static formatBytes(bytes: number, decimals = 2): string {
if (bytes === 0) return '0 Bytes'
const k = 1024
const dm = decimals < 0 ? 0 : decimals
Expand All @@ -56,12 +57,12 @@ export class Utils {
return `${Number.parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`
}

static formatNumber(number: number): string {
public static formatNumber(number: number): string {
return number.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}

static parseTime(string: string): number {
const time = string.match(/([0-9]+[d,h,m,s])/g)
public static parseTime(string: string): number {
const time = string.match(/(\d+[dhms])/g)
if (!time) return 0
let ms = 0
for (const t of time) {
Expand All @@ -75,31 +76,33 @@ export class Utils {
return ms
}

static progressBar(current: number, total: number, size = 20): string {
public static progressBar(current: number, total: number, size = 20): string {
const percent = Math.round((current / total) * 100)
const filledSize = Math.round((size * current) / total)
const filledBar = '▓'.repeat(filledSize)
const emptyBar = '░'.repeat(size - filledSize)
return `${filledBar}${emptyBar} ${percent}%`
}

static async paginate(client: MahinaBot, ctx: Context, embed: any[]): Promise<void> {
public static async paginate(client: MahinaBot, ctx: Context, embed: any[]): Promise<void> {
if (embed.length < 2) {
if (ctx.isInteraction) {
ctx.deferred
? ctx.interaction?.followUp({ embeds: embed })
: ctx.interaction?.reply({ embeds: embed })
return
}

;(ctx.channel as TextChannel).send({ embeds: embed })
return
}

let page = 0
const getButton = (p: number): any => {
const firstEmbed = p === 0
const lastEmbed = p === embed.length - 1
const pageEmbed = embed[p]
// eslint-disable-next-line @typescript-eslint/no-shadow
const getButton = (page: number): any => {
const firstEmbed = page === 0
const lastEmbed = page === embed.length - 1
const pageEmbed = embed[page]
const first = new ButtonBuilder()
.setCustomId('first')
.setEmoji(client.emoji.page.first)
Expand Down Expand Up @@ -129,17 +132,25 @@ export class Utils {
}

const msgOptions = getButton(0)
const msg = ctx.isInteraction
? await (ctx.deferred
? ctx.interaction!.followUp({
...msgOptions,
fetchReply: true as boolean,
})
: ctx.interaction!.reply({ ...msgOptions, fetchReply: true }))
: await (ctx.channel as TextChannel).send({
let msg: Message
if (ctx.isInteraction) {
if (ctx.deferred) {
msg = await ctx.interaction!.followUp({
...msgOptions,
fetchReply: true,
})
} else {
msg = (await ctx.interaction!.reply({
...msgOptions,
fetchReply: true,
})) as unknown as Message
}
} else {
msg = await (ctx.channel as TextChannel).send({
...msgOptions,
fetchReply: true,
})
}

const author = ctx instanceof CommandInteraction ? ctx.user : ctx.author

Expand Down

0 comments on commit 61131d7

Please sign in to comment.