diff options
| author | Max Isom <[email protected]> | 2020-03-12 22:41:26 -0500 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2020-03-12 22:41:26 -0500 |
| commit | 17ba78f7b7d78c638ab00b9d4af79110130b0bcd (patch) | |
| tree | df0671a4b2845333198b57906b5dde68b709d37a /src/utils/loading-message.ts | |
| parent | 8eb4c8a6c06f672cb50efae5ea30215d465000af (diff) | |
| download | muse-17ba78f7b7d78c638ab00b9d4af79110130b0bcd.tar.xz muse-17ba78f7b7d78c638ab00b9d4af79110130b0bcd.zip | |
Use IoC, impliment queue
Diffstat (limited to 'src/utils/loading-message.ts')
| -rw-r--r-- | src/utils/loading-message.ts | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/utils/loading-message.ts b/src/utils/loading-message.ts new file mode 100644 index 0000000..3b3d138 --- /dev/null +++ b/src/utils/loading-message.ts @@ -0,0 +1,67 @@ +import {TextChannel, Message} from 'discord.js'; +import delay from 'delay'; + +export default class { + private readonly channel: TextChannel; + private readonly text: string; + private msg!: Message; + private isStopped: boolean = false; + + constructor(channel: TextChannel, text: string) { + this.channel = channel; + this.text = text; + } + + async start(): Promise<void> { + this.msg = await this.channel.send(this.text); + + const period = 500; + + const icons = ['⚪', '🔵', '⚫']; + + const reactions = []; + + let i = 0; + let isRemoving = false; + (async () => { + while (!this.isStopped) { + if (reactions.length === icons.length) { + isRemoving = true; + } + + // eslint-disable-next-line no-await-in-loop + await delay(period); + + 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++; + } + } + })(); + } + + async stop(str?: string): Promise<Message> { + 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; + } +} |
