local ai-chatbot with commands
This is the most advanced example script available so far, demonstrating the full capabilities of the plugin. The AI chatbot runs entirely offline on the server after initially downloading its "brain" data from a remote source. This allows it to respond intelligently without relying on external services after startup.
This script uses an external library to power the chatbot functionality.
📦 Download: rivescript-core-0.9.2.jar
📁 Place it in: plugins/Libs/
If you're unsure how to install libraries for your scripts, [click here].
The script will not work without the required .jar
file.
//!import java.net.URL
//!import java.io.BufferedReader
//!import java.io.InputStreamReader
var URL = java.net.URL
var BufferedReader = java.io.BufferedReader
var InputStreamReader = java.io.InputStreamReader
var BrainUrl = new URL("https://pastebin.com/raw/XvPvaAfT")
var Connection = BrainUrl.openConnection()
Connection.setRequestMethod("GET")
Connection.setRequestProperty("User-Agent", "Mozilla/5.0")
var reader = new BufferedReader(new InputStreamReader(Connection.getInputStream()))
var inputLine
var AiBrain = ""
while ((inputLine = reader.readLine()) != null) {
AiBrain += inputLine + "\n"
}
reader.close()
var RiveScriptLoader = importLib("rivescript-core-0.9.2.jar");
var RiveScriptClass = RiveScriptLoader.loadClass("com.rivescript.RiveScript");
var riveScript = RiveScriptClass.getConstructor().newInstance();
var defaultHistory = `
+ hello
- Hi there!`
function addReply(user, reply, history) {
if (!history) history = "";
return history.trim() + "\n+ " + user + "\n- " + reply + "\n";
}
function getFileName(player) {
return player.getUniqueId() + "_data";
}
function sendMessage(sender, Message) {
sender.sendMessage("[AI-JS]: " + Message);
}
addCommand("ai", {
onCommand: function(sender, args) {
var arguments = toArray(args);
var fileName = getFileName(sender);
if (arguments.length === 0) {
sendMessage(sender, "Usage: /ai chat <message> | /ai clear");
return;
}
var subCommand = arguments[0].toLowerCase();
if (subCommand === "clear") {
DiskApi.setVar(fileName, "ChatHistory", null);
sendMessage(sender, "Chat history cleared.");
return;
}
if (subCommand === "chat") {
if (arguments.length === 1) {
sendMessage(sender, "Please provide a message to chat.");
return;
}
// message is everything after the first arg
var message = arguments.slice(1).join(" ");
var RawchatHistory = DiskApi.getVar(fileName, "ChatHistory", null);
var ChatHistory = RawchatHistory ? JSON.parse(RawchatHistory) : defaultHistory;
riveScript.stream(AiBrain + "\n" + ChatHistory);
riveScript.sortReplies();
var botReply = riveScript.reply("user", message);
if (botReply !== "ERR: No Reply Matched") {
var newHistory = addReply(message, botReply, ChatHistory);
DiskApi.setVar(fileName, "ChatHistory", JSON.stringify(newHistory));
}
sendMessage(sender, botReply);
return
}
sendMessage(sender, "Unknown subcommand. Use /ai chat <message> or /ai clear");
},
onTabComplete: function(sender, args) {
args = toArray(args);
if (args.length === 1) {
return toJavaList(["clear", "chat"]);
} else {
if (args[0].toLowerCase() == "chat") {
return toJavaList(["<message>"]);
}
}
return toJavaList([]);
}
});
// Load player data on join
registerEvent("org.bukkit.event.player.PlayerJoinEvent", {
handleEvent: function(event) {
var player = event.getPlayer();
var fileName = getFileName(player);
DiskApi.loadFile(fileName, true);
}
});
// Save player data on quit
registerEvent("org.bukkit.event.player.PlayerQuitEvent", {
handleEvent: function(event) {
var player = event.getPlayer();
var fileName = getFileName(player);
DiskApi.saveFile(fileName, true);
}
});
Last updated