muse/src/utils/loading-message.ts

74 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-03-13 04:41:26 +01:00
import {TextChannel, Message} from 'discord.js';
import delay from 'delay';
2020-03-20 01:16:07 +01:00
const INITAL_DELAY = 500;
const PERIOD = 500;
2020-03-13 04:41:26 +01:00
export default class {
2020-03-20 01:16:07 +01:00
public isStopped = true;
2020-03-13 04:41:26 +01:00
private readonly channel: TextChannel;
private readonly text: string;
private msg!: Message;
2020-03-17 23:59:26 +01:00
constructor(channel: TextChannel, text = 'cows! count \'em') {
2020-03-13 04:41:26 +01:00
this.channel = channel;
this.text = text;
}
async start(): Promise<void> {
this.msg = await this.channel.send(this.text);
const icons = ['🐮', '🐴', '🐄'];
2020-03-13 04:41:26 +01:00
const reactions = [];
let i = 0;
let isRemoving = false;
2020-03-20 01:16:07 +01:00
this.isStopped = false;
2020-03-13 04:41:26 +01:00
(async () => {
2020-03-20 01:16:07 +01:00
await delay(INITAL_DELAY);
2020-03-13 04:41:26 +01:00
while (!this.isStopped) {
if (reactions.length === icons.length) {
isRemoving = true;
}
// eslint-disable-next-line no-await-in-loop
2020-03-20 01:16:07 +01:00
await delay(PERIOD);
2020-03-13 04:41:26 +01:00
if (isRemoving) {
const reactionToRemove = reactions.shift();
if (reactionToRemove) {
// eslint-disable-next-line no-await-in-loop
await reactionToRemove.remove();
} else {
isRemoving = false;
}
} else {
if (!this.isStopped) {
// eslint-disable-next-line no-await-in-loop
reactions.push(await this.msg.react(icons[i % icons.length]));
}
i++;
}
}
})();
}
2020-03-17 23:59:26 +01:00
async stop(str = 'u betcha'): Promise<Message> {
2020-03-13 04:41:26 +01:00
this.isStopped = true;
if (str) {
await Promise.all([this.msg.reactions.removeAll(), this.msg.edit(str)]);
} else {
await this.msg.reactions.removeAll();
}
return this.msg;
}
}