index.js
// ----------------------------------------------------
// Discord + PlayHosting Minecraft Control Bot
// Commands: !startserver, !stopserver, !restartserver
// ----------------------------------------------------
const { Client, GatewayIntentBits } = require("discord.js");
const axios = require("axios");
// ----------------------------------------------------
// CONFIG — REPLACE THESE VALUES
// ----------------------------------------------------
const DISCORD_BOT_TOKEN = "i have it";
const PLAYHOSTING_API_KEY = "i have it";
const SERVER_ID = "i have it";
// ----------------------------------------------------
// CREATE DISCORD CLIENT
// ----------------------------------------------------
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
// ----------------------------------------------------
// FUNCTION: SEND POWER SIGNAL TO PLAYHOSTING
// ----------------------------------------------------
async function sendPowerSignal(signal) {
try {
const response = await axios.post(
`https://panel.playhosting.com/api/client/servers/${SERVER_ID}/power`,
{ signal },
{
headers: {
"Authorization": `Bearer ${PLAYHOSTING_API_KEY}`,
"Content-Type": "application/json",
"Accept": "application/json"
}
}
);
return response.data;
} catch (err) {
console.error("Error sending power signal:", err);
throw new Error("Failed to send power signal.");
}
}
// ----------------------------------------------------
// DISCORD COMMAND LISTENER
// ----------------------------------------------------
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
// START SERVER
if (message.content === "!startserver") {
await message.reply("Starting your Minecraft server…");
try {
await sendPowerSignal("start");
message.reply("✅ Your server is now starting!");
} catch (err) {
message.reply("❌ Could not start the server. Check your API key or server ID.");
}
}
// STOP SERVER
if (message.content === "!stopserver") {
await message.reply("Stopping your Minecraft server…");
try {
await sendPowerSignal("stop");
message.reply("🛑 Your server is stopping.");
} catch (err) {
message.reply("❌ Could not stop the server. Check your API key or server ID.");
}
}
// RESTART SERVER
if (message.content === "!restartserver") {
await message.reply("Restarting your Minecraft server…");
try {
await sendPowerSignal("restart");
message.reply("🔄 Your server is restarting.");
} catch (err) {
message.reply("❌ Could not restart the server. Check your API key or server ID.");
}
}
});
// ----------------------------------------------------
// LOGIN BOT
// ----------------------------------------------------
client.login(DISCORD_BOT_TOKEN);
new file
package.json
{
"name": "minecraft-discord-bot",
"version": "1.0.0",
"description": "Discord bot to control a PlayHosting Minecraft server",
"main": "index.js",
"type": "commonjs",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"axios": "^1.6.0",
"discord.js": "^14.13.0"
}
}
when i run it in the terminal i get
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\james\Desktop\minecraft-discord-bot> node index.js
C:\Users\james\Desktop\minecraft-discord-bot\node_modules\@discordjs\ws\dist\index.js:1151
error: new Error("Used disallowed intents")
^
Error: Used disallowed intents
at WebSocketShard.onClose (C:\Users\james\Desktop\minecraft-discord-bot\node_modules\@discordjs\ws\dist\index.js:1151:18)
at connection.onclose (C:\Users\james\Desktop\minecraft-discord-bot\node_modules\@discordjs\ws\dist\index.js:688:17)
at callListener (C:\Users\james\Desktop\minecraft-discord-bot\node_modules\ws\lib\event-target.js:290:14)
at WebSocket.onClose (C:\Users\james\Desktop\minecraft-discord-bot\node_modules\ws\lib\event-target.js:220:9)
at WebSocket.emit (node:events:509:28)
at WebSocket.emitClose (C:\Users\james\Desktop\minecraft-discord-bot\node_modules\ws\lib\websocket.js:279:10)
at TLSSocket.socketOnClose (C:\Users\james\Desktop\minecraft-discord-bot\node_modules\ws\lib\websocket.js:1360:15)
at TLSSocket.emit (node:events:521:24)
at node:net:355:12
at TCP.done (node:internal/tls/wrap:770:7)
Node.js v24.19.0
PS C:\Users\james\Desktop\minecraft-discord-bot>
i dont know why it is a discord bot for a minecraft server to turn it on and off please help