-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (167 loc) · 5.55 KB
/
index.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
const { MessageEmbed } = require("discord.js");
const defEmojiList = [
"\u0031\u20E3",
"\u0032\u20E3",
"\u0033\u20E3",
"\u0034\u20E3",
"\u0035\u20E3",
"\u0036\u20E3",
"\u0037\u20E3",
"\u0038\u20E3",
"\u0039\u20E3",
"\uD83D\uDD1F",
];
const voteEmbed = async (
message,
title,
options,
timeout = 30,
lang = "es",
emojiList = defEmojiList.slice(),
forceEndPollEmoji = "\u2705"
) => {
if (!lang || lang === "es") {
if (!message && !message.channel)
return message.reply("no puedo acceder a este canal.");
if (!title) return message.reply("especifica el título de la encuesta.");
if (!options) {
options = ["Sí", "No"];
}
if (options.length < 2)
return message.reply("por favor, proporciona más de dos opciones.");
if (options.length > emojiList.length)
return message.reply(
`por favor, no excedas de ${emojiList.length} opciones.`
);
let text = `✥ Para votar, reacciona usando el emoji correspondiente.\n✥ ${timeout > 0
? "La encuesta terminará en **" + timeout + "** segundos."
: "La encuesta **finalizará manualmente** según el creador de la encuesta lo decida."
}\n✥ El creador de esta encuesta puede finalizar **forzadamente** reaccionando al emoji ${forceEndPollEmoji}\n\n`;
const emojiInfo = {};
for (const option of options) {
const emoji = emojiList.splice(0, 1);
emojiInfo[emoji] = { option: option, votes: 0 };
text += `${emoji} : \`${option}\`\n\n`;
}
const usedEmojis = Object.keys(emojiInfo);
usedEmojis.push(forceEndPollEmoji);
const poll = await message.channel.send(
embedBuilder(title, message.author)
.setDescription(text)
.setColor("7c4e55")
.setThumbnail(message.guild.iconURL())
);
for (const emoji of usedEmojis) await poll.react(emoji);
const reactionCollector = poll.createReactionCollector(
(reaction, user) =>
usedEmojis.includes(reaction.emoji.name) && !user.bot,
timeout === 0 ? {} : { time: timeout * 1000 }
);
const voterInfo = new Map();
reactionCollector.on("collect", (reaction, user) => {
if (usedEmojis.includes(reaction.emoji.name)) {
if (
reaction.emoji.name === forceEndPollEmoji &&
message.author.id === user.id
)
return reactionCollector.stop();
if (
reaction.emoji.name === forceEndPollEmoji &&
message.author.id !== user.id
)
return;
emojiInfo[reaction.emoji.name].votes = reaction.count - 1;
}
});
// reactionCollector.on("dispose", (reaction, user) => {
// if (usedEmojis.includes(reaction.emoji.name)) {
// emojiInfo[reaction.emoji.name].votes -= 1;
// }
// });
reactionCollector.on("end", () => {
text = "*¡La encuesta ha terminado!*\n✥ Los resultados son:\n\n";
for (const emoji in emojiInfo)
text += `• \`${emojiInfo[emoji].option}\` - \`${emojiInfo[emoji].votes} votos\`\n\n`;
poll.delete();
message.channel.send(
embedBuilder(title, message.author).setDescription(text)
);
});
} else if(lang === "en"){
if (!message && !message.channel)
return message.reply("I can't access this channel.");
if (!title) return message.reply("specifies the title of the survey.");
if (!options) {
options = ["Yes", "No"];
}
if (options.length < 2)
return message.reply("please provide more than two options.");
if (options.length > emojiList.length)
return message.reply(
`please do not exceed ${emojiList.length}`
);
let text = `✥ To vote, react using the corresponding emoji.\n✥ ${timeout > 0
? "The survey will end in **" + timeout + "** seconds."
: "The survey will **manually end** as decided by the survey creator."
}\n✥ The creator of this survey can **forcefully** terminate by reacting to the emoji ${forceEndPollEmoji}\n\n`;
const emojiInfo = {};
for (const option of options) {
const emoji = emojiList.splice(0, 1);
emojiInfo[emoji] = { option: option, votes: 0 };
text += `${emoji} : \`${option}\`\n\n`;
}
const usedEmojis = Object.keys(emojiInfo);
usedEmojis.push(forceEndPollEmoji);
const poll = await message.channel.send(
embedBuilder(title, message.author)
.setDescription(text)
.setColor("7c4e55")
.setThumbnail(message.guild.iconURL())
);
for (const emoji of usedEmojis) await poll.react(emoji);
const reactionCollector = poll.createReactionCollector(
(reaction, user) =>
usedEmojis.includes(reaction.emoji.name) && !user.bot,
timeout === 0 ? {} : { time: timeout * 1000 }
);
const voterInfo = new Map();
reactionCollector.on("collect", (reaction, user) => {
if (usedEmojis.includes(reaction.emoji.name)) {
if (
reaction.emoji.name === forceEndPollEmoji &&
message.author.id === user.id
)
return reactionCollector.stop();
if (
reaction.emoji.name === forceEndPollEmoji &&
message.author.id !== user.id
)
return;
emojiInfo[reaction.emoji.name].votes = reaction.count - 1;
}
});
// reactionCollector.on("dispose", (reaction, user) => {
// if (usedEmojis.includes(reaction.emoji.name)) {
// emojiInfo[reaction.emoji.name].votes -= 1;
// }
// });
reactionCollector.on("end", () => {
text = "*The survey is over!*\n✥ The results are:\n\n";
for (const emoji in emojiInfo)
text += `• \`${emojiInfo[emoji].option}\` - \`${emojiInfo[emoji].votes} votes\`\n\n`;
poll.delete();
message.channel.send(
embedBuilder(title, message.author).setDescription(text)
);
});
}
};
const embedBuilder = (title, author) => {
return new MessageEmbed()
.setTitle(`Poll - ${title}`)
.setFooter(
`Survey created by ${author.tag}`,
author.displayAvatarURL()
);
};
module.exports = voteEmbed;