Whenever someone in your Twitch chat uses !spawncreeper, this snippet will spawn a Creeper on top of every player connected to the specified server. You can adapt this code to run any command via Autocode and get the response back!
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let {Rcon} = require('rcon-client'); // we'll use the rcon-client library to connect to Minecraft
// Connect to the Minecraft server via RCON
// Note: this code is for Minecraft Java Edition, not Bedrock (but should work for Paper/Spigot servers as well as Vanilla)
// You'll need to enable RCON for your server + set an RCON password
// See this guide for more info: https://shockbyte.com/billing/knowledgebase/79/How-to-Setup-RCON-on-Your-Minecraft-Server.html
const rcon = await Rcon.connect({
host: process.env.host,
port: process.env.port,
password: process.env.password,
});
// Once we're connected, run multiple commands in the Minecraft console
// You can put any commands that you want here!
let responses = await Promise.all([
rcon.send(`say §b${context.params.event.author.display_name} §espawned a creeper!`), // announce that a creeper has been spawned
rcon.send('execute at @a run summon minecraft:creeper ^ ^ ^'), // spawn a creeper on top of all players on the server. replace @a with a username to spawn on a specific player
]);
// For each response we get back from Minecraft, log it to the Autocode console
// This will let us see Minecraft errors inside Autocode
for (response of responses) {
console.log(response);
}
// Disconnect from the Minecraft server once we're done
rcon.end();