-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
61 lines (55 loc) · 2.47 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Client, Collection, GuildMember, TextChannel, User, Snowflake } from 'discord.js';
import * as config from './config.json';
import inquirer from 'inquirer';
import chalk from 'chalk';
interface PrizeWinners {
name: string;
winners: User[];
}
const client = new Client();
let guildFetched = false;
const roll = () => {
console.clear();
console.log(`\n`);
const channel = (client.channels.cache.get(config.messageToRoll.channelID) as TextChannel);
const promise: Promise<Collection<Snowflake, GuildMember>|void> = !guildFetched ? channel.guild.members.fetch() : new Promise((resolve) => resolve());
promise.then(() => {
channel.messages.fetch(config.messageToRoll.messageID).then((message) => {
message.reactions.cache.first()?.fetch().then((reaction) => {
reaction.users.fetch().then((users) => {
users = users.filter((user) => reaction.message.guild?.members.cache.has(user.id) || false)
const prizes: PrizeWinners[] = [];
config.prizes.forEach((prize) => {
const winners = users.random(prize.count);
prizes.push({
name: prize.name,
winners
});
console.log(`${chalk.green(prize.name)}`);
console.log(winners.map((winner) => `${winner.tag}`).join(', ')+'\n');
});
inquirer.prompt([
{
name: 'continue',
message: 'Are the winners correct?',
type: 'confirm'
}
]).then((answers) => {
if (!answers.continue) roll();
else {
const message = prizes.map((prize) => {
return `**${prize.name}**\n\nWon by ${prize.winners.map((winner) => winner.toString()).join(', ')}!`;
}).join('\n\n\n')+'\n\nCongratulations to the winners! :tada:';
(client.channels.cache.get(config.outputChannel) as TextChannel).send(message);
}
})
});
});
});
});
}
client.on('ready', () => {
console.log(`Ready. Serving ${client.guilds.cache.size} servers.`);
roll();
});
client.login(config.token);