Control Mineflayer with a Discord Bot #1499
-
I have made a discord bot with commands to control the mineflayer bot (!join ). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Can you provide code? |
Beta Was this translation helpful? Give feedback.
-
@Dani3l3-24 Your issue is here. You are trying to export the bot variable from within a function. You cannot do this. Each file should only have one export call. You might have a lot more luck returning the bot from within the function instead to access it. module.exports = {
name: 'join',
description: "Join the bot on a server",
execute(message, args, dbot) {
const mbot = mineflayer.createBot({
host: args[0].toString(),
port: parseInt(args[1]),
username: args[2],
version: false,
})
mbot.on('login', () => {
console.log(`${args[2]} has logged ${args[0]}:${args[1]}`)
message.channel.send(`${args[2]} has logged ${args[0]}:${args[1]}`)
})
return mbot;
}
} Because you are not creating the bot instantly on startup, you cannot import it via require. You'll have to pass the bot in as a function argument to all commands. module.exports = {
name: 'leave',
description: "Leave the bot from a server",
execute(message, args, dbot, mbot) { // <- here
mbot.end
}
} Back in your index.js file, you retrieve the bot from the join execution and save it as a local variable. let mbot; // <-- here
dbot.on('message', message =>{
if(!message.content.startsWith(dprefix) || message.author.bot) return;
const args = message.content.slice(dprefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'join'){
// here
mbot = dbot.commands.get('join').execute(message, args, dbot);
}
if(command === 'leave'){
dbot.commands.get('leave').execute(message, args, dbot. mbot); // <-- here
}
if(command === 'echo'){
dbot.commands.get('echo').execute(message, args, dbot, mbot); // <-- here
}
}); |
Beta Was this translation helpful? Give feedback.
@Dani3l3-24
Your issue is here.
https://github.com/Dani3l3-24/MineflayerDiscord/blob/05bccadb81b1fb5509ef75c94494aaf145178020/commands/join.js#L20
You are trying to export the bot variable from within a function. You cannot do this. Each file should only have one export call. You might have a lot more luck returning the bot from within the function instead to access it.