-
Notifications
You must be signed in to change notification settings - Fork 0
StreamerBot
KK edited this page Feb 24, 2022
·
11 revisions
Using the Plugin in Streamer.bot
For setting up the Minecraft server, please see the Getting Started page.
Make sure that you have Streamer.bot installed before following guide!
- Inside Streamer.bot, navigate to the
Actionstab. - Right click on the
Actionstab and selectAdd. - Create a group for the action called
Minecraft Websocket. - Name the action
MC WS Connected. - Then press
Ok. - Add 2 more actions to the group, called
MC WS DisconnectedandMC WS Message.
- Inside Streamer.bot, navigate to the
Servers/Clientstab. - Click the sub-tab
Websocket Clients. - Inside the window, add a new Host, by right-clicking and pressing
Add. - Inside the popup window, set the
NametoMinecraft WebSocket Server. - Inside the popup window, set the
Hosttows://{localhost}:{8887}. (please replace{localhost}with your host, and{8887}with your port number) - Set the Action Connected to
MC WS Connected. - Set the Action Disconnected to
MC WS Disconnected. - Set the Action Message to
MC WS Message. - Click
Ok.
In this guide, the following will be the flowchart of the actions:
Example workflow:
WS Connect
- Log to file
WS Message
- Authenticate
- Subscribe to events
- Send events to WS Events
WS Disconnect
- Log to file
WS Events
- Get event ran
- Run WS Event-{event ran}
WS Event-{events}
- Convert event data to Json
- Send message in twitch chat
- Make a sub action
File->Write to file, make a new file calledWS_Logs.txt, and set it to append to the file, with the text%date% %time% - WS Connected.
- Make a sub action
File->Write to file, select the file calledWS_Logs.txt, and set it to append to the file, with the text%date% %time% - WS Disconnected.
This is where the magic starts!
- Make a sub action
File->Write to file, select the file calledWS_Logs.txt, and set it to append to the file, with the text%date% %time% - WS Message: %message%. - Make a sub action
C#->Run C# code- inside the C# window, add the following inside
Execute(): - List of events
- inside the C# window, add the following inside
string auth = ""; // Add your authentication token here
string[] listeners = { // Add the events you want to listen to here
"PlayerDeathEvent",
};
string msg = args["message"].ToString();
if (msg.StartsWith("Authentication needed")) {
// Send authentication message
// Set connection to the 0-based index of your Minecraft websocket client.
CPH.WebsocketSend("Bearer " + auth, connection: 0);
return true;
}
if (msg.StartsWith("Authentication not required") || msg.StartsWith("Successfully Authenticated")) {
// Subscribe to events
string listenString = "Listen " + String.Join("\nListen ", listeners);
// Set connection to the 0-based index of your Minecraft websocket client.
CPH.WebsocketSend(listenString, connection: 0);
return true;
}
if (msg.StartsWith("Event ")) {
// Event received
CPH.SetArgument("event", msg.Substring("Event ".Length));
CPH.RunAction("MC WS Event Handler");
}
return true;- This will handle all messages between the server and the client.
- Make a sub action
C#->Run C# code- inside the C# window, add the following inside
Execute():
- inside the C# window, add the following inside
string ev = args["event"].ToString();
string evRun = ev.Split(' ')[0];
string unParsedJson = ev.Substring(evRun.Length);
CPH.SetArgument("event-json", unParsedJson);
CPH.RunAction("MC WS Event-" + evRun);
return true;- This will forward the event to the correct action.
- Replace
{event ran}with the event name you want to handle. - Handle the event
- Example with PlayerDeathEvent: (making the action name
MC WS Event-PlayerDeathEvent)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
public class CPHInline
{
public class Player
{
public string username { get; set; }
public string uuid { get; set; }
}
public class Location
{
public string world { get; set; }
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
}
public class Death
{
public Player player { get; set; }
public string deathMessage { get; set; }
public Location location { get; set; }
}
public bool Execute()
{
Death death = JsonConvert.DeserializeObject<Death>(args["event-json"].ToString());
Player player = death.player;
Location location = death.location;
CPH.SendMessage(death.deathMessage);
CPH.SendMessage(player.username + " died in " + location.world + ", at " + location.x + ", " + location.y + ", " + location.z);
return true;
}
}- Inside Streamer.bot, navigate to the
Actionstab. - Make an action called
Forward Twitch Chat to Minecraft Chat. - Navigate to the
Settings->Eventstab. - Set
Chat MessagetoForward Twitch Chat to Minecraft Chat. - Go back to the
Forward Twitch Chat to Minecraft Chataction. - Make a sub action
C#->Run C# code- inside the C# window, add the following inside
Execute():
- inside the C# window, add the following inside
string msg = args["message"].ToString();
// Make sure to remove bad characters from the message, if you don't, users can break out of tellraw, and other commands
msg = msg.Replace("\\", "");
msg = msg.Replace("\"", "");
string user = args["user"].ToString();
// Set connection to the 0-based index of your Minecraft websocket client.
CPH.WebsocketSend("Command say ["+user+"] " + msg, connection: 0);
return true;- This will forward the message to the Minecraft server.