Skip to content

Instantly share code, notes, and snippets.

@louis030195
Created November 19, 2020 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louis030195/f0a90443b10e29472a4fff2f8ffd5b73 to your computer and use it in GitHub Desktop.
Save louis030195/f0a90443b10e29472a4fff2f8ffd5b73 to your computer and use it in GitHub Desktop.
Minecraft bot that detect toxic players using TensorflowJS and Mineflayer
const mineflayer = require("mineflayer")
require('@tensorflow/tfjs')
const toxicity = require('@tensorflow-models/toxicity')
if (process.argv.length < 4 || process.argv.length > 6) {
console.log('Usage : node toxicity.js <host> <port> [<name>] [<password>]')
process.exit(1)
}
const bot = mineflayer.createBot({
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4],
password: process.argv[5]
})
// Wait until we spawn
bot.on('spawn', () => {
log('hello I\'m a bot, insult me please')
bot.on('chat', (username, message) => {
if (username !== bot.username) predict(username, message)
})
})
async function predict(username, sentence) {
const threshold = 0.9
const model = await toxicity.load(threshold)
const labels = model.model.outputNodes.map(d => d.split('/')[0])
const results = await model.classify([sentence])
if (results.length == 0) return
log(`${username} said ${sentence}`)
console.log(JSON.stringify(results))
results.forEach((classification) => {
if (classification.results[0].match === true) {
log(`${classification.label}:${classification.results[0].match}`)
}
})
}
function log(msg) {
console.log(msg)
bot.chat(msg)
}
@louis030195
Copy link
Author

Installation

npm i @tensorflow/tfjs @tensorflow-models/toxicity mineflayer

Usage

node toxicity.js <SERVER_IP> <SERVER_PORT> <MY_USERNAME> <MY_PASSWORD>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment