90 lines
2.1 KiB
TypeScript
90 lines
2.1 KiB
TypeScript
import { CommandInteraction, GuildMember, TextChannel } from "discord.js";
|
|
|
|
/**
|
|
* shuffles an array
|
|
* https://stackoverflow.com/a/2450976/2856416
|
|
* @param array an array
|
|
* @returns an array but shuffled
|
|
*/
|
|
export function shuffle(array: any[]) {
|
|
let currentIndex = array.length, randomIndex;
|
|
|
|
// While there remain elements to shuffle...
|
|
while (currentIndex != 0) {
|
|
|
|
// Pick a remaining element...
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
currentIndex--;
|
|
|
|
// And swap it with the current element.
|
|
[array[currentIndex], array[randomIndex]] = [
|
|
array[randomIndex], array[currentIndex]];
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
|
|
|
|
export class errorMessage {
|
|
public msg: string;
|
|
public ephemeral: boolean;
|
|
|
|
constructor(msg: string, ephemeral: boolean = true) {
|
|
this.msg = msg;
|
|
this.ephemeral = ephemeral;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* a simple class to contain an error message and related data
|
|
* @param msg error message
|
|
* @param ephemeral (default=true)
|
|
* @returns new errorMessage
|
|
*/
|
|
export const emsg = (msg: string, ephemeral: boolean = true) => new errorMessage(msg, ephemeral);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type queueInfo = {
|
|
players: GuildMember[],
|
|
initiator: {
|
|
interaction: CommandInteraction,
|
|
member: GuildMember
|
|
},
|
|
teamsize: number,
|
|
timeout: NodeJS.Timeout
|
|
}
|
|
|
|
/**
|
|
* get the GuildMember of an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be read
|
|
* @returns member
|
|
*/
|
|
export function getMember(interaction: CommandInteraction): GuildMember {
|
|
let member = interaction.member;
|
|
|
|
if (!(member instanceof GuildMember))
|
|
throw emsg('Unable to retrieve guild member information, please try again');
|
|
|
|
return member;
|
|
}
|
|
|
|
/**
|
|
* get the TextChannel of an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be read
|
|
* @returns member
|
|
*/
|
|
export function getChannel(interaction: CommandInteraction): TextChannel {
|
|
let channel = interaction.channel;
|
|
|
|
if (!(channel instanceof TextChannel))
|
|
throw emsg('Unable to retrieve text channel information, please try again');
|
|
|
|
return channel;
|
|
} |