Skip to main content

Writing C# in Visual Studio Code with linting for Streamer.bot

Description

This tutorial provides a general step-by-step guide on setting up Visual Studio Code (VS Code) for writing C# code for Streamer.bot. By following these instructions, you'll be able to write code with linting, which will help you catch errors early and ensure your code compiles. You still need to copy and paste your code into Streamer.bot's C# compiler.

What is linting?

A lint, or linter is a tool that analyzes your source code to automatically flag programming errors, bugs, stylistic errors, and suspicious constructs. (source)

Pre-requisites

Installation

  1. Install VS Code

  2. Launch VS Code

  3. Open Extensions

    • Keyboard Shortcut: Ctrl+Shift+X
    • Menu Bar: View > Extensions
  4. Install IntelliCode for C# Dev Kit

    image

  5. This will also install the following Extension dependencies:

    • C#
    • C# Dev Kit
    • .NET Install Tool

Sign into Visual Studio Account

  1. Open Command Palette with

    • Keyboard Shortcut: Ctrl+Shift+P
    • Menu Bar: View > Command Palette
  2. Type .NET: Sign into Visual Studio account and select it

    image

  3. Select Allow and it will open your web browser for you to log in with your Microsoft Account

    image

  4. Proceed to log in with Microsoft Account

Create new .NET Project

  1. Command Palette

    • Keyboard Shortcut: Ctrl+Shift+P
    • Menu Bar: View > Command Palette
  2. Type .NET: New Project and select it

    image

  3. Select Console App

    image

  4. Select a folder to save the new project folder into

  5. Write a name for the project and hit enter

    image

tip

You can re-use the same project and have unique .cs files for each of your extensions. If you plan on doing this, you can give your project a more generic name. Just make sure to keep unique names for each public class, otherwise you will see compile errors.

  1. Press enter or select Create Project

    image

Configure new project

  1. Open the Explorer

    • Keyboard Shortcut: Ctrl+Shift+E
    • Menu Bar: View > Explorer
  2. Navigate inside the open the .csproj file

  3. Replace with this template in the code block below

  4. Save and close the .csproj file

.csproj file
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Ignore CS0114 errors -->
<NoWarn>CS0114</NoWarn>
</PropertyGroup>

<ItemGroup>
<!-- Include all DLLs in a specific directory in the build output -->
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\netstandard.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Core.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Net.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Net.Http.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Drawing.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Windows.Forms.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.VisualBasic.dll" />

<!-- Replace following directories with where your Streamer.bot is located -->
<Reference Include="D:\overlays\streamerbot\Streamer.bot.Plugin.Interface.dll" />
<Reference Include="D:\overlays\streamerbot\Streamer.bot.Common.dll" />
<Reference Include="D:\overlays\streamerbot\Streamer.bot.Auth.dll" />
<Reference Include="D:\overlays\streamerbot\Streamer.bot.EmoteHandlers.dll" />
<Reference Include="D:\overlays\streamerbot\NAudio.dll" />
<Reference Include="D:\overlays\streamerbot\NAudio.Core.dll" />
<Reference Include="D:\overlays\streamerbot\Newtonsoft.Json.dll" />
<Reference Include="D:\overlays\streamerbot\Twitch.Common.dll" />

<!-- Example location of custom DLLs -->
<Reference Include="D:\overlays\streamerbot\dlls\NCrontab.dll" />
</ItemGroup>

</Project>
.csproj notes
  • The .csproj file configures the project as a .NET Framework 4.7.2 project
    • Streamer.bot v1.0.0+ will target .NET Framework 4.8.1
  • I've added some basic assembly references that are used frequently
  • You will need to add more per your requirements
  • To point to your own Streamer.bot dlls, replace the directory with your own Streamer.bot directory and Streamer.bot dlls directory

Template for the .cs file

  1. Open the Explorer
    1. Keyboard Shortcut: Ctrl+Shift+E
    2. Menu Bar: View > Explorer
  2. Navigate inside the open the .cs file
  3. Replace your cs file with this template:
.cs Template
using Streamer.bot.Plugin.Interface;
using Streamer.bot.Plugin.Interface.Enums;
using Streamer.bot.Plugin.Interface.Model;
using Streamer.bot.Common.Events;
using System;

public class UniqueClassName : CPHInlineBase
{
public bool Execute()
{
// your main code goes here
return true;
}
}
note
  • This is similar to the default code found in the Execute C# Code subaction
  • The first three lines are required in VS Code, but are used by default in Streamer.bot
  • Adding : CPHInlineBase after public class UniqueClassName seems to be the magic that gets VS Code to recognize the CPH methods

You can now write code with assistance from IntelliCode

  • You can type CPH. and it will automatically give you the available CPH methods
    • This uses the Streamer.bot.Plugin.Interface.dll of your Streamer.bot folder
    • The advantage of this is that you will have a current list of methods available

image

  • Open Problems

    • Keyboard Shortcut: Ctrl+Shift+M
    • Menu bar: View > Problems
  • The Problems view will show you if there will be any compile errors

    • Look for any red error icons

    image

  • Compiler errors will show in red and need to be remedied

Copying code to Streamer.bot

  • The Streamer.bot.Plugin.Interface namespaces are optional as they are automatically included in Streamer.bot's Execute C# Code subaction
  • After copying and pasting your code into the Execute C# Code subaction dialog, remember to change:
public class UniqueClassName : CPHInlineBase

to

public class CPHInline
  • Make sure the code compiles successfully by clicking Compile
  • Click Save and Compile