創造屬於你的 Chat Bots
備註
小提示
目前此頁只寫了Chat Bot API的基礎知識,你掌握這些基礎便可以製作基本的 Chat Bot 了。 如果想了解更多,可以點開這裡 ChatBot.cs 和範例 Examples. 此頁將在未來持續更新<3
Minecraft Console Client 擁有豐富的 C# API 可以用來創造許多擁有複雜功能的Chat Bots (需要建立可行的功能呦),因為這是普通淺層的按鍵腳本難以做到的。
需求
- 掌握基礎的 C# ( C Sharp ) 程式語言 。
- 使用程式編輯器的經驗
如果您不熟悉 C# 程式語言,可以看看下方,我們為您準備了一些簡單的課程,讓您一邊構思自己想寫的功能,一邊補足在寫Chat Bot 時必要的知識,祝您在學習 C# 程式的路上順利<3
速成課程 :
更多詳細教學 :
- Learn C# Youtube Playlist by Microsoft
- Getting started with C# (An index of tutorials and the documentation) by Microsoft
快速入門
快速入門是建立在您有C#的知識上🙏
小提示
在這裡,我們將“聊天機器人”和“腳本”兩個術語,進行互換
創建一個空白的資料夾和一個名為 ExampleChatBot.cs
的檔案,把兩者都放在跟MCC啟動器同一個位置的資料夾。
可以貼上以下的範例程式碼在 ExampleChatBot.cs 中
//MCCScript 1.0
MCC.LoadBot(new ExampleChatBot());
//MCCScript Extensions
// The code and comments above are defining a "Script Metadata" section
// Every single chat bot (script) must be a class which extends the ChatBot class.
// Your class must be instantiates in the "Script Metadata" section and passed to MCC.LoadBot function.
class ExampleChatBot : ChatBot
{
// This method will be called when the script has been initialized for the first time, it's called only once
// Here you can initialize variables, eg. Dictionaries. 等...
public override void Initialize()
{
LogToConsole("An example Chat Bot has been initialized!");
}
// This is a function that will be run when we get a chat message from a server
// In this example it just detects the type of the message and prints it out
public override void GetText(string text)
{
string message = "";
string username = "";
text = GetVerbatim(text);
if (IsPrivateMessage(text, ref message, ref username))
{
LogToConsole(username + " has sent you a private message: " + message);
}
else if (IsChatMessage(text, ref message, ref username))
{
LogToConsole(username + " has said: " + message);
}
}
}
啟動MCC,連接到伺服器並輸入此指令: /script ExampleChatBot.cs
.
如果一切正常順利,您將將可以看到: [Example Chat Bot] An example Chat Bot has been initialised!
的訊息顯示在您的指令窗中。
Chat Bots 的內部結構
Chat Bot (Script)的程式結構是您要遵守的:
<script metadata>
<chat bot class>
Script Metadata is a section with a custom format that mixes in C# with our format using comments.
Every single Chat Bot (Script) must have this section at the beginning in order to work.
Script 的位元組數據格式
//MCCScript 1.0
marks the beginning of the Script Metadata section, this must always be on the first line or the Chat Bot (Script) will not load and will throw an error.
//MCCScript Extensions
marks the end of the Script Metadata section, this must be defined before a Chat Bot (Script) class.
In order for your Chat Bot (Script) to properly load in-between the //MCCScript 1.0
and the //MCCScript Extensions
lines you must instantiate your Chat Bot (Script) class and pass it to the MCC.LoadBot
function.
Example code:
MCC.LoadBot(new YourChatBotClassNameHere());
Script Metadata section allows for including C# packages and libraries with: //using <namespace>
and /dll <dll name>
.
小提示
Avoid adding whitespace between //
and keywords
By the default the following packages are loaded:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
using MinecraftClient;
using MinecraftClient.Mapping;
using MinecraftClient.Inventory;
例如:
//using System.Collections.Immutable
//dll MyDll.dll
這些是範例
//MCCScript 1.0
//using System.Collections.Immutable
//dll MyDll.dll
MCC.LoadBot(new ExampleChatBot());
//MCCScript Extensions
Chat Bot 的Class文件
After the end of the Script Metadata section, you basically can define any number of classes you like, the only limitation is that the main class of your Chat Bot (Script) must extend ChatBot
class.
There are no required methods, everything is optional.
When the Chat Bot (Script) has been initialized for the first time the Initialize
method will be called.
In it you can initialize variables, eg. Dictionaries, etc..
小提示
For allocating resources like a database connection, we recommend allocating them in AfterGameJoined
and freeing them in OnDisconnect
範例
You can find a lot of examples in our Git Hub Repository at ChatBots and config.
C# 的應用程式介面
As of the time of writing, the C# API has been changed in forks that are yet to be merged, so for now you can use the ChatBot.cs for reference.
Each method is well documented with standard C# documentation comments.
In the future we will make a script to auto-generate this section based on the documentation in the code.