Skip to content

Commit

Permalink
docs: تحديث أسلوب إستيراد الأنواع
Browse files Browse the repository at this point in the history
  • Loading branch information
Assayyaad committed Sep 1, 2024
1 parent fc8d43a commit 7bfbaec
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 43 deletions.
33 changes: 17 additions & 16 deletions src/class/discordBot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @import {BotData, BotCommand, BotOptions, SuccessRecord, BotEvent, ClientEvent, ClientEventFunction, RepeatingEvent, RepeatingEventFunction, FailRecord} from '../options.js' */
import { findPaths, storeFolderPaths, readFolderPaths } from '@disqada/pathfinder'
import { Client, Events, GatewayIntentBits } from 'discord.js'
import { setTimeout } from 'timers/promises'
Expand All @@ -13,7 +14,7 @@ import { ready } from '../events/ready.js'
*/
export class DiscordBot extends Client {
/**
* @type {import('../options').BotData}
* @type {BotData}
*/
data = {
config: {
Expand All @@ -32,20 +33,20 @@ export class DiscordBot extends Client {
}

/**
* @type {Map<string, import('../options').BotCommand>}
* @type {Map<string, BotCommand>}
*/
commands = new Map()

/**
* Will be deleted in the next major release
* @type {import('discord.js').Client}
* @type {Client}
* @deprecated The bot class now extends `Client`, use it instead
*/
client

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

/**
* Start and connect the bot.
* @param {import('../options').BotOptions} options - Information about the DiscordBot.
* @param {BotOptions} options - Information about the DiscordBot.
* @returns {Promise<void>}
* @async
* @private
Expand Down Expand Up @@ -137,8 +138,8 @@ export class DiscordBot extends Client {

/**
* Register a command inside the bot.
* @param {import('../options').BotCommand} command - The bot command module.
* @returns {import('../options').SuccessRecord}
* @param {BotCommand} command - The bot command module.
* @returns {SuccessRecord}
*/
registerCommand(command) {
this.commands.set(command.data.name, command)
Expand All @@ -152,11 +153,11 @@ export class DiscordBot extends Client {

/**
* Register an event inside the bot.
* @param {import('../options').BotEvent} event - The bot event module.
* @returns {import('../options').SuccessRecord}
* @param {BotEvent} event - The bot event module.
* @returns {SuccessRecord}
*/
registerEvent(event) {
/** @type {import('../options').SuccessRecord} */
/** @type {SuccessRecord} */
const record = {
name: '',
type: event.data.module,
Expand All @@ -165,11 +166,11 @@ export class DiscordBot extends Client {

switch (event.data.module) {
case 'event': {
/** @type {import('../options').ClientEvent<any>} */
/** @type {ClientEvent<any>} */
// @ts-expect-error
const e = event
const { data, execute } = e
/** @type {import('../options').ClientEventFunction<any>} */
/** @type {ClientEventFunction<any>} */
const func = (args) => execute(this, args)

if (data.name === Events.ClientReady || data.once) {
Expand All @@ -183,11 +184,11 @@ export class DiscordBot extends Client {
}

case 'event-repeat': {
/** @type {import('../options').RepeatingEvent} */
/** @type {RepeatingEvent} */
// @ts-expect-error
const e = event
const { data, execute } = e
/** @type {import('../options').RepeatingEventFunction} */
/** @type {RepeatingEventFunction} */
const func = async () => {
let first
let nextWait
Expand Down Expand Up @@ -243,9 +244,9 @@ export class DiscordBot extends Client {
return
}

/** @type {import('../options').SuccessRecord[]} */
/** @type {SuccessRecord[]} */
const successRecords = []
/** @type {import('../options').FailRecord[]} */
/** @type {FailRecord[]} */
const failRecords = []

for (let i = 0; i < paths.length; i++) {
Expand Down
5 changes: 3 additions & 2 deletions src/events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/** @import {Brand, CommandInteraction} from '../options.js' */
import { applyStyle, asEmbed } from '../func/style'

/**
* Handler of interactions called by the bot.
* @param {import('../options').CommandInteraction} interaction - The interaction to handle.
* @param {CommandInteraction} interaction - The interaction to handle.
* @returns {Promise<void>}
* @private
*/
export async function interactionCreate(interaction) {
/**
* A shortcut to a full reply.
* @param {string} reply - The message to reply with.
* @param {import('../options').Brand} brand - The brand to style the reply with.
* @param {Brand} brand - The brand to style the reply with.
*/
async function quickReply(reply, brand) {
const embed = asEmbed(reply, brand)
Expand Down
18 changes: 11 additions & 7 deletions src/events/ready.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/** @import {CommandData, GuildIDs} from '../options.js' */
/** @import {DiscordBot} from '../class/discordBot.js' */

/**
* Get the correct server ID.
* @param {import('../options').CommandData} data - The command data deciding where to register the command.
* @param {import('../options').GuildIDs} guildIds - The container of the server IDs.
* @param {CommandData} data - The command data deciding where to register the command.
* @param {GuildIDs} guildIds - The container of the server IDs.
* @returns {string} The server ID.
* @throws {Error} - if data.deployment wasn't of the enum values.
* @private
Expand All @@ -24,8 +27,8 @@ export function getGuildId(data, guildIds) {

/**
* Preparing the bot commands for registration.
* @param {import('../class/discordBot').DiscordBot} bot - The bot to register the commands for.
* @returns {Map<string, import('../options').CommandData[]>}
* @param {DiscordBot} bot - The bot to register the commands for.
* @returns {Map<string, CommandData[]>}
* @private
*/
export function prepareCommands(bot) {
Expand All @@ -45,8 +48,8 @@ export function prepareCommands(bot) {

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

/**
* The bot is ready and has connected successfully.
* @param {import('../class/discordBot').DiscordBot} bot - The bot.
* @param {DiscordBot} bot - The bot.
* @returns {Promise<void>}
* @async
* @private
Expand All @@ -76,6 +79,7 @@ export async function ready(bot) {

if (bot.user) {
console.log(`-> The Bot '${bot.user.username}' Is Online <-`)
// @ts-expect-error
bot.user.setPresence(bot.data.config.presence)
} else {
console.log('-> Bot Is Online <-')
Expand Down
8 changes: 5 additions & 3 deletions src/func/log.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/** @import {FailRecord, SuccessRecord} from '../options.js' */
/** @import {TableUserConfig} from 'table' */
import { table } from 'table'

/**
* Get the default configurations for the table.
* @param {string} header - The name of the table.
* @param {number} width - The width of the table rows.
* @returns {import('table').TableUserConfig} Ready to use configurations.
* @returns {TableUserConfig} Ready to use configurations.
* @example
* const finalConfig = defaultConfig('Table name')
* @private
Expand All @@ -27,7 +29,7 @@ function defaultConfig(header, width) {

/**
* Log the table for successful records.
* @param {import('../options').SuccessRecord[]} records - The records to log as a table.
* @param {SuccessRecord[]} records - The records to log as a table.
* @returns {string} The table as a string.
* @example
* const records = [{ ... }]
Expand All @@ -51,7 +53,7 @@ export function logSuccessRecords(records) {

/**
* Log the table for failed records.
* @param {import('../options').FailRecord[]} records - The records to log as a table.
* @param {FailRecord[]} records - The records to log as a table.
* @returns {string} The table as a string.
* @example
* const records = [{ ... }]
Expand Down
27 changes: 14 additions & 13 deletions src/func/style.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
/** @import {Embed, Brand, StyleOptions} from '../options.js' */
import { asNumber } from './colour.js'

/**
* Convert a text to an embed.
* @param {string} text - The original text.
* @param {import('../options').Brand} brand - The brand to style the embed with.
* @returns {import('../options').Embed} The resulting embed.
* @param {Brand} brand - The brand to style the embed with.
* @returns {Embed} The resulting embed.
* @example
* const brand = { colour: 0xffffff }
* const result = asEmbed('HalfBot', brand)
*/
export function asEmbed(text, brand) {
/** @type {import('../options').Embed} */
/** @type {Embed} */
const embed = { description: text }
// @ts-expect-error
return applyStyle(embed, brand)
}

/**
* Apply brand style to an multiple embeds.
* @param {import('../options').Embed | import('../options').Embed[]} toApplyOn - The embeds to apply the style to.
* @param {import('../options').Brand} brand - The brand to get the style from.
* @returns {import('../options').Embed | import('../options').Embed[]} The embeds array after applying the style to it.
* @param {Embed | Embed[]} toApplyOn - The embeds to apply the style to.
* @param {Brand} brand - The brand to get the style from.
* @returns {Embed | Embed[]} The embeds array after applying the style to it.
* @example
* let embed = { title: 'HalfBot' }
* const brand = { colour: 0xffffff }
Expand All @@ -39,10 +40,10 @@ export function applyStyle(toApplyOn, brand) {

/**
* Apply brand style to an embed.
* @param {import('../options').Embed} embed - The embed to apply the style to.
* @param {import('../options').Brand} brand - The brand to get the style from.
* @param {import('../options').StyleOptions} options - Configuration options for the process.
* @returns {import('../options').Embed} The embed after applying the style to it.
* @param {Embed} embed - The embed to apply the style to.
* @param {Brand} brand - The brand to get the style from.
* @param {StyleOptions} options - Configuration options for the process.
* @returns {Embed} The embed after applying the style to it.
* @example
* let embed = { title: 'HalfBot' }
* const brand = { colour: 0xffffff }
Expand Down Expand Up @@ -72,9 +73,9 @@ export function applyStyle(toApplyOn, brand) {

/**
* Apply brand style to an multiple embeds.
* @param {import('../options').Embed[]} embeds - The embeds to apply the style to.
* @param {import('../options').Brand} brand - The brand to get the style from.
* @returns {import('../options').Embed[]} The embeds array after applying the style to it.
* @param {Embed[]} embeds - The embeds to apply the style to.
* @param {Brand} brand - The brand to get the style from.
* @returns {Embed[]} The embeds array after applying the style to it.
* @example
* let embeds = [{ title: 'HalfBot' }]
* const brand = { colour: 0xffffff }
Expand Down
6 changes: 4 additions & 2 deletions src/func/validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** @import {BotCommand, BotEvent} from '../options.js' */

/**
* Check a command's validity.
* @param {import('../options').BotCommand} obj - The command to check.
* @param {BotCommand} obj - The command to check.
* @returns {boolean} True if valid, false otherwise.
* @private
*/
Expand All @@ -27,7 +29,7 @@ export function validCommand(obj) {

/**
* Check an event's validity.
* @param {import('../options').BotEvent} obj - The event to check.
* @param {BotEvent} obj - The event to check.
* @returns {boolean} True if valid, false otherwise.
* @private
*/
Expand Down

0 comments on commit 7bfbaec

Please sign in to comment.