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}#${context.params.event.author.discriminator} §espawned a creeper!`,
`execute at @a run summon minecraft:creeper ^ ^ ^`, // Want to spawn in a different mob? Change this command!
],
});
// Tell the user in Discord that their creepers were spawned
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `Hissss... BOOM!`,
description: `Creepers were spawned on every player! `,
color: 0x20bf6b,
},
],
});
// 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.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `Uh oh! We couldn't spawn a Creeper!`,
description: `The Minecraft server returned the following error:\n${e}`,
color: 0xeb3b5a,
},
],
});
}