-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da1ade0
commit fbcbb15
Showing
1 changed file
with
132 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,138 @@ | ||
const { Command } = require("discord.js-commando") | ||
const { Command } = require("discord.js-commando"); | ||
|
||
const poll = {}; | ||
|
||
module.exports = class PollCommand extends Command { | ||
constructor(client) { | ||
super(client, { | ||
name: 'poll', | ||
group: 'util', | ||
memberName: 'poll', | ||
description: 'Creates a poll', | ||
examples: ['poll Cats or dogs? | Dogs | Cats'] | ||
}) | ||
} | ||
async run(msg){ | ||
let prefix = this.client.commandPrefix; | ||
let noPrefix = msg.content.slice(prefix.length); | ||
let split = noPrefix.split(" "); | ||
let cmd = split[0]; | ||
split = split.slice(1); | ||
let args = split; | ||
let id = msg.channel.id; | ||
constructor(client) { | ||
super(client, { | ||
name: "poll", | ||
group: "util", | ||
memberName: "poll", | ||
description: "Creates a poll", | ||
examples: ["poll Cats or dogs? | Dogs | Cats"] | ||
}); | ||
} | ||
async run(msg) { | ||
let prefix = this.client.commandPrefix; | ||
let noPrefix = msg.content.slice(prefix.length); | ||
let split = noPrefix.split(" "); | ||
let cmd = split[0]; | ||
split = split.slice(1); | ||
let args = split; | ||
let id = msg.channel.id; | ||
|
||
if(poll[id]) return msg.say(`:no_entry_sign: There is already a poll running in this channel!`) | ||
if(!args[0]) return msg.say(`To create a poll: ${prefix}poll <question> | <option one> | <option two> | <more options>`); | ||
let mSplit = args.join(' ').split("|"); | ||
if(!mSplit[0]) return msg.say(`You must split your poll options using |`); | ||
if(!mSplit[1]) return msg.say(`You must have two or more options to make a poll!`); | ||
if(!mSplit[2]) return msg.say(`You must have two or more options to make a poll!`); | ||
poll[id] = { | ||
id, | ||
question: split[0], | ||
auth: msg.author.id, | ||
voted: {} | ||
} | ||
mSplit.shift(); | ||
let opt = {} | ||
for(let i = 0; i < mSplit.length; i++){ | ||
opt[i + 1] = { | ||
text: mSplit[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.*'; | ||
msg.say(text, { | ||
split: true | ||
}) | ||
let collect = msg.channel.createMessageCollector(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.reply('You have aleady voted!') | ||
let sp = m.content.split(' ') | ||
if(!sp[1]) return m.reply('Provide the option number you are voting for') | ||
let tot = Object.keys(poll[id].opt).length | ||
let num = parseInt(sp[1]) || 0; | ||
|
||
} | ||
}) | ||
if (poll[id]) | ||
return msg.say( | ||
`:no_entry_sign: There is already a poll running in this channel!` | ||
); | ||
if (!args[0]) | ||
return msg.say( | ||
`To create a poll: ${prefix}poll <question> | <option one> | <option two> | <more options>` | ||
); | ||
let mSplit = args.join(" ").split("|"); | ||
if (!mSplit[0]) return msg.say(`You must split your poll options using |`); | ||
if (!mSplit[1]) | ||
return msg.say(`You must have two or more options to make a poll!`); | ||
if (!mSplit[2]) | ||
return msg.say(`You must have two or more options to make a poll!`); | ||
poll[id] = { | ||
id, | ||
question: split[0], | ||
auth: msg.author.id, | ||
voted: {} | ||
}; | ||
mSplit.shift(); | ||
let opt = {}; | ||
for (let i = 0; i < mSplit.length; i++) { | ||
opt[i + 1] = { | ||
text: mSplit[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.*"; | ||
msg.say(text, { | ||
split: true | ||
}); | ||
let collect = msg.channel.createMessageCollector( | ||
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.reply("You have aleady voted!"); | ||
let sp = m.content.split(" "); | ||
if (!sp[1]) | ||
return m.reply("Provide the option number you are voting for"); | ||
let tot = Object.keys(poll[id].opt).length; | ||
let num = parseInt(sp[1]) || 0; | ||
if (num < 1 || num > tot) | ||
return m.reply(`The option number must be between 1 and ${tot}`); | ||
poll[id].voted[m.author.id] = true; | ||
poll[id].opt[num.toString()].votes++; | ||
m.reply(`:white_check_mark: You've successfully voted!`); | ||
} | ||
}); | ||
collect.on("end", (col, res) => { | ||
if (res === "time" && col.size < 1) { | ||
msg.say(`The poll has ended without any votes.`); | ||
delete poll[id]; | ||
} else if (res === "early" && col.size < 2) { | ||
msg.say(`The poll has ended without any votes.`); | ||
delete poll[id]; | ||
} else { | ||
document(col.first()); | ||
} | ||
}); | ||
function done(message) { | ||
let id = msg.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"); | ||
msg.say(text); | ||
return delete poll[id]; | ||
} | ||
} | ||
} | ||
}; |