Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@louis030195 louis030195 commented Nov 19, 2020

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
You can’t perform that action at this time.