Make ping quotes sequential but shuffled

This commit is contained in:
Erik 2024-10-29 13:26:26 +02:00
parent f9d57ab0b6
commit b3898a498b
2 changed files with 36 additions and 5 deletions

View File

@ -18,19 +18,23 @@ import { SlashCommand } from '../../../interfaces/index.js';
import DiscordClient from '../../../DiscordClient.js'; import DiscordClient from '../../../DiscordClient.js';
import InvokerWrapper from '../../wrappers/InvokerWrapper.js'; import InvokerWrapper from '../../wrappers/InvokerWrapper.js';
import Quotes from '../../../../constants/Quotes.js'; import Quotes from '../../../../constants/Quotes.js';
import Util from '../../../../utilities/Util.js';
class PingCommand extends SlashCommand class PingCommand extends SlashCommand
{ {
#index: number;
#quotes: string[][];
constructor (client: DiscordClient) constructor (client: DiscordClient)
{ {
super(client, { super(client, {
name: 'ping', name: 'ping',
description: 'Ping? Pong!', description: 'Ping? Pong!',
moduleName: 'utility', moduleName: 'utility',
}); });
this.#index = 0;
this.#quotes = [ ...Quotes ];
Util.shuffle(this.#quotes);
} }
async execute (invoker: InvokerWrapper) async execute (invoker: InvokerWrapper)
@ -39,11 +43,16 @@ class PingCommand extends SlashCommand
const number = (ping / 40); const number = (ping / 40);
const repeat = number > 1 ? number : 1; const repeat = number > 1 ? number : 1;
const index = Math.floor(Quotes.length * Math.random()); if (this.#index >= this.#quotes.length)
const [ quote, author ] = Quotes[index]; {
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' }); return invoker.reply(`P${'o'.repeat(repeat)}ng! \`${ping}ms\`\n\n> ${quote.replaceAll('\n', '\n> ')}\n\\- *${author}*`, { emoji: 'success' });
} }
} }
export default PingCommand; export default PingCommand;

View File

@ -196,6 +196,28 @@ class Util
throw error; throw error;
} }
/**
* Shuffles array in place
* @date 3/25/2024 - 11:25:09 AM
*
* @static
* @template T
* @param {T[]} array
* @returns {T[]}
*/
static shuffle<T> (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<unknown>, page = 1, pageLength = 10) static paginate (items: Array<unknown>, page = 1, pageLength = 10)
{ {
const maxPage = Math.ceil(items.length / pageLength); const maxPage = Math.ceil(items.length / pageLength);