Guide

C# Code Actions

Take full control of Streamer.bot with custom code actions.

Introduction

When you add a new Execute C# Code sub-action, you will be greeted with the following code snippet:

Execute C# Code Action
using System;

public class CPHInline
{
    public bool Execute()
    {
        // your main code goes here

        return true;
    }
}

For every execute code sub-action, this is the minimum that must be present for it to function.

You can return false to stop execution of the current action, including any subsequent sub-actions.

Within your Execute() function, you have access to the CPH class, allowing you to make calls to internal Streamer.bot methods.

Explore all available C# methods at API References > C# Methods
CPH refers to the original development name of Streamer.bot, ChannelPointsHandler

Arguments

C# actions have access to a Dictionary<string, object> propertry named args

This property contains all variables from the current argument stack.

Accessing

It is best practice fetch args with CPH.TryGetArg, added in Streamer.bot v0.2.3.

If you attempt to access an argument by calling it directly via args["argName"] and it does not exist, a KeyNotFound exception will be thrown.

You can also check if a key exists with the general C# methods ContainsKey or TryGetValue.

// NOTE: CPH.TryGetArg requires Streamer.bot 0.2.3 or later

// If %rawInput% exists, store it in rawInput
CPH.TryGetArg("rawInput", out string rawInput);

// Log the result
CPH.LogInfo($"Variable rawInput: {rawInput}");

Special

Special arguments eventSource and __source must be accessed with their corresponding methods GetSource and GetEventType:
EventSource source = CPH.GetSource(); // eventSource in args

Lifecycle Methods

In addition to the Execute() method, you can also implement the lifecycle methods outlined below.

All lifecycle methods are optional

Init()

Executed when your code is first compiled, useful if you need to perform any initialization steps.

public void Init()
{
    // place your init code here
}

Dispose()

Executed on destroy, useful when you need to perform some cleanup.

public void Dispose()
{
    // place your dispose code here
}

Resources

Tools

Streamer.bot C# Snippets

Visual Studio Code plugin by Ik1497

Video Tutorials

C# Crash Course for Streamer.bot

by nutty

Streamer.bot C# Shorts

by TerrierDarts