owobot/plugins/selfcare.js

136 lines
4.2 KiB
JavaScript
Raw Permalink Normal View History

2024-09-19 23:27:00 -04:00
const parsePlain = require('../util/chatparse_plain.js')
const parseMc = require('../util/chatparse_mc_withHex.js')
const settings = require('../settings.json')
const version = require('../version.json')
2024-08-12 05:13:32 -04:00
class SCTask {
constructor (failTask, startFailed = false) {
/*
* failed: Whether to run this task
* failTask: Command to run when failed is true
*/
this.failed = startFailed
this.failTask = failTask
}
}
module.exports = {
load: (b) => {
b.sc_tasks = {}
b.selfcareRun = 0
2024-10-30 14:11:49 -04:00
b._client.on('login', () => {
b.interval.sc = setInterval(() => {
if (Date.now() - b.selfcareRun <= 600) {
return
2024-08-12 05:13:32 -04:00
}
2024-10-30 14:11:49 -04:00
for (const i in b.sc_tasks) {
if (b.sc_tasks[i].failed) {
b.sc_tasks[i].failTask()
b.selfcareRun = Date.now()
}
}
}, 40)
})
2024-08-12 05:13:32 -04:00
b.add_sc_task = (name, failTask, startFailed) => {
b.sc_tasks[name] = new SCTask(failTask, startFailed)
}
// Self care tasks
// Operator
b.add_sc_task('op', () => {
b.chat('/op @s[type=player]')
})
b._client.on('login', (p) => {
b.entityId = p.entityId
})
b._client.on('entity_status', (p) => {
if (p.entityId === b.entityId && p.entityStatus === 24) {
b.sc_tasks.op.failed = 1
} else if (p.entityId === b.entityId && p.entityStatus === 28) {
b.sc_tasks.op.failed = 0
}
})
// CommandSpy
if (!b.host.options.isVanilla) {
b.add_sc_task('cspy', () => {
b.chat('/cspy on')
}, true)
b.on('plainchat', (msg) => {
if (msg === 'Successfully disabled CommandSpy') {
b.sc_tasks.cspy.failed = 1
} else if (msg === 'Successfully enabled CommandSpy') {
b.sc_tasks.cspy.failed = 0
}
})
}
2024-08-25 21:45:47 -04:00
// Gamemode / end portal bug
2024-08-12 05:13:32 -04:00
b.add_sc_task('gamemode', () => {
b.chat('/minecraft:gamemode creative')
})
b._client.on('game_state_change', (p) => {
if (p.reason === 3 && p.gameMode !== 1) {
b.sc_tasks.gamemode.failed = 1
} else if (p.reason === 3 && p.gameMode === 1) {
b.sc_tasks.gamemode.failed = 0
2024-08-25 22:29:39 -04:00
} else if (p.reason === 4) {
2024-08-25 21:45:47 -04:00
b.sc_tasks.respawn.failed = 1
2024-08-12 05:13:32 -04:00
}
})
// Respawning after dying
b.add_sc_task('respawn', () => {
b._client.write('client_command', { actionId: 0 }) // Simulates respawning
b.sc_tasks.respawn.failed = 0
})
2024-09-19 23:27:00 -04:00
b.on('chat_unparsed', (data) => {
if (data.json.translate === 'chat.disabled.options' || (data.json.extra && data.json.extra[0] && data.json.extra[0].translate === 'chat.disabled.options') ||
data.json.translate === 'Chat disabled in client options' || (data.json.extra && data.json.extra[0] && data.json.extra[0].translate === 'Chat disabled in client options')) {
2024-08-12 05:13:32 -04:00
b.sc_tasks.respawn.failed = 1
}
})
2024-09-19 23:27:00 -04:00
// Prefix tablist ads
if (!b.host.options.isVanilla) {
b.adPrefix = {
2024-10-12 01:44:33 -04:00
translate: '[%s] %s', // Since the bot aims to have an invisible name, the ad prefix should contain information about the bot.
2024-09-19 23:27:00 -04:00
color: 'white',
with: [
{
translate: '%s: %s',
color: settings.colors.secondary,
with: [
{
text: 'Prefix'
},
{
2024-10-12 01:35:09 -04:00
text: settings.prefix[0],
2024-09-19 23:27:00 -04:00
color: settings.colors.primary
}
]
},
{
text: version.botName,
color: settings.colors.primary
2024-09-19 23:27:00 -04:00
}
]
}
b.add_sc_task('playerlist_ads', () => {
b.chat(`/prefix ${parseMc(b.adPrefix).replaceAll('§', '&')}`)
})
b.on('playerdata', (uuid, displayName) => {
if (uuid === b._client.uuid && !displayName.startsWith(parsePlain(b.adPrefix))) {
b.sc_tasks.playerlist_ads.failed = 1
}
})
2024-09-19 23:52:42 -04:00
b.on('plainchat', (msg) => {
2024-10-30 13:57:22 -04:00
if (msg === `You now have the tag: ${parseMc(b.adPrefix).replaceAll('§', '&')}` ||
2024-10-30 13:58:11 -04:00
msg === 'Something went wrong while saving the prefix. Please check console.') { // Fix the prefix issue
2024-09-19 23:52:42 -04:00
b.sc_tasks.playerlist_ads.failed = 0
}
})
2024-09-19 23:27:00 -04:00
}
2024-08-12 05:13:32 -04:00
}
}