diff --git a/src/client/components/commands/utility/Ping.ts b/src/client/components/commands/utility/Ping.ts index a1e7e50..6087e1b 100644 --- a/src/client/components/commands/utility/Ping.ts +++ b/src/client/components/commands/utility/Ping.ts @@ -18,19 +18,23 @@ import { SlashCommand } from '../../../interfaces/index.js'; import DiscordClient from '../../../DiscordClient.js'; import InvokerWrapper from '../../wrappers/InvokerWrapper.js'; import Quotes from '../../../../constants/Quotes.js'; +import Util from '../../../../utilities/Util.js'; class PingCommand extends SlashCommand { - + #index: number; + #quotes: string[][]; constructor (client: DiscordClient) { - super(client, { name: 'ping', description: 'Ping? Pong!', moduleName: 'utility', }); + this.#index = 0; + this.#quotes = [ ...Quotes ]; + Util.shuffle(this.#quotes); } async execute (invoker: InvokerWrapper) @@ -39,11 +43,16 @@ class PingCommand extends SlashCommand const number = (ping / 40); const repeat = number > 1 ? number : 1; - const index = Math.floor(Quotes.length * Math.random()); - const [ quote, author ] = Quotes[index]; + if (this.#index >= this.#quotes.length) + { + this.#index = 0; + Util.shuffle(this.#quotes); + } + + const index = this.#index++; + const [ quote, author ] = this.#quotes[index]; return invoker.reply(`P${'o'.repeat(repeat)}ng! \`${ping}ms\`\n\n> ${quote.replaceAll('\n', '\n> ')}\n\\- *${author}*`, { emoji: 'success' }); } - } export default PingCommand; \ No newline at end of file diff --git a/src/utilities/Util.ts b/src/utilities/Util.ts index d838780..f99e679 100644 --- a/src/utilities/Util.ts +++ b/src/utilities/Util.ts @@ -196,6 +196,28 @@ class Util throw error; } + /** + * Shuffles array in place + * @date 3/25/2024 - 11:25:09 AM + * + * @static + * @template T + * @param {T[]} array + * @returns {T[]} + */ + static shuffle (array: T[]): T[] + { + let current = array.length; + let random = 0; + while (current > 0) + { + random = Math.floor(Math.random() * current); + current--; + [ array[current], array[random] ] = [ array[random], array[current] ]; + } + return array; + } + static paginate (items: Array, page = 1, pageLength = 10) { const maxPage = Math.ceil(items.length / pageLength);