Zonies-Bot/commands/command.js
2025-05-12 12:57:59 -05:00

91 lines
4.5 KiB
JavaScript

const Embed = require("../functions/embed")
const CommandsDB = require('../models/commands');
const { random } = require('../functions/randomStr');
const moment = require('moment');
module.exports = {
config: {
name: "command",
usage: true,
cooldown: 0,
available: true,
permissions: ['ManageServer'],
aliases: ['cmd']
},
run: async (client, message, args, db) => {
/*************************************************
* HELP
*************************************************/
if (args[0] === 'help') {
let embed = new Embed()
.setDescription(`### Custom Commands\n**add**: \`add [name] [content]\`\nEx: \`add soul Fucks his cousin|Is swedish\` or \`add zemax Is the punching bag for uploaders\`\nIf you want to add multiple options to a command use a pipe \`|\`\n\n**delete**: \`delete [name/id]\`\n\n**edit**: \`edit [name/id]\`\n\n**show**: \`show [name/id]\`\n\n`)
.setColor(`#A52F05`);
return message.reply({ embeds: [embed] }, false);
/*************************************************
* ADD
*************************************************/
} else if (args[0] === 'add') {
let name = args[1];
let content = args[2];
const cmd = new CommandsDB();
let check = await CommandsDB.findOne({ name: name }).select("name").lean();
if (check) return message.reply('Command already exists with that name!');
if (content.includes('|')) {
cmd.id = random(5);
cmd.name = name;
cmd.content = JSON.stringify(content.split('|'));
cmd.createdBy = message.member.user.username;
cmd.save();
return message.reply('Command saved!');
}
cmd.id = random(5);
cmd.name = name;
cmd.content = args.slice(2).join(' ');
cmd.createdBy = message.member.user.username;
cmd.save();
return message.reply('Command saved!');
/*************************************************
* FIND
*************************************************/
} else if (args[0] === 'find') {
let check = await (CommandsDB.findOne({ name: args[1] }).select("id name").lean() || CommandsDB.findOne({ id: args[1] }).select("id name").lean());
if (!check) return message.reply('Command doesn\'t exists with that name or ID!');
CommandsDB.findOne({ name: args[1] }).then((data) => {
let embed = new Embed()
.setDescription(`### Custom Commands: ${data.name}\n**ID**: ${data.id}\n**Content**: ${data.content}\n**Created by**: ${data.createdBy}\n**Created at**: ${moment.unix(data.created_at).format('YYYY/MMM/DD HH:MM:ss')} (UTC)`)
.setColor(`#A52F05`);
return message.reply({ embeds: [embed] }, false);
});
/*************************************************
* DELETE
*************************************************/
} else if (args[0] === 'delete') {
let check = await (CommandsDB.findOne({ name: args[1] }).select("id name").lean() || CommandsDB.findOne({ id: args[1] }).select("id name").lean());
console.log(check)
if (!check) return message.reply('Command doesn\'t exists with that name or id!');
CommandsDB.deleteMany({ id: check.id }).exec();
return message.reply('Command Removed!');
/*************************************************
* SHOW
*************************************************/
} else if (args[0] === 'show') {
// let cmds = await CommandsDB.find({}).select('name').lean();
// console.log(cmds.toString().join(','))
let data = CommandsDB.find({}).select('name').then((data) => {
let names = [];
data.forEach((item) => { names.push(item.name) });
message.reply('DM Sent with all available custom commands.');
message.member.user.openDM().then((dm) => { dm.sendMessage(names.join(', ')) });
});
/*************************************************
* NO ARGS
*************************************************/
} else {
return message.reply(`First argument must be add, delete, edit, show, find or help. You put ${args[0]}`, false)
}
},
};