Skip to content

Instantly share code, notes, and snippets.

@ErraticFox
Created July 4, 2017 03:10
Show Gist options
  • Save ErraticFox/b2fad1330c9e74b47de309552fba1042 to your computer and use it in GitHub Desktop.
Save ErraticFox/b2fad1330c9e74b47de309552fba1042 to your computer and use it in GitHub Desktop.
add: {
description: 'Add a vote.',
exec: (client, msg, args) => {
const Discord = args.Discord;
const utils = args.utils;
const Fuse = args.Fuse;
if (utils.userVotes(msg, args.poll).length >= 3) {
utils.message(msg, 'error', 'You\'ve used all of your votes! `vote remove` to remove them.');
return;
}
let poll = args.poll;
const collectorOptions = {
time: 30000
};
if (!utils.totalVotes(poll).length) {
// happens if first vote
utils.message(msg, null, 'You\'re the first to vote! Type your vote now.', poll.title);
const collector = new Discord.MessageCollector(msg.channel, collected => collected.author.id === msg.author.id, collectorOptions);
collector.on('collect', collect => {
let reg = /\w/;
if (reg.test(collect.cleanContent) && collect.cleanContent.length > 1) {
addvote(msg, poll, collect.cleanContent);
utils.message(msg, 'success', `Your vote ${collect.cleanContent} has been added!`);
utils.writePoll('write', poll);
} else {
utils.message(msg, 'error', 'Must contain one letter and more than one character.');
}
collector.stop();
});
} else {
// after first vote
utils.message(msg, null, voteList(poll), poll.title);
const collector = new Discord.MessageCollector(msg.channel, collected => collected.author.id === msg.author.id, collectorOptions);
collector.on('collect', collect => {
collector.stop();
let reg = /^\d+$/;
if (reg.test(collect.cleanContent) && utils.totalVotes(poll).length >= collect.cleanContent) {
//selection vote
let index = parseInt(collect.cleanContent) - 1;
let title = utils.totalVotes(poll)[index];
if (userVoteExists(msg, poll, title)) {
utils.message(msg, 'error', `You've already voted for ${title}!`);
} else {
addvote(msg, poll, title);
utils.message(msg, 'success', `Your vote ${title} has been added!`);
utils.writePoll('write', poll);
}
} else {
//new vote
let reg = /\w/;
if (reg.test(collect.cleanContent) && collect.cleanContent.length > 1) {
let title = collect.cleanContent;
if (!userVoteExists(msg, poll, title)) {
if (voteExists(poll, title)) {
addvote(msg, poll, title);
utils.message(msg, 'success', `Your vote ${title} has been added!`);
utils.writePoll('write', poll);
} else {
const options = {
shouldSort: true,
threshold: 0.30,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [
'name'
]
};
const fuse = new Fuse(fuseList(msg, poll), options);
const results = fuse.search(title);
if (!results) {
addvote(msg, poll, title);
utils.message(msg, 'success', `Your vote ${title} has been added!`);
utils.writePoll('write', poll);
} else {
utils.message(msg, null, similar(results), poll.title);
const reply = new Discord.MessageCollector(msg.channel, collected => collected.author.id === msg.author.id, collectorOptions);
reply.on('collect', replied => {
let reg = /^\d+$/;
if (reg.test(replied.cleanContent) && results.length >= replied.cleanContent) {
//selection vote
reply.stop();
let index = parseInt(replied.cleanContent) - 1;
let newVote = results[index].name;
addvote(msg, poll, newVote);
utils.message(msg, 'success', `Your vote ${newVote} has been added!`);
utils.writePoll('write', poll);
} else if (reply.cleanContent === 'no') {
reply.stop();
addvote(msg, poll, title);
utils.message(msg, 'success', `Your vote ${title} has been added!`);
utils.writePoll('write', poll);
}
});
}
}
} else {
utils.message(msg, 'error', 'You\'ve already voted for that!');
}
} else {
utils.message(msg, 'error', 'Must contain one letter and more than one character.');
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment