-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.js
218 lines (186 loc) · 7.38 KB
/
buttons.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const { ActionRowBuilder, Events, ModalBuilder, TextInputBuilder, TextInputStyle, EmbedBuilder, AttachmentBuilder } = require('discord.js');
const fs = require("fs");
module.exports = [
{
name: "seevotes",
admin: true,
execute: async function (bot, interaction, voteId)
{
bot.data.votes = bot.data.votes || {};
bot.data.votes[voteId] = bot.data.votes[voteId] || {};
bot.data.votes[voteId].responses = bot.data.votes[voteId].responses || {};
let textResponse = "VOTE RESPONSES:";
const entries = Object.entries(bot.data.votes[voteId].responses);
entries.forEach(([key, value]) =>
{
if (value && value.reason)
{
textResponse += `\n\n${key}\n${value.username} voted ${value.vote}.\nReason given:\n${value.reason}`;
}
});
let fileBuffer = Buffer.from(textResponse);
const attachment = new AttachmentBuilder(fileBuffer, { name: "results.txt" });
interaction.reply({
files: [attachment],
ephemeral: true
});
}
},
{
name: "endvotes",
admin: true,
execute: async function (bot, interaction, voteId)
{
bot.data.votes = bot.data.votes || {};
bot.data.votes[voteId] = bot.data.votes[voteId] || {};
bot.data.votes[voteId].closed = true;
bot.data.votes[voteId].responses = bot.data.votes[voteId].responses || {};
let textResponse = "VOTE RESPONSES:";
const entries = Object.entries(bot.data.votes[voteId].responses);
let yes = 0;
let no = 0;
entries.forEach(([key, value]) =>
{
textResponse += `\n\n${key}\n${value.username} voted ${value.vote}.\nReason given:\n${value.reason}`;
if (value && value.reason)
{
if (value.vote == "Yes")
{
yes++;
}
else
{
no++;
}
}
});
let total = yes + no;
let fileBuffer = Buffer.from(textResponse);
const attachment = new AttachmentBuilder(fileBuffer, { name: "results.txt" });
let result = "Tied!";
if (yes > no)
{
result = "Yes";
}
if (no > yes)
{
result = "No";
}
await interaction.reply({
content: `## Vote closed!\nThe result is...\n# ${result}!\nYes: ${(yes / total) * 100}% (${yes} votes)\nNo: ${(no / total) * 100}% (${no} votes)`
});
await interaction.followUp({
files: [attachment],
ephemeral: true
});
}
},
{
name: "voteyes",
admin: false,
execute: async function (bot, interaction, voteId)
{
let responseId = "response" + Date.now();
bot.data.votes = bot.data.votes || {};
bot.data.votes[voteId] = bot.data.votes[voteId] || {};
let userVote = UserHasVoted(bot.data.votes[voteId], interaction.user.tag);
let changedVote = false;
let oldText = "";
if (bot.data.votes[voteId].closed)
{
await interaction.reply({ content: 'You can no longer vote on this.', ephemeral: true });
return;
}
if (userVote)
{
responseId = userVote.id;
changedVote = userVote.vote.reason && userVote.vote.vote != "Yes";
oldText = userVote.vote.reason ?? "";
userVote.vote.vote = "Yes";
}
else
{
bot.data.votes[voteId].responses = bot.data.votes[voteId].responses || {};
bot.data.votes[voteId].responses[responseId] = { vote: "Yes", username: interaction.user.tag };
}
const modal = new ModalBuilder()
.setCustomId(voteId + ":" + responseId)
.setTitle(changedVote ? "Changed Vote to \"Yes\"" : "Voted \"Yes\"");
const reasonInput = new TextInputBuilder()
.setCustomId('reasonInput')
.setLabel(changedVote ? "Vote changed to \"Yes\"! Update your reason?" : "Your vote requires reasoning to be valid.")
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('What is the reason for voting "Yes"?')
.setRequired(true)
.setMinLength(50);
if (oldText && oldText.length >= 50)
reasonInput.setValue(oldText);
const firstActionRow = new ActionRowBuilder().addComponents(reasonInput);
modal.addComponents(firstActionRow);
await interaction.showModal(modal);
}
},
{
name: "voteno",
admin: false,
execute: async function (bot, interaction, voteId)
{
let responseId = "response" + Date.now();
bot.data.votes = bot.data.votes || {};
bot.data.votes[voteId] = bot.data.votes[voteId] || {};
let userVote = UserHasVoted(bot.data.votes[voteId], interaction.user.tag);
let changedVote = false;
let oldText = "";
if (bot.data.votes[voteId].closed)
{
await interaction.reply({ content: 'You can no longer vote on this.', ephemeral: true });
return;
}
if (userVote)
{
responseId = userVote.id;
changedVote = userVote.vote.reason && userVote.vote.vote != "No";
oldText = userVote.vote.reason ?? "";
userVote.vote.vote = "No";
}
else
{
bot.data.votes[voteId].responses = bot.data.votes[voteId].responses || {};
bot.data.votes[voteId].responses[responseId] = { vote: "No", username: interaction.user.tag };
}
const modal = new ModalBuilder()
.setCustomId(voteId + ":" + responseId)
.setTitle(changedVote ? "Changed Vote to \"No\"" : "Voted \"No\"");
const reasonInput = new TextInputBuilder()
.setCustomId('reasonInput')
.setLabel(changedVote ? "Vote changed to \"No\"! Update your reason?" : "Your vote requires reasoning to be valid.")
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('What is the reason for voting "No"?')
.setRequired(true)
.setMinLength(50);
if (oldText && oldText.length >= 50)
reasonInput.setValue(oldText);
const firstActionRow = new ActionRowBuilder().addComponents(reasonInput);
modal.addComponents(firstActionRow);
await interaction.showModal(modal);
}
}
];
function UserHasVoted (vote, user)
{
if (!vote.responses)
{
return false;
}
const entries = Object.entries(vote.responses);
let found = false;
entries.forEach(([key, value]) =>
{
if (value.username == user)
{
found = { id: key, vote: value };
return;
}
});
return found;
}