forked from PretendoNetwork/Yamamura
-
Notifications
You must be signed in to change notification settings - Fork 13
/
view-rules.js
93 lines (79 loc) · 2.32 KB
/
view-rules.js
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
const Discord = require('discord.js');
const database = require('../database');
const { button: verifyCompleteButton } = require('./verify-complete');
const viewRulesButton = new Discord.ButtonBuilder();
viewRulesButton.setCustomId('view-rules');
viewRulesButton.setLabel('View Rules');
viewRulesButton.setStyle(Discord.ButtonStyle.Primary);
/**
*
* @param {Discord.ButtonInteraction} interaction
*/
async function viewRulesHandler(interaction) {
const parts = interaction.customId.split('-');
const ruleId = Number(parts[2]) || 0;
const rules = await database.getAllRules(interaction.guildId);
const rule = rules[ruleId];
const nextButton = new Discord.ButtonBuilder();
nextButton.setCustomId(`view-rules-${ruleId + 1}`);
nextButton.setLabel('Next');
nextButton.setStyle('Primary');
nextButton.setEmoji('⏩');
nextButton.setDisabled(false);
const row = new Discord.ActionRowBuilder();
row.addComponents(nextButton);
if (rules.length === 0) {
row.setComponents(verifyCompleteButton);
await interaction.reply({
content: 'No rules are set, press Verify to continue.',
components: [row],
ephemeral: true
});
return;
}
const ruleEmbed = new Discord.EmbedBuilder();
ruleEmbed.setColor(0x9D6FF3);
ruleEmbed.setTitle(`Rule ${ruleId + 1}: ${rule.title}`);
ruleEmbed.setDescription(rule.description);
if (ruleId === 0) {
await interaction.reply({
embeds: [ruleEmbed],
components: [row],
ephemeral: true
});
} else {
await interaction.update({
embeds: [ruleEmbed],
components: [row],
ephemeral: true
});
}
let time = rule.time;
if (time !== 0) {
const timer = setInterval(function countdown() {
nextButton.setLabel(time === 0 ? 'Next' : `Next (${time})`);
nextButton.setDisabled(time === 0 ? false : true);
time -= 1;
row.setComponents(nextButton);
if(time === -1) {
clearInterval(timer);
if (rules[ruleId + 1] === undefined) {
row.setComponents(verifyCompleteButton);
interaction.editReply({components: [row]});
}
}
interaction.editReply({components: [row]});
return countdown;
}(), 1000);
} else {
if (rules[ruleId + 1] === undefined) {
row.setComponents(verifyCompleteButton);
interaction.editReply({components: [row]});
}
}
}
module.exports = {
name: viewRulesButton.data.custom_id,
button: viewRulesButton,
handler: viewRulesHandler
};