makecord-create
Version:
Create advanced Discord Bots with Makecord - Now with TypeScript support
149 lines (130 loc) • 4.65 kB
JavaScript
import inquirer from 'inquirer';
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import ora from 'ora';
import gradient from 'gradient-string';
import boxen from 'boxen';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Pretty welcome message
console.log(
boxen(
gradient.pastel.multiline(
'⭐ Makecord Generator ⭐\n' +
'Create commands and events easily'
),
{
padding: 1,
margin: 1,
borderStyle: 'round',
borderColor: 'cyan'
}
)
);
const questions = [
{
type: 'list',
name: 'type',
message: 'What do you want to create?',
choices: ['Slash Command', 'Prefix Command', 'Event'],
default: 'Slash Command'
},
{
type: 'input',
name: 'name',
message: 'Enter the name:',
validate: input => {
if (input.match(/^[a-z0-9-_]+$/)) {
return true;
}
return 'Name can only contain lowercase letters, numbers, hyphens and underscores';
}
},
{
type: 'input',
name: 'description',
message: 'Enter the description:',
when: answers => answers.type !== 'Event'
},
{
type: 'list',
name: 'category',
message: 'Select a category:',
choices: answers => {
const commandType = answers.type === 'Slash Command' ? 'slash' : 'prefix';
const categoriesPath = path.join(__dirname, 'commands', commandType);
// Create base directories if they don't exist
if (!fs.existsSync(categoriesPath)) {
fs.mkdirSync(categoriesPath, { recursive: true });
return ['general']; // Default category
}
const categories = fs.readdirSync(categoriesPath)
.filter(file => fs.statSync(path.join(categoriesPath, file)).isDirectory());
return categories.length > 0 ? categories : ['general'];
},
when: answers => answers.type !== 'Event'
}
];
async function generate() {
try {
const answers = await inquirer.prompt(questions);
const spinner = ora('Generating files...').start();
if (answers.type === 'Event') {
const eventTemplate = `import chalk from 'chalk';
export default {
name: '${answers.name}',
once: false,
execute(...args) {
console.log(chalk.blue('[EVENT] ' + this.name + ' was triggered'));
// Your event code here
}
};`;
const eventsPath = path.join(__dirname, 'events');
if (!fs.existsSync(eventsPath)) {
fs.mkdirSync(eventsPath, { recursive: true });
}
fs.writeFileSync(
path.join(eventsPath, `${answers.name}.js`),
eventTemplate
);
spinner.succeed(`Event "${answers.name}" created successfully!`);
} else {
const isSlash = answers.type === 'Slash Command';
const commandTemplate = isSlash
? `import { SlashCommandBuilder } from 'discord.js';
export default {
data: new SlashCommandBuilder()
.setName('${answers.name}')
.setDescription('${answers.description}'),
async execute(interaction) {
await interaction.reply('Command executed successfully!');
}
};`
: `export default {
name: '${answers.name}',
description: '${answers.description}',
async execute(message, args) {
await message.reply('Command executed successfully!');
}
};`;
const commandType = isSlash ? 'slash' : 'prefix';
const categoryPath = path.join(__dirname, 'commands', commandType, answers.category);
if (!fs.existsSync(categoryPath)) {
fs.mkdirSync(categoryPath, { recursive: true });
}
fs.writeFileSync(
path.join(categoryPath, `${answers.name}.js`),
commandTemplate
);
spinner.succeed(`${answers.type} "${answers.name}" created in category "${answers.category}"!`);
}
console.log(gradient.pastel('\n⭐ Happy Coding! ⭐'));
} catch (error) {
console.error(chalk.red('An error occurred:'), error);
process.exit(1);
}
}
generate();