Minecraft bot 編寫教學#2 上次很簡單的講述了如何建立一隻自製的客戶端 這次我要來講解一下如何使用listener ``` const mineflayer = require("mineflayer") let loginOpts = { //載入登入的資料 host: `locaolhost`, //伺服器ip port: 25565, //port username: `username`, //賣快帳號的登入的電子郵件 password: `password`, //你的賣快帳號密碼 version: "1.16.4" //客戶端登入的版本(mineflayer 支援1.16.4) } const bot = mineflayer.createBot(loginopts) //這邊要加入的listener 是message (意思大概是偵測到訊息的時候) bot.on("message",async function(jsonMsg){ //jsonMsg 是在這個listener 裡面你可以得到的資訊 const message = jsonMsg.toString()//清除jsonMsg的格式 //到了這邊我們就可以很簡單的用 if 的判定式判定訊息的內容來執行指定動作了 if(message ===`[收到私訊 Miku0139] : 123`){ //記得需要3個= bot.chat(`hello`) //讓bot 說出hello } //當然還有更高階的利用,這邊會示範自動接受tpa 還有tpahere 請求的程式碼 const whitelist =["minecraft ID"] //這邊先宣告白名單 if (message.startsWith(`[廢土伺服] :`) && message.toLowerCase().includes(`想要你傳送到 該玩家 的位置!`)) { let dec = message.split(/ +/g); let playerid = dec[2] //2 if (whitelist.includes(playerid)) { bot.chat(`/tok`) } else { bot.chat(`/tno`) } } if (messagestartsWith(`[廢土伺服] :`) && message.toLowerCase().includes(`想要傳送到 你 的位置`)) { //切訊息 let dec = message.split(/ +/g); let playerid = dec[2] //2 if (whitelist.includes(playerid)) { bot.chat(`/tok`) } else { bot.chat(`/tno`) } } }) //這邊程式碼的部分懶人可以直接複製貼上喔~~
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up