How to make a bot execute a command via messages in discord? Mineflayer bot

Viewed 36

I use the bot's Mineflayer and want it to be synchronized with the discord afterwards. At the moment, the bot outputs the game chat from minecraft to discord. And I want to be able to, say, write .runcmd /help and execute the /help command. But for some reason when I enter .runcmd nothing happens.

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Discord.Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
  ]
})
const mineflayer = require('mineflayer');

let channel = client.channels.cache.get("id discord channel")
let sending = false;
let chatData = [];

let bot = mineflayer.createBot({
  username: 'nick',
  host: 'ip',
  version: 'version',
})

client.on("ready", async =>{
  console.log("Ingame Bot Online")
})

bot.once('spawn', function () {
  bot.chat('/server1')
})

bot.on("message", message => {
  if (sending == true) {
    chatData.push(`${message}`)
  }
})

client.on("message", async msg => {
  let args = msg.content.split(" ").slice(1)
  
  if(msg.content.startsWith(".runcmd")) {
    let toSend = args.join(" ");
    if(!toSend) return msg.reply("No Args")

    bot.chat(toSend)
    sending = true
    msg.channel.send(`${msg.author.tag} just sent ${toSend}`)

    setTimeout(() => {
      sending = false
      msg.channel.send(chatData.join("\n"))
      chatData = []
    })

  }
})

bot.on("message", message => {
  let channel = client.channels.cache.get("id discord channel")
  if (!channel) return;
  channel.send(`- ${message}`)
})

client.login("token")```
0 Answers
Related