Add commands

This commit is contained in:
7cc5c4f330d47060 2024-10-23 23:35:21 -04:00
parent dfcb9c5200
commit 140ce538e8
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA
10 changed files with 599 additions and 2 deletions

282
commands/about.js Normal file
View file

@ -0,0 +1,282 @@
import os from 'node:os'
import { execSync } from 'child_process'
import { getMessage, formatTime } from '../util/lang.js'
import memoryconvert from '../util/memoryconvert.js'
import { readdirSync, readFileSync } from "node:fs"
import { default as botVersion } from "../util/version.js"
import { default as version } from "../version.json" with { type: "json" }
import { bots } from "../index.js"
const aboutBot = function (c) {
c.reply({
translate: getMessage(c.lang, 'command.about.author'),
color: c.colors.secondary,
with: [
{
text: version.botName,
color: c.colors.primary
},
{
text: version.botAuthor,
color: c.colors.primary
}
]
})
if (version.isPreRelease) {
c.reply({
text: getMessage(c.lang, 'command.about.preRelease'),
color: c.colors.secondary
})
}
c.reply({ text: '' })
c.reply({
text: getMessage(c.lang, 'command.about.license'),
color: c.colors.secondary
})
c.reply({
translate: getMessage(c.lang, 'command.about.sourceCode'),
color: c.colors.secondary,
with: [
{
text: version.sourceURL,
color: c.colors.primary,
clickEvent: {
action: 'open_url',
value: version.sourceURL
},
hoverEvent: {
action: 'show_text',
contents: {
text: getMessage(c.lang, 'command.about.sourceCode.openInBrowser')
},
value: { // Added twice for backwards compatibility
text: getMessage(c.lang, 'command.about.sourceCode.openInBrowser')
}
}
}
]
})
}
const os2 = function (o2, l) {
switch (o2) {
case 'win32':
return `${os.version()}`
case 'android':{
try {
const version = execSync('getprop ro.build.version.release').toString('UTF-8').split('\n')[0]
return getMessage(l, 'command.about.serverInfo.os.android', [version])
} catch (e) {
return getMessage(l, 'command.about.serverInfo.os.android.noVersion')
}
}
case 'linux':
case 'freebsd':{
if (readdirSync('/etc').includes('os-release')) {
const osrelease = readFileSync('/etc/os-release').toString('UTF-8').split('\n')
const osrelease2 = {}
for (const item of osrelease) {
if (!item.includes('=')) continue
let osrvalue = item.split('=')[1]
if (osrvalue.startsWith('"') && osrvalue.endsWith('"')) { osrvalue = osrvalue.slice(1, osrvalue.length - 1) };
osrelease2[item.split('=')[0]] = osrvalue
}
if (osrelease2.PRETTY_NAME) {
return getMessage(l, '%s', [osrelease2.PRETTY_NAME])
} else {
return getMessage(l, `command.about.serverInfo.os.${o2}`)
}
} else {
return getMessage(l, `command.about.serverInfo.os.${o2}`)
}
}
default:
return o2
}
}
const aboutServer = function (c) {
const displayInfo = function (name, infoFunc) {
let thisItem
try {
thisItem = infoFunc()
} catch (e) {
console.error(e)
thisItem = 'Error! (check console)'
}
c.reply({
translate: '%s: %s',
color: c.colors.primary,
with: [
{
text: getMessage(c.lang, name),
color: c.colors.secondary
},
{
text: thisItem,
color: c.colors.primary,
clickEvent: {
action: 'copy_to_clipboard',
value: thisItem
},
hoverEvent: {
action: 'show_text',
contents: {
text: getMessage(c.lang, 'copyText'),
color: c.colors.secondary
},
value: { // Added twice for backwards compatibility
text: getMessage(c.lang, 'copyText'),
color: c.colors.secondary
}
}
}
]
})
}
// Operating system
displayInfo('command.about.serverInfo.os', () => {
return os2(process.platform, c.lang)
})
// Kernel version: os.release()
displayInfo('command.about.serverInfo.kernelVer', () => {
return os.release()
})
// Processor
if (os.cpus()[0]) {
displayInfo('command.about.serverInfo.processor', () => {
return os.cpus()[0].model
})
}
if (os.cpus()[0]) {
// Processor architecture
displayInfo('command.about.serverInfo.arch', () => {
return os.machine()
})
}
// System memory (total)
displayInfo('command.about.serverInfo.totalMem', () => {
return memoryconvert(os.totalmem())
})
// System memory (free)
displayInfo('command.about.serverInfo.freeMem', () => {
return memoryconvert(os.freemem())
})
// System memory (used)
displayInfo('command.about.serverInfo.usedMem', () => {
return memoryconvert(os.totalmem() - os.freemem())
})
// Username and UID
displayInfo('command.about.serverInfo.osUsername', () => {
return `${os.userInfo().username} (${os.userInfo().uid})`
})
// Hostname
displayInfo('command.about.serverInfo.hostName', () => {
return os.hostname()
})
// Current working directory
displayInfo('command.about.serverInfo.workingDir', () => {
return process.cwd()
})
// Node.js® version
displayInfo('command.about.serverInfo.nodeVersion', () => {
return process.version
})
// Bot uptime
displayInfo('command.about.serverInfo.runTime', () => {
return formatTime(process.uptime() * 1000, c.lang)
})
// System uptime
displayInfo('command.about.serverInfo.upTime', () => {
return formatTime(os.uptime() * 1000, c.lang)
})
if (process.platform === 'android') {
// Device model
displayInfo('command.about.serverInfo.os.android.model', () => {
const brand = execSync('getprop ro.product.brand').toString('UTF-8').split('\n')[0]
const model = execSync('getprop ro.product.model').toString('UTF-8').split('\n')[0]
return `${brand} ${model}`
})
}
// Bot version
displayInfo('command.about.serverInfo.botVer', () => {
return botVersion
})
}
const displayServerList = function (c) {
index.bots.forEach((item, i) => {
if (c.bot.id === i && c.bot.host.options.hideLocally) return
if (item.host.options && item.host.options.hidden && c.verify !== 2 && c.bot.id !== i) return
let message = 'command.about.serverListItem'
if (c.bot.id === i) message = 'command.about.serverListItem.thisServer'
let host = item.host.host
const port = item.host.port
if (item.host.options && item.host.options.displayAsIPv6) {
host = `[${host}]`
}
c.reply({
translate: getMessage(c.lang, message),
color: c.colors.secondary,
with: [
{
text: i.toString(),
color: c.colors.primary
},
{
text: `${host}:${port}`,
color: c.colors.primary,
clickEvent: {
action: 'copy_to_clipboard',
value: `${host}:${port}`
},
hoverEvent: {
action: 'show_text',
contents: {
text: getMessage(c.lang, 'copyText'),
color: c.colors.secondary
},
value: { // Added twice for backwards compatibility
text: getMessage(c.lang, 'copyText'),
color: c.colors.secondary
}
}
}
]
})
})
}
const execute = function (c) {
let subcmd
if (c.args.length >= 1) subcmd = c.args[0].toLowerCase()
if (subcmd === 'servers') subcmd = 'serverlist'
if (c.cmdName.toLowerCase() === 'serverinfo' || c.cmdName.toLowerCase() === 'specs') subcmd = 'server'
if (c.cmdName.toLowerCase() === 'serverlist' || c.cmdName.toLowerCase() === 'servers') subcmd = 'serverlist'
if (subcmd === 'server') {
aboutServer(c)
} else if (subcmd === 'serverlist') {
displayServerList(c)
} else {
aboutBot(c)
}
}
const aliases = ['info', 'serverlist', 'servers', 'serverinfo', 'specs']
export { execute, aliases }

49
commands/test.js Normal file
View file

@ -0,0 +1,49 @@
import { getMessage } from '../util/lang.js'
const execute = (c) => {
const reply = function (name, item) {
return {
translate: '%s: %s',
color: c.colors.primary,
with: [
{
text: getMessage(c.lang, `command.test.${name}`),
color: c.colors.secondary
},
{
text: item,
color: c.colors.primary,
clickEvent: {
action: 'copy_to_clipboard',
value: item
},
hoverEvent: {
action: 'show_text',
contents: {
text: getMessage(c.lang, 'copyText'),
color: c.colors.secondary
},
value: { // Added twice for backwards compatibility
text: getMessage(c.lang, 'copyText'),
color: c.colors.secondary
}
}
}
]
}
}
c.reply(reply('uuid', c.uuid))
c.reply(reply('username', c.username))
c.reply(reply('nickname', c.nickname))
c.reply(reply('command', c.command))
c.reply(reply('msgType', c.msgType))
c.reply(reply('msgSubtype', c.msgSubtype))
c.reply(reply('prefix', c.prefix))
c.reply(reply('args', c.args.join(', ')))
c.reply(reply('verify', c.verify.toString()))
c.reply(reply('host', c.host))
c.reply(reply('port', c.port.toString()))
c.reply(reply('lang', c.lang))
c.reply(reply('colorPrimary', c.colors.primary))
c.reply(reply('colorSecondary', c.colors.secondary))
}
export { execute }

133
lang/en_US.json Normal file
View file

@ -0,0 +1,133 @@
{
"language.name": "English",
"language.region": "United States",
"time.week": " week ",
"time.weekPlural": " weeks ",
"time.day": " day ",
"time.dayPlural": " days ",
"time.hour": " hour ",
"time.hourPlural": " hours ",
"time.minute": " minute ",
"time.minutePlural": " minutes ",
"time.second": " second ",
"time.secondPlural": " seconds ",
"chat.antiSpamTriggered": "Anti-spam has been triggered for this server.",
"command.about.usage": "",
"command.about.desc": "About the bot",
"command.cb.usage": " <command>",
"command.cb.desc": "Run a command in a command block",
"command.cloop.usage": " add <rate> <command>|| remove <index>|| list|| clear",
"command.cloop.desc": "Manage command loops",
"command.eval.usage": " <code>",
"command.eval.desc": "Run JavaScript code",
"command.help.usage": " [cmd]",
"command.help.desc": "Shows command help",
"command.logoff.usage": "",
"command.logoff.desc": "Disconnect and reconnect the bot from a server",
"command.netmsg.usage": " <message>",
"command.netmsg.desc": "Send a message to all servers the bot is connected to",
"command.refill.usage": "",
"command.refill.desc": "Refill core",
"command.say.usage": " <message>",
"command.say.desc": "Sends a message to chat",
"command.settings.usage": " get|| set <key> <value>",
"command.settings.desc": "Set your user preferences",
"command.restart.usage": "",
"command.restart.desc": "Restart bot",
"command.stop.usage": "",
"command.stop.desc": "Stop bot",
"command.template.usage": " <required> [optional]",
"command.template.desc": "Does nothing",
"command.test.usage": " [args...]",
"command.test.desc": "Chat parsing debugger command",
"command.tpr.usage": "",
"command.tpr.desc": "Teleport to a random location",
"command.validate.usage": " [args...]",
"command.validate.desc": "Check the hashing system",
"command.about.author": "%s - a Minecraft bot made by %s for Kaboom and clones",
"command.about.version": "Version %s",
"command.about.preRelease": "This is a development version - there may be errors, and features may be changed or removed at any time. Please report any errors to the bot's developer.",
"command.about.sourceCode": "Source code: %s",
"command.about.license": "This bot is free and open-source software and is available under the terms of the MIT license.",
"command.about.sourceCode.openInBrowser": "Click to open the source code link in your default browser",
"command.cloop.error.tooShort": "Command loops must have a rate above 20ms.",
"command.cloop.error.subcommand": "Unknown subcommand, please do %s",
"command.cloop.success.add": "Added command loop with command %s and rate %s",
"command.cloop.success.remove": "Removed command loop %s",
"command.cloop.success.clear": "Cleared all command loops",
"command.cloop.list": "%s: Command: %s Rate: %s",
"command.eval.output": "Output",
"command.help.cmdList": "Commands",
"command.help.commandInfo": "%s%s - %s",
"command.help.commandUsage": "Usage - %s%s",
"command.help.commandDesc": "Description - %s",
"command.help.commandPerms": "Required permissions - %s",
"command.help.commandUsage.lf": "Usage - %s%s\n",
"command.help.commandDesc.lf": "Description - %s\n",
"command.help.commandPerms.lf": "Required permissions - %s\n",
"command.help.runCommand": "Click to run command",
"command.help.permsNormal": "Public",
"command.help.permsTrusted": "Trusted",
"command.help.permsOwner": "Owner",
"command.help.noCommand": "Command does not exist",
"command.help.alias": "Alias to %s",
"command.netmsg.disabled": "This command has been disabled on this server.",
"command.netmsg.serverAddress": "Server Address",
"command.settings.disabled.console": "This command cannot be run from the console.",
"command.settings.get.colorPrimary": "Primary color",
"command.settings.get.colorSecondary": "Secondary color",
"command.settings.get.language": "Language",
"command.settings.setLanguage": "Run %s to set this as your language",
"command.settings.error.invalidKey": "Invalid key",
"command.settings.error.invalidLanguage": "Invalid language",
"command.settings.error.mustProvideValue": "You must provide a value",
"command.settings.saved": "Settings saved.",
"command.test.uuid": "UUID",
"command.test.username": "Username",
"command.test.nickname": "Nickname",
"command.test.command": "Command",
"command.test.msgType": "Message type",
"command.test.msgSubtype": "Message subtype",
"command.test.prefix": "Prefix",
"command.test.args": "Arguments",
"command.test.verify": "Permission level",
"command.test.host": "Server host",
"command.test.port": "Server port",
"command.test.lang": "Language",
"command.test.colorPrimary": "Primary color",
"command.test.colorSecondary": "Secondary color",
"command.about.serverInfo.os.android": "Android %s",
"command.about.serverInfo.os.android.noVersion": "Android",
"command.about.serverInfo.os.freebsd": "FreeBSD",
"command.about.serverInfo.os.linux": "Linux",
"command.about.serverInfo.os.macos": "macOS",
"command.about.serverInfo.os.macos_old": "OS X",
"command.about.serverInfo.os": "Operating system",
"command.about.serverInfo.kernelVer": "Kernel version",
"command.about.serverInfo.processor": "CPU",
"command.about.serverInfo.arch": "Architecture",
"command.about.serverInfo.totalMem": "Total memory",
"command.about.serverInfo.freeMem": "Free memory",
"command.about.serverInfo.usedMem": "Used memory",
"command.about.serverInfo.osUsername": "Username",
"command.about.serverInfo.hostName": "Hostname",
"command.about.serverInfo.workingDir": "Working directory",
"command.about.serverInfo.runTime": "Bot uptime",
"command.about.serverInfo.upTime": "System uptime",
"command.about.serverInfo.nodeVersion": "Node.js version",
"command.about.serverInfo.osRelease": "Linux release",
"command.about.serverInfo.osRelease.missing": "/etc/os-release does not exist. Information may be limited.",
"command.about.serverInfo.os.android.version": "Android version",
"command.about.serverInfo.os.android.model": "Device model",
"command.about.serverInfo.botName": "Bot name",
"command.about.serverInfo.botVer": "Bot version",
"command.about.serverListItem": "Server %s - %s",
"command.about.serverListItem.thisServer": "Server %s - %s (this server)",
"command.tpr.success": "Teleporting %s to %s, %s, %s",
"command.verify.success": "Successfully verified with permission level %s",
"command.error": "An error occured (check console for more info)",
"command.disallowed.perms": "You do not have permission to run this command. If you do have permission, please make sure you put the command hash at the end, or ran the command through the hashing system of your client or proxy.",
"command.disallowed.perms.yourLevel": "Your permission level: %s",
"command.disallowed.perms.cmdLevel": "Command requires: %s",
"copyText": "Click to copy!"
}

44
plugins/command.js Normal file
View file

@ -0,0 +1,44 @@
import cmds from "../util/commands.js"
import { default as settings } from '../settings.json' with {type: "json"}
import Command from '../util/Command.js'
export default function load (b) {
b.on('chat', (data) => { // constructor (uuid, user, nick, cmd, senderType, msgType, msgSubtype, prefix, bot, prefs) {
const fullCommand = data.message
for (const prefix of settings.prefixes) {
if (fullCommand.startsWith(prefix)) {
const command = fullCommand.slice(prefix.length)
b.runCommand(data.username, data.nickname, data.uuid, command, data.type, data.subtype, prefix)
}
}
})
b.runCommand = function (user, nick, uuid, command, type, subtype, prefix){
const context = new Command(uuid, user, nick, command, "minecraft", type, subtype, prefix, b, {})
if(cmds[context.cmdName.toLowerCase()]){
try {
cmds[context.cmdName.toLowerCase()].execute(context)
} catch (e) {
console.log(e)
context.reply({
text: "An error occured (check console)"
})
}
}
}
}
/*
{
parsed: true,
json: {
color: '#FF99DD',
hoverEvent: { action: 'show_text', contents: [Object] },
translate: '%s %s %s',
with: [ [Object], [Object], [Object] ]
},
type: 'system',
subtype: 'chipmunkmod_name3',
uuid: '00000000-265d-39fa-979f-d8c9d835084e',
message: 'uwu',
nickname: 'owo439895035',
username: 'owo439895035'
}
*/

View file

@ -104,7 +104,7 @@ export default function load (b) {
text: 'Prefix'
},
{
text: settings.prefix[0],
text: settings.prefixes[0],
color: settings.colors.primary
}
]

16
util/Command.js Normal file
View file

@ -0,0 +1,16 @@
import { default as settings } from '../settings.json' with {type: "json"}
export default class Command {
constructor (uuid, user, nick, cmd, senderType, msgType, msgSubtype, prefix, bot, prefs) {
this.uuid=uuid;
this.reply = text => bot.tellraw(uuid, text)
this.username = user;
this.nickname = nick;
this.args = cmd.split(' ').slice(1)
this.cmdName = cmd.split(' ')[0]
this.prefix = prefix
this.colors = settings.colors
this.verify = 0
this.host = "google.com"
this.port = 443
}
}

32
util/commands.js Normal file
View file

@ -0,0 +1,32 @@
const cmds = Object.create(null)
const bpl = readdirSync('commands')
import { readdirSync } from "node:fs"
for (const plugin of bpl) {
if (!plugin.endsWith('.js')) {
continue
}
try {
const commandName = plugin.split('.js')[0]
import(`../commands/${plugin}`).then((pluginItem)=>{
cmds[commandName] = pluginItem // For rejoining
})
} catch (e) { console.log(e) }
}
for(const commandName in cmds){
if (cmds[commandName].aliases) {
for (const j in cmds[commandName].aliases) {
cmds[cmds[commandName].aliases[j]] = {
execute: cmds[commandName].execute,
alias: commandName,
usage: cmds[commandName].usage,
level: cmds[commandName].level,
hidden: true,
consoleIndex: cmds[commandName].consoleIndex
}
}
}
}
export default cmds

16
util/memoryconvert.js Normal file
View file

@ -0,0 +1,16 @@
export default function memoryconvert (bytes) {
if (bytes >= 1125899906842624) { // Petabytes
return `${Math.round(bytes / 1125899906842624 * 100) / 100} PB`
} else if (bytes >= 1099511627776) { // Terabytes
return `${Math.round(bytes / 1099511627776 * 100) / 100} TB`
} else if (bytes >= 1073741824) { // Gigabytes
return `${Math.round(bytes / 1073741824 * 100) / 100} GB`
} else if (bytes >= 1048576) { // Megabytes
return `${Math.round(bytes / 1048576 * 100) / 100} MB`
} else if (bytes >= 1024) { // Kilobytes
return `${Math.round(bytes / 1024 * 100) / 100} KB`
} else { // Bytes
return `${bytes} B`
}
}

19
util/version.js Normal file
View file

@ -0,0 +1,19 @@
import { default as version } from "../version.json" with { type: "json" }
import { execSync } from "child_process"
let botVersion = version.botVersion
let gitCommit
let gitBranch
try {
gitCommit = execSync('git rev-parse --short HEAD').toString('UTF-8').split('\n')[0]
gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString('UTF-8').split('\n')[0]
} catch (e) {
gitCommit = false
gitBranch = false
}
if (gitCommit) {
botVersion += ` (${gitCommit} - ${gitBranch})`
}
export default botVersion

View file

@ -1 +1,7 @@
{}
{
"botName": "botv12 Dev",
"botVersion": "12.0.0-alpha.1",
"botAuthor": "owo439895035",
"isPreRelease": true,
"sourceURL": "https://code.chipmunk.land/7cc5c4f330d47060/botv12"
}