mIRC Home    About    Download    Register    News    Help

Print Thread
Need help with viewer list random user (Twitch) #266025 09/09/19 09:54 AM
Joined: Dec 2018
Posts: 53
T
TroyL Offline OP
Babel fish
OP Offline
Babel fish
T
Joined: Dec 2018
Posts: 53
Hey everyone! So I need help with a script that randomly selects a user from the viewer list and displays it in chat.
Code
on *:text:*!slap*:#troyl:{
alias randuser {
  VAR %x = 1
  WHILE ($hget(activeusers, %x).item != $null) {
    VAR %nick $v1
    IF (!$1) { IF ((%nick ison %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == other) { IF (((%nick isOn %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) && (%nick != $nick)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == notme) { IF (((%nick isOn %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) && (%nick != %streamer)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == othernotme) { IF (((%nick isOn %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) && (%nick != %streamer) && (%nick != $nick)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == list) { IF ((%nick isOn %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) VAR %activelist %activelist %nick }
    ELSE BREAK
    INC %x
    }
  IF ($1 == list) RETURN %activelist
  ELSE {
    VAR %randuser $gettok(%activelist, $rand(1, $numtok(%activelist, 32)), 32)
    IF (%randuser != $null) RETURN %randuser
    msg $chan slapped %nick
    ELSE RETURN $nick
    }
  }

This is what i have so far, i got the alias from a website. But every time i do the command it says "slapped" it doesnt display a user from my viewer list. I've been trying to put the msg $chan in different places to see if it would work, i just dont know how to use an alias and connect that with a twitch command. Any help with this would be greatly appreciated! Thanks!

Re: Need help with viewer list random user (Twitch) [Re: TroyL] #266026 09/09/19 05:53 PM
Joined: Jan 2004
Posts: 1,214
maroon Offline
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 1,214
For one thing, you should not place an alias inside the ON TEXT handler. An alias is like a subroutine that you can call from multiple places, rather than repeating the code each time you need the result. The randuser alias is expecting $1 to be something like "notme" "other" "list" etc, but instead it's the 1st word of the text message, which I assume is !slap.

You need to move the alias outside the ON TEXT event, like how the flood_counter and repeat_counter aliases I once made for you, then inside the ON TEXT you use $randuser($2) (assuming that LIST OTHER etc is the 2nd word of the text message)

I still can't tell from your event handler what's supposed to be done with the results of $randuser when someone does type the keyword. It "return" the string instead of messaging it to channel.

Your randuser alias makes several assumptions, and will give unexpected results if those guesses are wrong.

It assumes %activetime is defined as a global variable, yet doesn't define it before using it.
It assumes %mychan is defined as a global variable, yet doesn't define it before using it.
It assumes %activelist does not exist as a global variable, so if it does exist, that string is included in the output.
Next to where you initialize %x as 1, you need to initialize %activelist as empty with:
var %activelist

It assumes that a hashtable named activeusers exists, and that it contains a list of items named after a bunch of nicks, and the data attached to those items is some kind of active time value.

The 'slapped' message is supposed to display %nick, which is only altered if there's at least 1 item in that hashtable, so since it's $null, that indicates your hashtable doesn't exist.

Your alias does not clear %nick before defining it, so if the hashtable does not exist, but %nick already exists as a global variable, the displayed message would have shown the same contents of the global variable each time.

Either you didn't paste everything you got from "a website", didn't grab everything you should have, or "a website" needs to give a refund.

Re: Need help with viewer list random user (Twitch) [Re: TroyL] #266027 09/09/19 06:06 PM
Joined: Dec 2011
Posts: 13
J
JohnEricNO Offline
Pikka bird
Offline
Pikka bird
J
Joined: Dec 2011
Posts: 13
If you only want to slap a random nick in the nicklist, use

Code
on *:text:*!slap*:#troyl:{ .msg # slap $nick(#,$rand(1,$nick(#,0))) }

Re: Need help with viewer list random user (Twitch) [Re: TroyL] #266070 15/09/19 08:23 PM
Joined: Oct 2015
Posts: 112
B
Blas Offline
Vogon poet
Offline
Vogon poet
B
Joined: Oct 2015
Posts: 112
I wrote the $randuser script. It's designed to select a random active user in chat, as I prefer to not have lurkers being called out in chat randomly as that is a huge no-no on Twitch. It also relies on other variables that are created from other scripts that I designed (%streamer, %mychan, etc). It looks like you tried to dissect the randuser script and to maroon's point, you made several errors trying to do so. If you don't care if the user has been active or not, than JohnEricNO's one line suggestion sounds like a perfect solution for your needs. Otherwise, it's probably going to take a bit more effort and work on your part to only have your script select active users. smile

I did take some time and had a look back at the last edit of the randuser script, and it shouldn't be too difficult to have your bot only select active users. The default is 900 seconds (15 minutes), which means that a user had to have said something or done an action in the last 15 minutes to be eligible to be chosen randomly. Just /set %activetime to whatever you want the time to be (in seconds), and /set %mychan to your channel name (or just manually edit your channel name in). You can then use something like:

Code
ON *:TEXT:!slap:#troyl: MSG $chan $nick slaps $randuser $+ .


Added lines in the original code to make the randuser script (hopefully) work now without anything else:

Code
ON *:LOAD: {
  IF (!%activetime) SET %activetime 900
  IF (!$hget(activeusers)) HMAKE activeusers
  IF (!%commonbots) SET %commonbots moobot nightbot revlobot vivbot xanbot wizebot streamelements
}

ON *:CONNECT: IF (($server == tmi.twitch.tv) && (!$hget(activeusers))) HMAKE activeusers

alias randuser {
  VAR %x = 1
  WHILE ($hget(activeusers, %x).item != $null) {
    VAR %nick $v1
    IF (!$1) { IF ((%nick ison %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == other) { IF (((%nick ison %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) && (%nick != $nick)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == notme) { IF (((%nick ison %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) && (%nick != %streamer)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == othernotme) { IF (((%nick ison %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) && (%nick != %streamer) && (%nick != $nick)) VAR %activelist %activelist %nick }
    ELSEIF ($1 == list) { IF ((%nick ison %mychan) || ($calc($hget(activeusers, %nick) + 90) >= %activetime)) VAR %activelist %activelist %nick }
    ELSE BREAK
    INC %x
  }
  IF ($1 == list) RETURN %activelist
  ELSE {
    VAR %randuser $gettok(%activelist, $rand(1, $numtok(%activelist, 32)), 32)
    IF (%randuser != $null) RETURN %randuser
    ELSE RETURN $nick
  }
}

ON *:TEXT:*:%mychan:IF (($nick != twitchnotify) && ($nick != $me) && (!$istok(%commonbots,$nick,32))) activeusers
ON *:ACTION:*:%mychan:IF (($nick != twitchnotify) && ($nick != $me) && (!$istok(%commonbots,$nick,32))) activeusers

alias activeusers HADD -mz activeusers $nick %activetime

Last edited by Blas; 15/09/19 08:30 PM. Reason: hit POST by accident