Skip to content

Instantly share code, notes, and snippets.

@boly38
Last active February 16, 2024 12:14
Show Gist options
  • Save boly38/2d91ed5f17f9dcfaa559bfecb484eb1f to your computer and use it in GitHub Desktop.
Save boly38/2d91ed5f17f9dcfaa559bfecb484eb1f to your computer and use it in GitHub Desktop.
import * as mineflayer from "mineflayer";
const username = process.env.BOT_USERNAME || 'SAMPLE[bot]';
const host = "127.0.0.1";
const port = process.env.MINECRAFT_SERVER_PORT || 25565; // /publish true adventure 25565
console.log(`chicken bot ${host}:${port}`)
const bot = mineflayer.createBot({host, port, username});
// Log errors and kick reasons:
bot.on('kicked', console.log)
bot.on('error', console.log)
bot.on("death", console.log)
bot.on('spawn', () => {
const simpleRegex = "simpleRegex";
bot.addChatPattern(simpleRegex, /coucou/, {})
bot.on(`chat:${simpleRegex}`, (arg0,arg1,arg2,arg3,arg4,arg5,arg6) => {
console.log(JSON.stringify({arg0,arg1,arg2,arg3,arg4,arg5,arg6}));
// [t] coucou toi
// example : {"arg0":["<boly38> coucou toi"]}
const botUsername = bot.username;
if (arg0[0].includes(botUsername)) {
console.warn(`chat:${simpleRegex}: OH WAIT : dont trigger pattern on my own tchat`);
} else {
bot.chat(`${simpleRegex}oh You say coucou`)// including pattern "coucou" here would cause infinite loop when trigger my own tchat is allowed
}
});
const withOneGroupRegex = "withOneGroupRegex";
bot.addChatPattern(withOneGroupRegex, /salut ([a-zA-Z]*)$/, {})
bot.on(`chat:${withOneGroupRegex}`, (arg0,arg1,arg2,arg3,arg4,arg5,arg6) => {
console.log(JSON.stringify({arg0,arg1,arg2,arg3,arg4,arg5,arg6}));
// [t] salut toi
// example : {"arg0":["<boly38> salut toi"]}
const botUsername = bot.username;
if (arg0[0].includes(botUsername)) {
console.warn(`chat:${withOneGroupRegex}: OH WAIT : dont trigger pattern on my own tchat`);
} else {
bot.chat(`${withOneGroupRegex} oh You say >salut xxx`)// including pattern
}
});
const withGroupRegexParseTrue = "withGroupRegexParseTrue";
bot.addChatPattern(withGroupRegexParseTrue, /bonjour ([a-zA-Z]*)$/, {"parse": true})
bot.on(`chat:${withGroupRegexParseTrue}`, (arg0,arg1,arg2,arg3,arg4,arg5,arg6) => {
console.log(JSON.stringify({arg0,arg1,arg2,arg3,arg4,arg5,arg6}));
// [t] bonjour toi
// example :{"arg0":[["toi"]]}
});
})
/**
$ node tests/manual/addChatPattern.js
chicken bot 127.0.0.1:25565
{"arg0":["<boly38> coucou toi"]}
{"arg0":["<SAMPLE[bot]> simpleRegexoh You say coucou"]}
chat:simpleRegex: OH WAIT : dont trigger pattern on my own tchat
{"arg0":["<boly38> salut toi"]}
{"arg0":["<SAMPLE[bot]> withOneGroupRegex oh You say >salut xxx"]}
chat:withOneGroupRegex: OH WAIT : dont trigger pattern on my own tchat
{"arg0":[["toi"]]}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment