A prefix command to spawn a creeper on each player connected to a Minecraft server. You can modify this command to spawn any mob or run any command!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// This snippet spawns a creeper onto every player connected to your server using /summon
// ▶️ This snippet uses one request every time someone sends a message starting with !creeper
// We put this code inside a try/catch block so if connecting to the Minecraft server fails we can show an error
try {
// Send two commands to the Minecraft server
// The first command puts a message in chat saying who spawned the creepers
// The second spawns a creeper on each player
let result = await lib.minecraft.servers['@0.0.1'].java.sendCommands({
commands: [
`say §b${context.params.event.author.username}§e spawned a creeper!`,
`execute at @a run summon minecraft:creeper ^ ^ ^`, // Want to spawn in a different mob? Change this command!
],
});
// Tell the user in Twitch that their creepers were spawned
await lib.twitch.chat['@1.0.0'].messages.create({
channel_id: `${context.params.event.channel.id}`,
content: `Hissss... BOOM! Creepers were spawned on every player by ${context.params.event.author.username}!`,
});
// The code inside our catch() block will only run if there's an error
} catch (e) {
// If there is an error, show it to the command user
await lib.twitch.chat['@1.0.0'].messages.create({
channel_id: `${context.params.event.channel.id}`,
content: `Uh oh! We couldn't spawn a Creeper!`,
});
console.error(`The Minecraft server returned the following error:\n${e}`);
}