-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (103 loc) · 4.45 KB
/
index.js
File metadata and controls
118 lines (103 loc) · 4.45 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import * as fs from 'fs';
import Discord from 'discord.js';
import Tournament from './tournament.js';
const client = new Discord.Client();
const config = {};
const guildMap = {};
(async () => {
const contents = await fs.promises.readFile('config.json', 'utf8');
Object.assign(config, JSON.parse(contents));
client.login(config.auth);
})()
client.once('ready', () => {
console.log('Ready!');
});
client.on('ready', () => {
console.log('connected');
client.guilds.cache.each((g) => {
if (g.available) {
guildMap[g.id] = guildMap[g.id] || {};
guildMap[g.id].tournament = new Tournament();
let staffRole = g.roles.cache.find((role) => role.name === config.role);
if (staffRole) {
guildMap[g.id].role = staffRole.id;
} else {
g.roles.create({
data: {
name: config.role,
color: 'YELLOW',
},
reason: 'The bot needs a Staff role for special actions',
})
.then((r) => {
console.log('Created missing Staff role for guild');
guildMap[g.id].role = r.id;
})
.catch(console.error);
}
}
});
});
const allowMentions = { allowedMentions: { parse: ['users'] } };
client.on('message', async message => {
const guild = guildMap[message.guild.id];
// - !checkin (ORGANIZER - se nao houver torneio a decorrer, cria um torneio)
if (message.content === '!checkin' && message.member.roles.cache.has(guild.role)) {
message.channel.send(guild.tournament.checkin());
}
// - !join (PLAYER - o jogador junta-se ao torneio a decorrer)
if (message.content === '!join') {
message.channel.send(guild.tournament.add(message.author), allowMentions);
}
// - !standings (ANY - mostra standings atuais)
if (message.content === '!standings' && message.member.roles.cache.has(guild.role)) {
const [header, rows] = guild.tournament.standings();
await message.channel.send(header, allowMentions)
for (const row of rows) {
await message.channel.send(row, allowMentions);
}
}
// - !start (ORGANIZER - se o torneio estiver em modo checkin, passa para o modo playing,
// debitando a lista de jogadores inscritos sob a forma de pairings para a primeira ronda)
if (message.content === '!start' && message.member.roles.cache.has(guild.role)) {
const [header, rows] = guild.tournament.start();
await message.channel.send(header, allowMentions)
for (const row of rows) {
await message.channel.send(row, allowMentions);
}
}
// - !result [1-0-0, 2-0-0 , etc] (JOGADOR o jogador envia o resultado para o bot, o bot fica com o
// resultado nao aprovado, e manda mensagem ao oponente para que este aprove (y/n), se o oponente
// aprovar, fica o resultado em modo definitivo, senao o resultado é apagado e o primeiro jogador notificado)
if (message.content.startsWith('!result')) {
const [_, result] = message.content.split(' ');
const [output, opponent] = guild.tournament.submitResult(message.author, ...(result.split('-')));
const filter = (reaction, user) => (user.id === opponent.id);
const reply = await message.channel.send(output, allowMentions);
await reply.react('👍');
await reply.react('👎');
const collector = reply.createReactionCollector(filter, { time: 20000, max: 1 })
collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '👍') {
message.channel.send(guild.tournament.confirmResult(opponent), allowMentions)
}
if (reaction.emoji.name === '👎') {
message.channel.send(guild.tournament.denyResult(opponent), allowMentions)
}
});
}
// - !next (ORGANIZADOR, o torneio avanca para a proxima ronda, se houver resultados em falta,
// sao automaticamente empate. Se a ronda anterior for a ultima, o torneio acaba e mostra os standings finais)
if (message.content === '!next' && message.member.roles.cache.has(guild.role)) {
const [header, rows] = guild.tournament.nextRound();
await message.channel.send(header, allowMentions)
for (const row of rows) {
await message.channel.send(row, allowMentions);
}
}
if (config.marcia && config.marcia.length > 0 && message.content.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").indexOf('marcia') !== -1) {
const url = config.marcia[Math.floor(Math.random() * (config.marcia.length + 1))];
const emb = new Discord.MessageEmbed().setTitle('Zorca!').setImage(url);
message.channel.send(emb);
}
});