forked from funnbot/Nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll.js
More file actions
97 lines (93 loc) · 3.57 KB
/
Copy pathpoll.js
File metadata and controls
97 lines (93 loc) · 3.57 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
let poll = {};
exports.run = (message, bot, send) => {
let prefix = message.guild.prefix
let id = message.channel.id;
if (poll[id]) return send("**:no_entry: | There is already a poll running in this channel.**")
if (!message.args[0]) return send(`**To Create A Poll:**\n\`${prefix}poll <question> |<option1>|<option2>|<opt...\``);
let split = message.args.join(" ").split("|");
if (!split[0]) return send(`*You must split your question and options with \`|\`\n**To Create A Poll:**\n\`${prefix}poll <question> |<option1>|<option2>|<opt...\``);
if (!split[1]) return send(`*You must have two or more options to make a poll*\n**To Create A Poll:**\n\`${prefix}poll <question> |<option1>|<option2>|<opt...\``);
if (!split[2]) return send(`*You must have two or more options to make a poll*\n**To Create A Poll:**\n\`${prefix}poll <question> |<option1>|<option2>|<opt...\``);
poll[id] = {
id,
question: split[0],
auth: message.author.id,
voted: {}
};
split.shift();
let opt = {};
for (i = 0; i < split.length; i++) {
opt[i + 1] = {
text: split[i],
votes: 0
};
}
poll[id].opt = opt;
let ar = [];
let num = 1;
Object.keys(opt).forEach(r => {
ar.push(num + ". " + opt[r].text) + "";
num++
})
let text = "__**" + poll[id].question + "**__\n\n*You can vote with `" + prefix + "vote <optionNum>`*\n\n**" + ar.join("\n") + "**\n\n*The creator of this poll or a server admin can end the poll early by typing `endpoll` in chat. Otherwise it will go for 2 minutes.*"
message.channel.send(text, {
split: true
});
let collect = message.channel.createCollector(m => {
if (m.author.bot) return false;
if (m.content.startsWith(prefix + "vote") || m.content === "endpoll") return true
else return false;
}, {
time: 180000
});
collect.on('collect', (m) => {
let id = m.channel.id;
if (m.content === "endpoll") {
if (poll[id].auth === m.author.id) return collect.stop('early')
else if (m.channel.permissionsFor(m.member).has("MANAGE_CHANNELS")) return collect.stop('early')
else return;
} else {
m.delete()
if (poll[id].voted[m.author.id]) return m.channel.send("**You have already voted.**");
let sp = m.content.split(" ");
if (!sp[1]) return m.channel.send("**Provide the option number you are voting on**");
let tot = Object.keys(poll[id].opt).length;
let num = parseInt(sp[1]) || 0;
if (num < 1 || num > tot) return m.channel.send("**The option number must be between 1 and " + tot + "**");
poll[id].voted[m.author.id] = true;
poll[id].opt[num.toString()].votes++
}
});
collect.on('end', (col, reas) => {
if (reas === "time" && col.size < 1) return message.channel.send("**The poll has ended without any votes.**")
else if (reas === "early" && col.size < 2) return message.channel.send("**The poll has ended without any votes.**")
else done(col.first());
});
}
function done(message) {
let id = message.channel.id;
let opt = [];
let votes = Object.keys(poll[id].opt);
votes = votes.sort((a, b) => {
return poll[id].opt[b].votes - poll[id].opt[a].votes;
})
let num = 1;
let arr = [];
votes.forEach(p => {
let i = poll[id].opt[p];
arr.push("**" + num + ". " + i.text + " = " + i.votes + "**")
num++
});
let text = "*Here are the results:*\n\n__**" + poll[id].question + "**__\n\n" + arr.join("\n");
message.channel.send(text);
return delete poll[id];
}
exports.conf = {
userPerm: [],
botPerm: ["SEND_MESSAGES"],
coolDown: 0,
dm: false,
category: "Utility",
help: "Create a poll",
args: "",
}