Skip to content

Basic Structure

AnkhHeart edited this page Apr 30, 2018 · 11 revisions

1. Importing your libraries

This is where you will import any necessary libraries that you require for your Python Script.

#---------------------------
#   Import Libraries
#---------------------------
import os
import sys
import json
sys.path.append(os.path.join(os.path.dirname(__file__), "lib")) #point at lib folder for classes / references

import clr
clr.AddReference("IronPython.SQLite.dll")
clr.AddReference("IronPython.Modules.dll")

#   Import your Settings class
from Settings_Module import MySettings

2. Include Script Information

These variables are required since they will be used to Display Errors in the PythonScriptErrorlog.txt located in the Logs folder / Scripts Tab under settings in case a script is not behaving properly. This is required for both StreamlabsSystems & StreamlabsParameters.

#---------------------------
#   [Required] Script Information
#---------------------------
ScriptName = "Template Script"
Website = "https://www.streamlabs.com"
Description = "This is a basic boilerplate script"
Creator = "AnkhHeart"
Version = "1.0.0.0"

3. Define Global Variables

Declare your Global Variables next. You will be able to use these within the Init, Tick and Execute functions. Once you want to edit a Global Variable you will need to use ex: global m_Response m_Response = "This is the replacement"

#---------------------------
#   Define Global Variables
#---------------------------
global SettingsFile
SettingsFile = ""
global ScriptSettings
ScriptSettings = MySettings()

4. Initialize Your Script

Initialize anything that you want here. This function will only be called when the script is loaded.

#---------------------------
#   [Required] Initialize Data (Only called on load)
#---------------------------
def Init():

    #   Create Settings Directory
    directory = os.path.join(os.path.dirname(__file__), "Settings")
    if not os.path.exists(directory):
        os.makedirs(directory)

    #   Load settings
    SettingsFile = os.path.join(os.path.dirname(__file__), "Settings\settings.json")
    ScriptSettings = MySettings(SettingsFile)
    ScriptSettings.Response = "Overwritten pong! ^_^"
    return

5. Execute Based on Incoming Data

The execute function will be called when there is a new chat message to be processed. If there are no new messages then this function will not be called. It also receives data which you can process to create to create your own commands, games, … More info on the data type can be found below.

#---------------------------
#   [Required] Execute Data / Process messages
#---------------------------
def Execute(data):

    #   Check if the propper command is used, the command is not on cooldown and the user has permission to use the command
    if data.IsChatMessage() and data.GetParam(0).lower() == ScriptSettings.Command and not Parent.IsOnCooldown(ScriptName,ScriptSettings.Command) and Parent.HasPermission(data.User,ScriptSettings.Permission,ScriptSettings.Info):
        Parent.SendStreamMessage(ScriptSettings.Response)    # Send your message to chat
        Parent.AddCooldown(ScriptName,ScriptSettings.Command,ScriptSettings.Cooldown)  # Put the command on cooldown
    return

6. Tick

This is the Tick function and will be executed every time the program progresses. As you can see there is no data variable here. So use this function when you don't require data but want to do something while there is no data.

#---------------------------
#   [Required] Tick method (Gets called during every iteration even when there is no incoming data)
#---------------------------
def Tick():
    return

7. (Optional) Parse

The Parse function is used to create your own parameters and extend the built in ones. When dealing with Parsing make sure to always return the parseString but replace your parameter inside of the string. In case the parameter is not present inside of the string just pass the parseString back so other Parameters can continue parsing.

#---------------------------
#   [Optional] Parse method (Allows you to create your own custom $parameters) 
#---------------------------
def Parse(parseString, userid, username, targetid, targetname, message):
    
    if "$myparameter" in parseString:
        return parseString.replace("$myparameter","I am a cat!")
    
    return parseString

8. (Optional) Reload Settings

The ReloadSettings function is an optional function that you can add that will automatically be called once a user clicks on the Save Settings button in the scripts tab. The entire Json object will be passed to the function so you can load that back into your settings.

#---------------------------
#   [Optional] Reload Settings (Called when a user clicks the Save Settings button in the Chatbot UI)
#---------------------------
def ReloadSettings(jsonData):
    # Execute json reloading here
    ScriptSettings.__dict__ = json.loads(jsonData)
    ScriptSettings.Save(SettingsFile)
    return

9. Unload & Cleanup

The Unload function is an optional function that gets called when a script is getting reloaded , unloaded or Streamlabs Chatbot gets closed.

#---------------------------
#   [Optional] Unload (Called when a user reloads their scripts or closes the bot / cleanup stuff)
#---------------------------
def Unload():
    return

10. Script Toggled

The ScriptToggled function is an optional function that gets called when your script is Enabled/Disabled through the Streamlabs Chatbot UI. The state object is just a plain Boolean which either contains True or False.

#---------------------------
#   [Optional] ScriptToggled (Notifies you when a user disables your script or enables it)
#---------------------------
def ScriptToggled(state):
    return

Clone this wiki locally