Search code examples
discord.jsmineflayer

Discord Slashcommand sends message in game


My bot doesn't do its prefered job, but I get no errors from the bot? Im trying to make it to where the text from the slash command gets sent in game.

Moving "const bot" into and out of functions did nothing.

code:

const fs = require("fs");
const path = require("path");
const mineflayer = require("mineflayer");
const { Client, Events, GatewayIntentBits, SlashCommandBuilder} = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

const OPTIONS = {
  host: '0b0t.org',
  username: '',
  auth: 'microsoft',
  version: '1.12.2'
};

const bot = mineflayer.createBot(OPTIONS);

client.once(Events.ClientReady, readyClient => {
    console.log(`✅Bot loaded!`);
  client.user.setActivity('0b0t is terrible');

  const ping = new SlashCommandBuilder()
  .setName('ping')
  .setDescription('pong')
  .addStringOption(option =>
    option
    .setName('message')
    .setDescription('msg to send')
    .setRequired(true)
    )
  client.application.commands.create(ping)
});


client.on('interactionCreate', (interaction) => {
  if(!interaction.isChatInputCommand()) return;
  if(interaction.command.name==='ping') {
    const message = interaction.options.getString('message')
    bot.chat(message)
    interaction.reply('sent')
  }
})

client.login(token);

Solution

  • As I see, you don't listen for events. Also, your command doesn't have the execute function, so it won't work.

    Try implementing the run/execute function to actually be able to see some output.

    .setRun(async (client, interaction) => {
     return interaction.reply({
      content: `Pong`
     });
    }
    

    Be also sure to deploy your command, after changing a structure of it (in that case: adding a setRun() function.

    Here's a discord.js guide for it: click