|
Joined: Mar 2014
Posts: 214
Fjord artisan
|
OP
Fjord artisan
Joined: Mar 2014
Posts: 214 |
I would like a poll script [kinda like nightbot] where when someone says "!poll start [question]: [answer 1], [answer 2], etc" it will start a poll, and when someone would vote they say "!vote [1/2/3/etc]" and it would add their vote to that number. When a vote needs to be ended someone says "!poll end" and it will give them the results. I would like this to be made of channel based messages (not notices) to fit twitch. Hope someone can do this!
|
|
|
|
Teksura
|
Teksura
|
Heh. I'm actually in the process of doing the same thing. I might as well share what I have since it might help you out even though it's not done and has a couple issues. This is what I have written for it so far.
;************************************************
;********************** POLL ********************
;************************************************
on *:TEXT:!poll *:#: {
if (($nick !isop $chan) && ($$2 != results)) {
msg $chan Only mods may use this feature.
}
else {
; *********
; OPEN POLL
; *********
if ($$2 == open) {
unset %polloptions
unset %pollvotes
unset %hasvoted
set %polloptions $3-
var %numberofoptions $numtok($3-,124)
while ( %numberofoptions > 0 ) {
dec %numberofoptions
set %pollvotes %pollvotes 0
}
var %number $numtok(%polloptions,124)
while (%number > 0) {
if (%number == $numtok(%polloptions,124)) {
var %finalresults %number - $gettok(%polloptions,%number,124) %finalresults
}
else {
var %finalresults %number - $gettok(%polloptions,%number,124) || %finalresults
}
dec %number
}
msg $chan A new poll has been opened: %finalresults
}
; ************
; POLL RESULTS
; ************
elseif ($$2 == results) {
if (%polloptions != $null) {
msg $chan Current Results are: $pollresults
}
else {
msg $chan Results for previous poll are: %oldresults
}
}
; **********
; POLL CLOSE
; **********
elseif ($$2 == close) {
msg $chan $nick has closed the poll! Results are: $pollresults
set %oldresults $pollresults
unset %polloptions
unset %pollvotes
unset %hasvoted
}
}
}
alias pollresults {
var %number $numtok(%polloptions,124)
while (%number > 0) {
if (%number == $numtok(%polloptions,124)) {
var %finalresults $gettok(%polloptions,%number,124) : $gettok(%pollvotes,%number,32) votes %finalresults
}
else {
var %finalresults $gettok(%polloptions,%number,124) $+ : $gettok(%pollvotes,%number,32) votes || %finalresults
}
dec %number
}
return %finalresults
}
on *:TEXT:!vote *:#: {
if ( %polloptions == $null) {
msg $chan There is no open poll to vote on!
}
elseif ($nick isin %hasvoted) {
msg $chan $nick $+ , you have already voted in this poll!
}
elseif ($$2 !isnum 1 - $numtok(%pollvotes,32)) {
msg $chan $$2- is not a valid poll option!
}
else {
set %hasvoted %hasvoted $nick
var %voteupdate $gettok(%pollvotes,$$2,32)
inc %voteupdate
set %pollvotes $puttok(%pollvotes,%voteupdate,$$2,32)
msg $chan $nick has voted for option $$2 $+ : $gettok(%polloptions,$$2,124)
}
}
So, you can get stuff like this [00:46:43] <teksura> !poll open This | That | The other thing [00:46:45] <tekbot_v2> A new poll has been opened: 1 - This || 2 - That || 3 - The other thing [00:46:50] <teksura> !vote 1 [00:46:51] <tekbot_v2> teksura has voted for option 1: This [00:46:55] <teksura> !poll results [00:46:59] <tekbot_v2> Current Results are: This : 1 votes || That : 0 votes || The other thing : 0 votes [00:47:00] <teksura> !vote 2 [00:47:01] <tekbot_v2> teksura, you have already voted in this poll! [00:47:04] <teksura> !poll close [00:47:05] <tekbot_v2> teksura has closed the poll! Results are: This : 1 votes || That : 0 votes || The other thing : 0 votes [00:47:08] <teksura> !poll results [00:47:10] <tekbot_v2> Results for previous poll are: This : 1 votes || That : 0 votes || The other thing : 0 votes
I had to add a function to allow recalling the results of the last already closed poll due to the occasional problem with twitch chat. This protects you by ensuring that you can still get the results if the message doesn't get through. This is still a work in progress for me, but you certainly can use it as it stands. It should work fine IF that is the only channel you intend to use the bot in. The current issue I'm trying to solve though is giving each channel their own unique set of variables so that opening a poll in one channel doesn't wipe the poll in another channel. As written, this script will have pretty disastrous results if 2 different channels attempt to have polls open at the same time. I tried doing something simple like %polloptions $+ $chan in the hopes that I'd be able to use %polloption#channelname as a variable, but that just seems to give me strange results when I try to actually call the variable. It'll generally just look at %polloptions and then just add the channel name to the end of whatever is at %polloptions. Does anyone know how I can resolve that issue?
Last edited by Teksura; 13/05/14 08:46 AM.
|
|
|
|
blessing
|
blessing
|
I tried doing something simple like %polloptions $+ $chan in the hopes that I'd be able to use %polloption#channelname as a variable, but that just seems to give me strange results when I try to actually call the variable. It'll generally just look at %polloptions and then just add the channel name to the end of whatever is at %polloptions.
Does anyone know how I can resolve that issue?
You can use $(text, N) identifier. To understand this properly, you might want to try this to see how $(text, N) works. //var %poll $+ $me hello world | echo -agt N = 0 is $($+(%,poll,$me), 0) | echo -agt N = 1 is $($+(%,poll,$me), 1) | echo -agt N = 2 is $($+(%,poll,$me), 2) $(var,0) will return plain text. $(var,1) will return var itself. $(var,2) will return value of var.
|
|
|
|
Teksura
|
Teksura
|
I tried doing something simple like %polloptions $+ $chan in the hopes that I'd be able to use %polloption#channelname as a variable, but that just seems to give me strange results when I try to actually call the variable. It'll generally just look at %polloptions and then just add the channel name to the end of whatever is at %polloptions.
Does anyone know how I can resolve that issue?
You can use $(text, N) identifier. To understand this properly, you might want to try this to see how $(text, N) works. //var %poll $+ $me hello world | echo -agt N = 0 is $($+(%,poll,$me), 0) | echo -agt N = 1 is $($+(%,poll,$me), 1) | echo -agt N = 2 is $($+(%,poll,$me), 2) $(var,0) will return plain text. $(var,1) will return var itself. $(var,2) will return value of var. Ahh hah! I knew there had to be something. Thanks, that's exactly what I needed. updated code for the OP is this:
;************************************************
;********************** POLL ********************
;************************************************
on *:TEXT:!poll *:#: {
if (($nick !isop $chan) && ($$2 != results)) {
msg $chan Only mods may use this feature.
}
else {
; *********
; OPEN POLL
; *********
if ($$2 == open) {
unset $($+(%,polloptions,$chan),1)
unset $($+(%,pollvotes,$chan),1)
unset $($+(%,hasvoted,$chan),1)
set $($+(%,polloptions,$chan),1) $3-
var %numberofoptions $numtok($3-,124)
while ( %numberofoptions > 0 ) {
dec %numberofoptions
set $($+(%,pollvotes,$chan),1) $($+(%,pollvotes,$chan),2) 0
}
var %number $numtok($($+(%,polloptions,$chan),2),124)
while (%number > 0) {
if (%number == $numtok($($+(%,polloptions,$chan),2),124)) {
var %finalresults %number - $gettok($($+(%,polloptions,$chan),2),%number,124) %finalresults
}
else {
var %finalresults %number - $gettok($($+(%,polloptions,$chan),2),%number,124) || %finalresults
}
dec %number
}
msg $chan A new poll has been opened: %finalresults
}
; ************
; POLL RESULTS
; ************
elseif ($$2 == results) {
if ($($+(%,polloptions,$chan),2) != $null) {
msg $chan Current Results are: $pollresults
}
else {
msg $chan Results for previous poll are: $($+(%,oldresults,$chan),2)
}
}
; **********
; POLL CLOSE
; **********
elseif ($$2 == close) {
msg $chan $nick has closed the poll! Results are: $pollresults
set $($+(%,oldresults,$chan),1) $pollresults
unset $($+(%,polloptions,$chan),1)
unset $($+(%,pollvotes,$chan),1)
unset $($+(%,hasvoted,$chan),1)
}
}
}
alias pollresults {
var %number $numtok($($+(%,polloptions,$chan),2),124)
while (%number > 0) {
if (%number == $numtok($($+(%,polloptions,$chan),2),124)) {
var %finalresults $gettok($($+(%,polloptions,$chan),2),%number,124) : $gettok($($+(%,pollvotes,$chan),2),%number,32) votes %finalresults
}
else {
var %finalresults $gettok($($+(%,polloptions,$chan),2),%number,124) $+ : $gettok($($+(%,pollvotes,$chan),2),%number,32) votes || %finalresults
}
dec %number
}
return %finalresults
}
on *:TEXT:!vote *:#: {
if ( $($+(%,polloptions,$chan),2) == $null) {
msg $chan There is no open poll to vote on!
}
elseif ($nick isin $($+(%,hasvoted,$chan),2)) {
msg $chan $nick $+ , you have already voted in this poll!
}
elseif ($$2 !isnum 1 - $numtok($($+(%,pollvotes,$chan),2),32)) {
msg $chan $$2- is not a valid poll option!
}
else {
set $($+(%,hasvoted,$chan),1) $($+(%,hasvoted,$chan),2) $nick
var %voteupdate $gettok($($+(%,pollvotes,$chan),2),$$2,32)
inc %voteupdate
set $($+(%,pollvotes,$chan),1) $puttok($($+(%,pollvotes,$chan),2),%voteupdate,$$2,32)
msg $chan $nick has voted for option $$2 $+ : $gettok($($+(%,polloptions,$chan),2),$$2,124)
}
} It now tracks polls for each channel independent of each other (as opposed to the original code which had 1 poll for everything).
|
|
|
|
Joined: Mar 2014
Posts: 214
Fjord artisan
|
OP
Fjord artisan
Joined: Mar 2014
Posts: 214 |
What would i change in the script to where instead of | being the separator of choices to , being the separator?
|
|
|
|
Teksura
|
Teksura
|
Look for the token identifiers. $numtok $gettok $puttok
Token identifiers use ASCII codes to identify what character defines where one token ends and the next begins. In the case of the script above, I chose to use | to define this function for poll options.
The ASCII code for a | is 124. So if you replace that with 44 (which is the ascii code for ,) everywhere you find a token identifier, it should start looking for a , rather than a |.
Try changing it yourself and see where that gets you.
|
|
|
|
|