Skip to content

Commit

Permalink
refactor: إعادة تسمية صنف البوت الرئيسي وكلمة اللون
Browse files Browse the repository at this point in the history
...

BREAKING CHANGE: تم تغيير امس الصنف DiscordBot إلى Bot فعلى المستحدم تغيير الاسم تبعاً
  • Loading branch information
Assayyaad committed Sep 3, 2024
1 parent d6fc420 commit c59f0f8
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 77 deletions.
10 changes: 5 additions & 5 deletions src/class/discordBot.js → src/class/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ready } from '../events/ready.js'
/**
* @class
*/
export class DiscordBot extends Client {
export class Bot extends Client {
/**
* @type {BotData}
*/
Expand All @@ -23,7 +23,7 @@ export class DiscordBot extends Client {
},
brand: {
name: 'HalfBot',
colour: 0xffffff,
color: 0xffffff,
logoUrl: 'https://cdn.discordapp.com/embed/avatars/0.png'
},
presence: {
Expand All @@ -45,8 +45,8 @@ export class DiscordBot extends Client {
client

/**
* The initialization of a new DiscordBot.
* @param {BotOptions} options - Information about the DiscordBot.
* The initialization of a new Bot.
* @param {BotOptions} options - Information about the Bot.
*/
constructor(options) {
super(
Expand All @@ -61,7 +61,7 @@ export class DiscordBot extends Client {

/**
* Start and connect the bot.
* @param {BotOptions} options - Information about the DiscordBot.
* @param {BotOptions} options - Information about the Bot.
* @returns {Promise<void>}
* @async
* @private
Expand Down
8 changes: 4 additions & 4 deletions src/events/ready.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @import {CommandData, GuildIDs} from '../options.js' */
/** @import {DiscordBot} from '../class/discordBot.js' */
/** @import {Bot} from '../class/bot.js' */

/**
* Get the correct server ID.
Expand Down Expand Up @@ -27,7 +27,7 @@ export function getGuildId(data, guildIds) {

/**
* Preparing the bot commands for registration.
* @param {DiscordBot} bot - The bot to register the commands for.
* @param {Bot} bot - The bot to register the commands for.
* @returns {Map<string, CommandData[]>}
* @private
*/
Expand All @@ -47,7 +47,7 @@ export function prepareCommands(bot) {

/**
* Register the commands via the API.
* @param {DiscordBot} bot - The bot to register the commands for.
* @param {Bot} bot - The bot to register the commands for.
* @param {Map<string, CommandData[]>} commandMap - The commands to register.
* @returns {Promise<void>}
* @private
Expand All @@ -61,7 +61,7 @@ async function registerCommands(bot, commandMap) {

/**
* The bot is ready and has connected successfully.
* @param {DiscordBot} bot - The bot.
* @param {Bot} bot - The bot.
* @returns {Promise<void>}
* @async
* @private
Expand Down
4 changes: 2 additions & 2 deletions src/exports.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { DiscordBot } from './class/discordBot.js'
export { asNumber, asString } from './func/colour.js'
export { Bot } from './class/bot.js'
export { asNumber, asString } from './func/color.js'
export { asEmbed, applyStyle } from './func/style.js'
export * from './options.js'
43 changes: 43 additions & 0 deletions src/func/color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Get a color as a number.
* @param {string | number} color - The color value.
* @returns {number} The color as a number.
* @example
* const num = asNumber('#ffffff')
* // num = 0xffffff
* @example
* const num = asNumber(0xffffff)
* // num = 0xffffff
*/
export function asNumber(color) {
let num

if (typeof color === 'number') num = color
else if (typeof color === 'string') num = Number.parseInt(color.substring(1), 16)

if (num) return num
else return 0
}

/**
* Get a color as a string.
* @param {string | number} color - The color value.
* @returns {string} The color as a string.
* @example
* const num = asNumber('#ffffff')
* // num = '#ffffff'
* @example
* const num = asNumber(0xffffff)
* // num = '#ffffff'
*/
export function asString(color) {
let str

if (typeof color === 'string') str = color.startsWith('#') ? color.substring(1) : color
else if (typeof color === 'number') str = color.toString(16)

if (str) {
while (str.length < 6) str = '0' + str
return `#${str}`
} else return `#000000`
}
43 changes: 0 additions & 43 deletions src/func/colour.js

This file was deleted.

14 changes: 7 additions & 7 deletions src/func/style.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/** @import {Embed, Brand, StyleOptions} from '../options.js' */
import { asNumber } from './colour.js'
import { asNumber } from './color.js'

/**
* Convert a text to an embed.
* @param {string} text - The original text.
* @param {Brand} brand - The brand to style the embed with.
* @returns {Embed} The resulting embed.
* @example
* const brand = { colour: 0xffffff }
* const brand = { color: 0xffffff }
* const result = asEmbed('HalfBot', brand)
*/
export function asEmbed(text, brand) {
Expand All @@ -24,11 +24,11 @@ export function asEmbed(text, brand) {
* @returns {Embed | Embed[]} The embeds array after applying the style to it.
* @example
* let embed = { title: 'HalfBot' }
* const brand = { colour: 0xffffff }
* const brand = { color: 0xffffff }
* embed = applyStyle(embed, brand)
* @example
* let embeds = [{ title: 'HalfBot' }]
* const brand = { colour: 0xffffff }
* const brand = { color: 0xffffff }
* embeds = applyStyle(embeds, brand)
*/
export function applyStyle(toApplyOn, brand) {
Expand All @@ -43,11 +43,11 @@ export function applyStyle(toApplyOn, brand) {
* @returns {Embed} The embed after applying the style to it.
* @example
* let embed = { title: 'HalfBot' }
* const brand = { colour: 0xffffff }
* const brand = { color: 0xffffff }
* embed = applyToEmbed(embed, brand)
*/
function applyToEmbed(embed, brand, options = { skipFooter: false }) {
if (brand.colour) embed.color = asNumber(brand.colour)
if (brand.color) embed.color = asNumber(brand.color)

if (!options.skipFooter) {
if (brand.name) {
Expand All @@ -68,7 +68,7 @@ export function applyStyle(toApplyOn, brand) {
* @returns {Embed[]} The embeds array after applying the style to it.
* @example
* let embeds = [{ title: 'HalfBot' }]
* const brand = { colour: 0xffffff }
* const brand = { color: 0xffffff }
* embeds = applyToEmbeds(embeds, brand)
*/
function applyToEmbeds(embeds, brand) {
Expand Down
10 changes: 5 additions & 5 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @import {ApplicationCommandData, ChatInputCommandInteraction, ClientOptions, MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction, InteractionReplyOptions, ClientEvents} from 'discord.js' */
/** @import {DiscordBot} from './class/discordBot.js' */
/** @import {Bot} from './class/bot.js' */

export {}

Expand Down Expand Up @@ -58,7 +58,7 @@ export {}
* The container of all IDs
* @typedef {object} Brand
* @property {string} name
* @property {number} colour hex colour
* @property {number} color hex color
* @property {string} logoUrl
*/

Expand Down Expand Up @@ -124,7 +124,7 @@ export {}

/**
* @typedef {object} BaseCommandInteraction
* @property {DiscordBot} bot
* @property {Bot} bot
*/

/**
Expand Down Expand Up @@ -159,7 +159,7 @@ export {}
/**
* @template {keyof ClientEvents} Key
* @callback ClientEventFunction
* @param {DiscordBot} bot
* @param {Bot} bot
* @param {...ClientEvents[Key]} args
* @returns {any}
*/
Expand All @@ -180,7 +180,7 @@ export {}

/**
* @callback RepeatingEventFunction
* @param {DiscordBot} bot
* @param {Bot} bot
* @returns {Promise<void | number | string>}
*/

Expand Down
12 changes: 6 additions & 6 deletions test/func/colour.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { equal } from 'assert/strict'
import { asNumber, asString } from '../../src/func/colour.js'
import { asNumber, asString } from '../../src/func/color.js'

describe('func', function () {
describe('colour', function () {
describe('color', function () {
describe('asNumber()', function () {
it('should return the same number', function () {
const result = asNumber(0xffffff)
equal(result, 0xffffff)
})

it('should return the number version of the colour', function () {
it('should return the number version of the color', function () {
const result = asNumber('#ffffff')
equal(result, 0xffffff)
})
Expand All @@ -19,7 +19,7 @@ describe('func', function () {
equal(result, 0x0000ff)
})

it('should return the number version of the colour in full length', function () {
it('should return the number version of the color in full length', function () {
const result = asNumber('#ff')
equal(result, 0x0000ff)
})
Expand All @@ -31,7 +31,7 @@ describe('func', function () {
equal(result, '#ffffff')
})

it('should return the string version of the colour', function () {
it('should return the string version of the color', function () {
const result = asString(0xffffff)
equal(result, '#ffffff')
})
Expand All @@ -41,7 +41,7 @@ describe('func', function () {
equal(result, '#0000ff')
})

it('should return the string version of the colour in full length', function () {
it('should return the string version of the color in full length', function () {
const result = asString(0xff)
equal(result, '#0000ff')
})
Expand Down
10 changes: 5 additions & 5 deletions test/func/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { applyStyle, asEmbed } from '../../src/func/style.js'

const brand = {
name: 'HalfBot',
colour: 0xffffff,
color: 0xffffff,
logoUrl: 'https://cdn.discordapp.com/embed/avatars/0.png'
}
const str = 'test'
Expand All @@ -15,7 +15,7 @@ describe('func', function () {
const result = asEmbed(str, brand)
deepEqual(result, {
description: str,
color: brand.colour,
color: brand.color,
footer: { text: brand.name, iconUrl: brand.logoUrl }
})
})
Expand All @@ -26,16 +26,16 @@ describe('func', function () {
const result = applyStyle({ title: str }, brand)
deepEqual(result, {
title: str,
color: brand.colour,
color: brand.color,
footer: { text: brand.name, iconUrl: brand.logoUrl }
})
})

it('should return a styled embeds containing the string', function () {
const result = applyStyle([{ title: str }, { title: str }], brand)
deepEqual(result, [
{ title: str, color: brand.colour },
{ title: str, color: brand.colour, footer: { text: brand.name, iconUrl: brand.logoUrl } }
{ title: str, color: brand.color },
{ title: str, color: brand.color, footer: { text: brand.name, iconUrl: brand.logoUrl } }
])
})
})
Expand Down

0 comments on commit c59f0f8

Please sign in to comment.