Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. const VoteKick = module.exports = {
  2. command: '!votekick',
  3. active: {}
  4. }
  5.  
  6. VoteKick.manager = function * (message, command) {
  7. if (!(message.sender in VoteKick.active))
  8. VoteKick.active[message.sender] = {}
  9.  
  10. const parsed = VoteKick.parser(command)
  11. const issuer = yield global.NameCache.get(message.from)
  12. const target = yield global.NameCache.get(parsed.target)
  13.  
  14. if (parsed.vote !== undefined)
  15. VoteKick.voteUser(message.sender, issuer, target, parsed.vote)
  16. else
  17. VoteKick.createVote(message.sender, issuer, target)
  18. }
  19.  
  20. VoteKick.createVote = function (conference, issuer, target) {
  21. if (target.id in VoteKick.active[conference])
  22. return global.VK.api.messages.send({
  23. peer_id: conference,
  24. message: `Голосование за исключение [id${target.id}|${target.first_name} ${target.last_name}] уже началось`
  25. })
  26.  
  27. VoteKick.active[conference][target.id] = {yes: [issuer.id], no: []}
  28. global.VK.api.messages.send({
  29. peer_id: conference,
  30. message: `[id${issuer.id}|${issuer.first_name} ${issuer.last_name}] начал голосование за исключение [id${target.id}|${target.first_name} ${target.last_name}]
  31.  
  32. Проголосуйте с помощью ${VoteKick.command} @id${target.id} yes/no`
  33. })
  34. }
  35.  
  36. VoteKick.voteUser = function (conference, issuer, target, vote = false) {
  37. if (!(target.id in VoteKick.active[conference]))
  38. return global.VK.api.messages.send({
  39. peer_id: conference,
  40. message: `Голосование за исключение [id${target.id}|${target.first_name} ${target.last_name}] не начиналось`
  41. })
  42.  
  43. if (VoteKick.active[conference][target.id]['yes'].includes(issuer.id))
  44. return global.VK.api.messages.send({
  45. peer_id: conference,
  46. message: `Вы уже проголосовали за исключение [id${target.id}|${target.first_name} ${target.last_name}]`
  47. })
  48.  
  49. if (VoteKick.active[conference][target.id]['yes'].includes(issuer.id))
  50. return global.VK.api.messages.send({
  51. peer_id: conference,
  52. message: `Вы уже проголосовали против исключения [id${target.id}|${target.first_name} ${target.last_name}]`
  53. })
  54.  
  55. if (vote) {
  56. VoteKick.active[conference][target.id]['yes'].push(issuer.id)
  57. return global.VK.api.messages.send({
  58. peer_id: conference,
  59. message: `За исключение [id${target.id}|${target.first_name} ${target.last_name}] проголосовало ${VoteKick.active[target.id].yes.length} из 15 человек`
  60. })
  61. } else {
  62. VoteKick.active[conference][target.id]['no'].push(issuer.id)
  63. return global.VK.api.messages.send({
  64. peer_id: conference,
  65. message: `Против исключения [id${target.id}|${target.first_name} ${target.last_name}] проголосовало ${VoteKick.active[target.id].no.length} из 15 человек`
  66. })
  67. }
  68.  
  69. }
  70.  
  71. VoteKick.parser = function (command) {
  72. let toRet = {}
  73. if (command[0] != VoteKick.command)
  74. return false
  75. if (command[1] === undefined)
  76. return false
  77.  
  78. const regexp = command[1].match(/\[id(\d+)\|.+\]/)
  79. if (regexp === null || regexp[1] === undefined)
  80. return false
  81. toRet["target"] = regexp[1]
  82.  
  83. if (command[2] !== undefined) {
  84. if (command[2] == 'yes')
  85. toRet["vote"] = true
  86. if (command[2] == 'no')
  87. toRet["vote"] = false
  88. }
  89.  
  90. return toRet
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement