Twitch Advanced Auto-Host for mIRC

By Blasman on Nov 29, 2015

UPDATE (Feb 18, 2017) ::: Just posting another update here to remind people that this script is OLD and outdated. I have created a MUCH better version of an AutoHost script that will continue to be updated at https://github.com/Blasman/mIRC-Twitch-Scripts/wiki/Script-Documentation#advanced-autohost-version-2 . PLEASE use that version instead!

UPDATE (Jan 5, 2016) ::: All updates and other scripts will be added to my GitHub instead. As it just makes more sense that way. https://github.com/Blasman/mIRC-Twitch-Scripts

UPDATE (Dec 7, 2015) ::: Added !nexthost and !settier commands

This is my current Twitch Auto-Host script for mIRC. I got frustrated with the lack of options and configurability of other auto-hosting methods, so I decided to create my own. I only recently started getting into scripting about two weeks ago and I have spent a lot of time learning by creating this script. I am open to suggestions of ways to improve upon this script as well. It is the script that I have been using for only a week or two at the moment, but it has been functioning perfectly so far with about 70 channels in my autohost list. I have put details about the script in various comments within the script itself. I hope someone finds this useful. :)

You need JSON for mIRC in order to use this script. Get it here: http://hawkee.com/snippet/10194/

It is recommended that you use mTwitch for this script to make things easier and to simply have a better mIRC Twitch bot. Get mTwitch here: https://github.com/SReject/mTwitch

If you choose not to use mTwitch, then you will have to perform the following two steps:

Edit the script so that the !autohost, !nexthost, and !settier commands can be used in the channel instead of a whisper.

In mIRC, go to Tools > Options > Connect > Options > Enable "Perform on Connect" and select the Twitch server. Add the following lines in the dialog box:
/CAP REQ :twitch.tv/membership
/CAP REQ :twitch.tv/commands
/CAP REQ :twitch.tv/tags

/*
****************************************************************************
************** Twitch Multi-Tier Advanced Auto-Hosting Script **************
****************************************************************************

** This script is a three tier based auto-hosting script for twitch.tv.
** To use it, you must create an autohost.txt file in your mIRC directory.
** Put all of the channels that you want to auto-host on either line 1, 2, or 3 of autohost.txt.
** All channels on line 1 will be "Tier 1." All channels on line 2 will be "Tier 2."
** All channels on line 3 will be "Tier 3." Seperate channel names with a space.
**
** When the autohost alias is ran, it will search each tier in the order that channels are listed.
** If a Tier 1 channel is still live and being hosted, the script will simply end.
** If a Tier 2 channel is still live and being hosted, the script will look for a Tier 1 channel
** to host if the amount of time passed since the host began is greater than %t2.wait.
** If a Tier 3 channel is still live and being hosted, the script will look for a Tier 1 or 2
** channel to host if the amount of time passed since the host began is greater than %t3.wait.
**
** Set the variables below that are needed for the script.
** Change %mychan to your own channel name (keep the # symbol).
** Change %t2.wait to the amount of time (in seconds) that you want the autohost script to wait
** before trying to host a Tier 1 channel if a Tier 2 channel is still being hosted.
** Change %t3.wait to the amount of time (in seconds) that you want the autohost script to wait
** before trying to host a Tier 1 or Tier 2 channel if a Tier 3 channel is being hosted.
** Change %rh.wait to the amount of time (in seconds) that you want the autohost script to wait
** before being allowed to host the same channel again.
** Change %twitch_id to your Twitch ID number.  It can be found at
** http://api.twitch.tv/kraken/channels/YOUR_CHANNEL_NAME and it is the number after "_id".
**
** This script needs the JSON for mIRC script found here:  http://hawkee.com/snippet/10194/
**
*/

ON *:LOAD: {
  SET %mychan #my_twitch_channel
  SET %t2.wait 3600
  SET %t3.wait 1800
  SET %rh.wait 28800
  SET %twitch_id 00000000
}

ON *:UNLOAD: {
  UNSET %mychan
  UNSET %t2.wait
  UNSET %t3.wait
  UNSET %rh.wait
  UNSET %twitch_id
}

ON *:EXIT: {
  UNSET %Autohost
}

; ** This section looks for ANY time that you host a channel (not just auto-host) and displays a
; ** message in your channel that you are currently hosting another channel.
; ** If you are hosting for more than 1 active viewer, it will also display that in the message.
; ** This also sets the %current.host variable needed for the rest of the script.

RAW *:*: {
  IF $nick == tmi.twitch.tv {
    IF (HOSTTARGET isin $rawmsg) && (%mychan isin $rawmsg) {
      tokenize 32 $rawmsg
      IF ($chr(45) !isin $4) {
        SET %current.host $twitch_name($remove($4, :))
        IF (%current.host != $null) && ($5 isnum 2-) {
          MSG %mychan We are now hosting %current.host for $5 active viewers!  Go visit them at twitch.tv/ $+ %current.host and say hello!
        }
        IF (%current.host != $null) && ($5 isnum 0-1) {
          MSG %mychan We are now hosting %current.host $+ !  Go visit them at twitch.tv/ $+ %current.host and say hello!
        }
      }
    }
  }
}

; ** This section will immediately run the autohost script as soon as the current host
; ** goes offline and auto-hosting is currently enabled.

RAW *:*: {
  IF (%Autohost == On) {
    IF (HOSTTARGET isin $rawmsg) && (%mychan isin $rawmsg) && ($chr(45) isin $rawmsg) && ($nick == tmi.twitch.tv) {
      IF (!%ah.run) {
        timer.[AUTOHOST] 0 300 autohost
        autohost
      }
    }
  }
}

; ** This is the auto-host command.  When a mod on your channel sends a whisper to your bot with
; ** "!autohost on" or "!autohost off" it will enable or disable autohosting.  For whispers to work,
; ** you will need something like what is found here: https://github.com/SReject/mTwitch
; ** Otherwise, you can simply edit the command so that mods may type !autohost in your channel.
; **
; ** If a channel is currently being hosted when the "!autohost on" command is ran, it will add that
; ** channel as a temporary "Tier 1" channel, regardless of if it is in the autohost.txt file or not.
; ** This is a personal preference, as I generally don't want the autohost to start looking for other
; ** channels until the channel that I was just visiting has gone offline.  Modify it to your preference.
; **
; ** "!autohost on" sets a timer to run the autohost script every five minutes.

ON *:TEXT:!autohost &:?: {
  IF ($nick isop %mychan) {
    IF ($2 == on) {
      IF (%Autohost == On ) { MSG $nick Auto-host is already on! }
      IF (!%AutoHost) {
        UNSET %current.host
        IF ($getcurrenthost == true) {
          SET %ah.tier 1
          SET %ah.uptime $ctime
        }
        SET %AutoHost On
        timer.[AUTOHOST] 0 300 autohost
        MSG $nick Auto-Host is now on!
        IF (!%current.host) { autohost }
      }
    }
    IF ($2 == off) {
      IF (!%AutoHost) { MSG $nick Auto-host was not on! }
      IF (%Autohost == On ) {
        MSG $nick Auto-Host is now disabled!
        timer.[AUTOHOST] off
        UNSET %AutoHost
      }
    }
  }
}

; ** !nexthost will unhost the current host and force a search for a new host.

ON *:TEXT:!nexthost:?: {
  IF ($nick isop %mychan) {
    IF (%Autohost == On) {
      IF (!%ah.run) {
        timer.[AUTOHOST] off
        UNSET %current.host
        MSG %mychan .unhost
        MSG $nick The current host has been skipped!  Now searching for another host!
        timer.[AUTOHOST] 0 300 autohost
      }
      ELSEIF (%ah.run) {
        MSG $nick Auto-Host is currently running!  Please try again in a few seconds!
      }
    }
    IF (%Autohost == Off) {
      MSG $nick Auto-Host is not on!
    }
  }
}

; ** !settier # will force a temporary tier for the current host.

ON *:TEXT:!settier &:?: {
  IF ($nick isop %mychan) && ($2 isnum 1-3) {
    IF (!%ah.run) {
      SET %ah.tier $2
      MSG $nick The current host has been temporarily set as a Tier $2 host!
    }
    ELSEIF (%ah.run) {
      MSG $nick Auto-Host is currently running!  Please try again in a few seconds!
    }
  }
}

; ****** This is the main autohost script.  ******

alias autohost {

  SET %ah.run True

  ; ** two ways to check if the current host is live, as Twitch is prone to API
  ; ** downtime and other various malfunctions quite often.

  IF (($livecheck(%current.host) == true) || ($getcurrenthost == true)) { VAR %still.live = True }

  IF (%still.live) {
    IF (%ah.tier == 1) { UNSET %ah.run | halt }
    IF (%ah.tier == 2) && ($calc($ctime - %ah.uptime) < %t2.wait) { UNSET %ah.run | halt }
    IF (%ah.tier == 3) && ($calc($ctime - %ah.uptime) < %t3.wait) { UNSET %ah.run | halt }
  }

  VAR %ahcount = 1
  WHILE ($wildtok($read(autohost.txt, n, 1), *, %ahcount, 32) != $null) {
    VAR %ahn = $wildtok($read(autohost.txt, n, 1), *, %ahcount, 32)
    IF ($livecheck(%ahn) == true) && ($rehostcheck(%ahn) != true) && (%livechannel != %current.host) {
      SET %ah.tier 1
      autohost2
      halt
    }
    INC %ahcount
  }

  IF (%still.live) && (%ah.tier == 2) { UNSET %ah.run | halt }
  VAR %ahcount = 1
  WHILE ($wildtok($read(autohost.txt, n, 2), *, %ahcount, 32) != $null) {
    VAR %ahn = $wildtok($read(autohost.txt, n, 2), *, %ahcount, 32)
    IF ($livecheck(%ahn) == true) && ($rehostcheck(%ahn) != true) && (%livechannel != %current.host) {
      SET %ah.tier 2
      autohost2
      halt
    }
    INC %ahcount
  }

  IF (%still.live) && ((%ah.tier == 2) || (%ah.tier == 3)) { UNSET %ah.run | halt }
  VAR %ahcount = 1
  WHILE ($wildtok($read(autohost.txt, n, 3), *, %ahcount, 32) != $null) {
    VAR %ahn = $wildtok($read(autohost.txt, n, 3), *, %ahcount, 32)
    IF ($livecheck(%ahn) == true) && ($rehostcheck(%ahn) != true) && (%livechannel != %current.host) {
      SET %ah.tier 3
      autohost2
      halt
    }
    INC %ahcount
  }
  UNSET %ah.run
}

alias autohost2 {

  MSG %mychan .host %livechannel
  SET %ah.uptime $ctime
  INC %no.rehost
  WRITE -l $+ %no.rehost norehost.txt %livechannel $ctime
  IF (%no.rehost == 20) %no.rehost = 0
  UNSET %livechannel
  UNSET %ah.run
}

; ****** These are the various aliases needed for the script to function.  ******

alias rehostcheck {

  VAR %rhcount = 1
  IF ($exists(norehost.txt)) {
    WHILE ($read(norehost.txt, %rhcount) != $null) {
      VAR %rhnick = $wildtok($read(norehost.txt, %rhcount), *, 1, 32)
      VAR %rhtime = $wildtok($read(norehost.txt, %rhcount), *, 2, 32)
      IF ($calc($ctime - %rhtime) < %rh.wait) && (%rhnick == $1) { return true }
      INC %rhcount
    }
  }
}

alias livecheck {

  IF (%tu == 1000) %tu = 0
  INC %tu
  JSONOpen -ud live $+ %tu https://api.twitch.tv/kraken/streams/ $+ $1 $+ ?nocache= $+ $ticks
  IF ( $json(live $+ %tu $+ ,stream) != $null ) {
    SET %livechannel $1
    return true
  }
  JSONClose live $+ %tu
}

alias getcurrenthost {

  JSONOpen -ud currenthost http://tmi.twitch.tv/hosts?include_logins=1&host= $+ %twitch_id
  IF ( $json(currenthost, hosts, 0, target_login) != $null ) {
    SET %current.host $json(currenthost, hosts, 0, target_login)
    return true
  }
  JSONClose currenthost
}

alias twitch_name {

  IF (%tn == 1000) %tn = 0
  INC %tn
  JSONOpen -ud twitch_name $+ %tn https://api.twitch.tv/kraken/channels/ $+ $1
  return $json(twitch_name $+ %tn $+ , display_name)
  JSONClose twitch_name $+ %tn
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.