0% found this document useful (0 votes)
28 views2 pages

Text 2

This document outlines the setup for a Discord bot that interacts with a Minecraft server using the Discord.js and Mineflayer libraries. It includes the registration of slash commands for banning, unbanning, kicking, muting, and unmuting players, as well as the deployment of these commands to the Discord application. The bot listens for interactions and executes the corresponding Minecraft commands while providing feedback to the user in Discord.

Uploaded by

jadillll69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Text 2

This document outlines the setup for a Discord bot that interacts with a Minecraft server using the Discord.js and Mineflayer libraries. It includes the registration of slash commands for banning, unbanning, kicking, muting, and unmuting players, as well as the deployment of these commands to the Discord application. The bot listens for interactions and executes the corresponding Minecraft commands while providing feedback to the user in Discord.

Uploaded by

jadillll69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

require('dotenv').

config();
const { Client, GatewayIntentBits, REST, Routes } = require('discord.js');
const { createBot } = require('mineflayer');

// Discord Bot Setup


const discordClient = new Client({ intents: [GatewayIntentBits.Guilds] });
const token = process.env.DISCORD_TOKEN;

// Register Slash Commands


const commands = [
{
name: 'ban',
description: 'Ban a player in the Minecraft server',
options: [{ name: 'player', type: 3, description: 'Player name', required:
true }],
},
{
name: 'unban',
description: 'Unban a player in the Minecraft server',
options: [{ name: 'player', type: 3, description: 'Player name', required:
true }],
},
{
name: 'kick',
description: 'Kick a player in the Minecraft server',
options: [{ name: 'player', type: 3, description: 'Player name', required:
true }],
},
{
name: 'mute',
description: 'Mute a player in the Minecraft server',
options: [{ name: 'player', type: 3, description: 'Player name', required:
true }],
},
{
name: 'unmute',
description: 'Unmute a player in the Minecraft server',
options: [{ name: 'player', type: 3, description: 'Player name', required:
true }],
},
];

// Deploy commands
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
try {
console.log('Deploying commands...');
await rest.put(Routes.applicationCommands('your_app_id'), { body:
commands });
console.log('Commands deployed successfully!');
} catch (error) {
console.error(error);
}
})();

// Minecraft Bot Setup


const mcBot = createBot({
host: 'your_minecraft_server_host', // e.g., localhost
port: 25565, // Default port
username: 'bot_username', // Minecraft bot username
password: 'bot_password', // If using online mode
});

// Event Listener for Commands


discordClient.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

const { commandName, options } = interaction;

const player = options.getString('player');


if (!player) {
await interaction.reply({ content: 'Please specify a player.', ephemeral:
true });
return;
}

try {
switch (commandName) {
case 'ban':
mcBot.chat(`/ban ${player}`);
await interaction.reply(`Banned player ${player} in the Minecraft
server.`);
break;
case 'unban':
mcBot.chat(`/pardon ${player}`);
await interaction.reply(`Unbanned player ${player} in the Minecraft
server.`);
break;
case 'kick':
mcBot.chat(`/kick ${player}`);
await interaction.reply(`Kicked player ${player} in the Minecraft
server.`);
break;
case 'mute':
mcBot.chat(`/mute ${player}`);
await interaction.reply(`Muted player ${player} in the Minecraft
server.`);
break;
case 'unmute':
mcBot.chat(`/unmute ${player}`);
await interaction.reply(`Unmuted player ${player} in the Minecraft
server.`);
break;
default:
await interaction.reply({ content: 'Unknown command.', ephemeral:
true });
}
} catch (error) {
console.error(error);
await interaction.reply({ content: 'An error occurred while executing the
command.', ephemeral: true });
}
});

// Start the Discord Bot


discordClient.login(token);

You might also like