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
- Streamer.bot
- Visual Studio Code
- Microsoft account
Installation
-
Install VS Code
-
Launch VS Code
-
Open Extensions
- Keyboard Shortcut:
Ctrl+Shift+X
- Menu Bar:
View > Extensions
- Keyboard Shortcut:
-
Install
IntelliCode for C# Dev Kit
-
This will also install the following Extension dependencies:
- C#
- C# Dev Kit
- .NET Install Tool
Sign into Visual Studio Account
-
Open Command Palette with
- Keyboard Shortcut:
Ctrl+Shift+P
- Menu Bar:
View > Command Palette
- Keyboard Shortcut:
-
Type
.NET: Sign into Visual Studio account
and select it -
Select
Allow
and it will open your web browser for you to log in with your Microsoft Account -
Proceed to log in with Microsoft Account
Create new .NET Project
-
Command Palette
- Keyboard Shortcut:
Ctrl+Shift+P
- Menu Bar:
View > Command Palette
- Keyboard Shortcut:
-
Type
.NET: New Project
and select it -
Select
Console App
-
Select a folder to save the new project folder into
-
Write a name for the project and hit enter
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.
-
Press enter or select
Create Project
Configure new project
-
Open the Explorer
- Keyboard Shortcut:
Ctrl+Shift+E
- Menu Bar:
View > Explorer
- Keyboard Shortcut:
-
Navigate inside the open the
.csproj
file -
Replace with this template in the code block below
-
Save and close the
.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>
- 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
- Open the Explorer
- Keyboard Shortcut:
Ctrl+Shift+E
- Menu Bar:
View > Explorer
- Keyboard Shortcut:
- Navigate inside the open the
.cs
file - Replace your cs file with this 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;
}
}
- 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
afterpublic 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
- This uses the
-
Open
Problems
- Keyboard Shortcut:
Ctrl+Shift+M
- Menu bar:
View > Problems
- Keyboard Shortcut:
-
The
Problems
view will show you if there will be any compile errors- Look for any red error icons
-
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