IllusionaryOne

Vote Enhancement

Nov 15th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. function vote(sender, voteText) {
  2. var optionIndex;
  3.  
  4. if (!poll.pollRunning) {
  5. return;
  6. }
  7.  
  8. if (hasKey(poll.voters, sender.toLowerCase())) {
  9. $.say($.whisperPrefix(sender) + $.lang.get('pollsystem.vote.already'));
  10. return;
  11. }
  12.  
  13. voteOptions = voteText.split(',');
  14. if (voteOptions.length > 3) {
  15. $.say($.whisperPrefix(sender) + 'You can only vote on up to three items.');
  16. return;
  17. }
  18.  
  19. // First validate that the user voted properly.
  20. for (voteOption in voteOptions) {
  21. optionIndex = parseInt(voteOptions[voteOption]);
  22. if (isNaN(optionIndex) || optionIndex < 1 || optionIndex > poll.options.length) {
  23. $.say($.whisperPrefix(sender) + $.lang.get('pollsystem.vote.invalid', voteText));
  24. return;
  25. }
  26. }
  27.  
  28. // Now, apply the votes. Assume first vote gets 3 points, second 2, last 1.
  29. votePoints = 3;
  30. poll.voters.push(sender);
  31.  
  32. for (voteOption in voteOptions) {
  33. optionIndex = parseInt(voteOptions[voteOption]);
  34. optionIndex--;
  35. for (var i = 0; i < votePoints; i++) {
  36. poll.votes.push(optionIndex);
  37. $.inidb.incr('pollVotes', poll.options[optionIndex].replace(/\s/, '%space_option%'), 1);
  38. }
  39. votePoints--;
  40. }
  41. };
  42.  
  43. /* This could use more error checking, but for now it works: */
  44. poll open "color" "red, blue, green"
  45. [11-15-2018 @ 17:50:30.803 MST] [MUTED] The Bot IllusionaryBot started a poll (minimum votes: 1): color! Use "!vote [option]" to vote. Options: 1) red 2) blue 3) green
  46. [11-15-2018 @ 17:50:30.805 MST] [MUTED] @IllusionaryBot, Poll started! Use "!poll close" to end the poll manually
  47. vote 2,1,3
  48. poll close
  49. [11-15-2018 @ 17:50:42.671 MST] [MUTED] The poll on "color" has ended! The winner is "blue"!
  50. poll open "color" "red, blue, green"
  51. [11-15-2018 @ 17:50:50.609 MST] [MUTED] The Bot IllusionaryBot started a poll (minimum votes: 1): color! Use "!vote [option]" to vote. Options: 1) red 2) blue 3) green
  52. [11-15-2018 @ 17:50:50.612 MST] [MUTED] @IllusionaryBot, Poll started! Use "!poll close" to end the poll manually
  53. vote 1,2,3
  54. poll close
  55. [11-15-2018 @ 17:50:57.834 MST] [MUTED] The poll on "color" has ended! The winner is "red"!
  56.  
  57. As you can see the item that I first voted for wins. It has the most points.
Advertisement
Add Comment
Please, Sign In to add comment