Skip to content

Instantly share code, notes, and snippets.

@Ayomide661
Last active May 25, 2025 18:22
Show Gist options
  • Save Ayomide661/30cbb7e8c1dacb96108c946ddb8a3152 to your computer and use it in GitHub Desktop.
Save Ayomide661/30cbb7e8c1dacb96108c946ddb8a3152 to your computer and use it in GitHub Desktop.
const { bot } = require('../lib/')
const { exec } = require('child_process')
bot(
{
pattern: 'npm ?(.*)',
fromMe: true,
desc: 'Execute NPM commands (install, uninstall, ls, cache clean, update, etc.)',
type: 'system',
},
async (message, match) => {
if (!match) return await message.send(
'*Available NPM Commands:*\n\n' +
'• Install package:\n npm install axios\n' +
'• Uninstall package:\n npm uninstall lodash\n' +
'• List packages:\n npm ls\n' +
'• Clean cache:\n npm cache clean\n' +
'• Update packages:\n npm update\n' +
'• Audit fixes:\n npm audit fix\n' +
'• View help:\n npm help'
)
const [cmd, ...argsArray] = match.split(' ')
const args = argsArray.join(' ').trim()
// Validate required arguments
if (['install', 'uninstall'].includes(cmd) && !args) {
return await message.send(`*Usage:* npm ${cmd} <package_name>`)
}
await message.send(`🔄 Running *npm ${cmd} ${args}*...`)
exec(`npm ${cmd} ${args}`, async (error, stdout, stderr) => {
// Error handling
if (error) {
console.error(`NPM Error: ${error}`)
return await message.send(
`*❌ Failed: npm ${cmd} ${args}*\n` +
`\`\`\`${error.message || 'Unknown error'}\`\`\``
)
}
// Prepare output
let output = [stdout, stderr].filter(Boolean).join('\n').trim()
if (!output) output = 'No output generated'
// Truncate long output
const maxLength = 1500
const truncated = output.length > maxLength
? output.substring(0, maxLength) + '...\n\n*Output truncated*'
: output
// Format response
const result = `*✅ npm ${cmd} ${args}*\n\`\`\`${truncated}\`\`\``
// Send as file if original output was too long
if (output.length > maxLength) {
await message.send(Buffer.from(output), {
filename: `npm-${cmd}-output.txt`,
mimetype: 'text/plain',
quoted: message.data
})
} else {
await message.send(result)
}
})
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment