Skip to content

Commit

Permalink
Chore: lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ballaual committed May 28, 2023
1 parent 6c66233 commit 6ac023d
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 354 deletions.
207 changes: 106 additions & 101 deletions commands/core/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,126 +6,131 @@ const { reloadGuildCommands } = require('../../handlers/deployCommands');
const { PermissionFlagsBits } = require('discord.js');

async function loadModules(guildId) {
try {
const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`);
const fileExists = await fs.promises.access(guildModulesFilePath).then(() => true).catch(() => false);
if (fileExists) {
const data = await fs.promises.readFile(guildModulesFilePath, 'utf8');
return JSON.parse(data);
} else {
return {};
}
} catch (error) {
console.error('Error loading modules:', error);
return {};
}
try {
const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`);
const fileExists = await fs.promises.access(guildModulesFilePath).then(() => true).catch(() => false);
if (fileExists) {
const data = await fs.promises.readFile(guildModulesFilePath, 'utf8');
return JSON.parse(data);
}
else {
return {};
}
}
catch (error) {
console.error('Error loading modules:', error);
return {};
}
}

async function saveModules(guildId, modules) {
try {
const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`);
await fs.promises.writeFile(guildModulesFilePath, JSON.stringify(modules, null, 2));
} catch (error) {
console.error('Error saving modules:', error);
}
try {
const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`);
await fs.promises.writeFile(guildModulesFilePath, JSON.stringify(modules, null, 2));
}
catch (error) {
console.error('Error saving modules:', error);
}
}

function getModuleChoices() {
const commandsFolderPath = path.join(__dirname, '..', '..', 'commands');
const moduleChoices = [];
const commandsFolderPath = path.join(__dirname, '..', '..', 'commands');
const moduleChoices = [];

fs.readdirSync(commandsFolderPath).forEach(folder => {
const moduleFolderPath = path.join(commandsFolderPath, folder);
if (fs.statSync(moduleFolderPath).isDirectory()) {
const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js'));
if (commandFiles.length > 0 && folder !== 'core') {
moduleChoices.push({
name: folder.charAt(0).toUpperCase() + folder.slice(1),
value: folder,
});
}
}
});
fs.readdirSync(commandsFolderPath).forEach(folder => {
const moduleFolderPath = path.join(commandsFolderPath, folder);
if (fs.statSync(moduleFolderPath).isDirectory()) {
const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js'));
if (commandFiles.length > 0 && folder !== 'core') {
moduleChoices.push({
name: folder.charAt(0).toUpperCase() + folder.slice(1),
value: folder,
});
}
}
});

return moduleChoices;
return moduleChoices;
}

function createGuildModules(guildId) {
const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`);
if (!fs.existsSync(guildModulesFolderPath)) {
fs.mkdirSync(guildModulesFolderPath, { recursive: true });
}
if (!fs.existsSync(guildModulesFilePath)) {
fs.writeFileSync(guildModulesFilePath, JSON.stringify({}, null, 2));
}
const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`);
if (!fs.existsSync(guildModulesFolderPath)) {
fs.mkdirSync(guildModulesFolderPath, { recursive: true });
}
if (!fs.existsSync(guildModulesFilePath)) {
fs.writeFileSync(guildModulesFilePath, JSON.stringify({}, null, 2));
}
}

module.exports = {
cooldown: 180,
data: new SlashCommandBuilder()
.setName('module')
.setDescription('Enable, disable, or show modules')
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addSubcommand(subcommand =>
subcommand
.setName('add')
.setDescription('Enable a module')
.addStringOption(option =>
option.setName('module')
.setDescription('Module to enable')
.setRequired(true)
.addChoices(...getModuleChoices())
)
)
.addSubcommand(subcommand =>
subcommand
.setName('remove')
.setDescription('Disable a module')
.addStringOption(option =>
option.setName('module')
.setDescription('Module to disable')
.setRequired(true)
.addChoices(...getModuleChoices())
)
)
.addSubcommand(subcommand =>
subcommand
.setName('show')
.setDescription('Show active modules for the guild')
),
category: 'core',
async execute(interaction) {
const subcommand = interaction.options.getSubcommand();
const guildId = interaction.guildId;
const modules = await loadModules(guildId); // Load modules asynchronously
cooldown: 180,
data: new SlashCommandBuilder()
.setName('module')
.setDescription('Enable, disable, or show modules')
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addSubcommand(subcommand =>
subcommand
.setName('add')
.setDescription('Enable a module')
.addStringOption(option =>
option.setName('module')
.setDescription('Module to enable')
.setRequired(true)
.addChoices(...getModuleChoices()),
),
)
.addSubcommand(subcommand =>
subcommand
.setName('remove')
.setDescription('Disable a module')
.addStringOption(option =>
option.setName('module')
.setDescription('Module to disable')
.setRequired(true)
.addChoices(...getModuleChoices()),
),
)
.addSubcommand(subcommand =>
subcommand
.setName('show')
.setDescription('Show active modules for the guild'),
),
category: 'core',
async execute(interaction) {
const subcommand = interaction.options.getSubcommand();
const guildId = interaction.guildId;
const modules = await loadModules(guildId);

createGuildModules(guildId);
createGuildModules(guildId);

if (subcommand === 'add') {
const module = interaction.options.getString('module');
if (subcommand === 'add') {
const module = interaction.options.getString('module');

if (module === 'core') {
interaction.reply('The core module cannot be enabled or disabled per guild.');
return;
}
if (module === 'core') {
interaction.reply('The core module cannot be enabled or disabled per guild.');
return;
}

modules[module] = true;
await saveModules(guildId, modules); // Save modules before deploying commands
reloadGuildCommands(guildId); // Deploy commands after saving modules
modules[module] = true;
await saveModules(guildId, modules);
reloadGuildCommands(guildId);

interaction.reply({ content: `Module \`${module}\` has been enabled for this guild.`, ephemeral: true });
} else if (subcommand === 'remove') {
const module = interaction.options.getString('module');
interaction.reply({ content: `Module \`${module}\` has been enabled for this guild.`, ephemeral: true });
}
else if (subcommand === 'remove') {
const module = interaction.options.getString('module');

delete modules[module];
await saveModules(guildId, modules); // Save modules before deploying commands
reloadGuildCommands(guildId); // Deploy commands after saving modules
delete modules[module];
await saveModules(guildId, modules);
reloadGuildCommands(guildId);

interaction.reply({ content: `Module \`${module}\` has been disabled for this guild.`, ephemeral: true });
} else if (subcommand === 'show') {
const activeModules = Object.keys(modules).filter(key => modules[key]).join(', ') || '---';
interaction.reply({ content: `Active modules for Guild ID ${guildId}: ${activeModules}`, ephemeral: true });
}
},
interaction.reply({ content: `Module \`${module}\` has been disabled for this guild.`, ephemeral: true });
}
else if (subcommand === 'show') {
const activeModules = Object.keys(modules).filter(key => modules[key]).join(', ') || '---';
interaction.reply({ content: `Active modules for Guild ID ${guildId}: ${activeModules}`, ephemeral: true });
}
},
};
51 changes: 26 additions & 25 deletions commands/core/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ const { ownerId } = require('../../config/config.json');
const deployCommands = require('../../handlers/deployCommands');

module.exports = {
cooldown: 180,
data: new SlashCommandBuilder()
.setName('reload')
.setDescription('Reloads all commands.')
.setDMPermission(false),
category: 'core',
async execute(interaction) {
if (interaction.member.id !== ownerId) {
return interaction.reply({
content: 'Only the bot owner is allowed to use this command!',
ephemeral: true,
});
}
cooldown: 180,
data: new SlashCommandBuilder()
.setName('reload')
.setDescription('Reloads all commands.')
.setDMPermission(false),
category: 'core',
async execute(interaction) {
if (interaction.member.id !== ownerId) {
return interaction.reply({
content: 'Only the bot owner is allowed to use this command!',
ephemeral: true,
});
}

try {
await interaction.deferReply({ ephemeral: true });
setTimeout(async () => {
await interaction.editReply('Reloading commands...');
deployCommands();
await interaction.editReply('All commands have been reloaded!');
}, 5000);
} catch (error) {
console.error(error);
await interaction.editReply(`There was an error while reloading commands:\n\`${error.message}\``);
}
},
try {
await interaction.deferReply({ ephemeral: true });
setTimeout(async () => {
await interaction.editReply('Reloading commands...');
deployCommands();
await interaction.editReply('All commands have been reloaded!');
}, 5000);
}
catch (error) {
console.error(error);
await interaction.editReply(`There was an error while reloading commands:\n\`${error.message}\``);
}
},
};
4 changes: 2 additions & 2 deletions commands/dvc/dvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ module.exports = {
channel.delete();
}

await interaction.reply({ content:'Voice creator successfully removed.', ephemeral: true});
await interaction.reply({ content:'Voice creator successfully removed.', ephemeral: true });
}
}
catch (error) {
console.error(error);
await interaction.reply({ content:'An internal error occured.', ephemeral: true});
await interaction.reply({ content:'An internal error occured.', ephemeral: true });
}
},
};
36 changes: 18 additions & 18 deletions handlers/commandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ const path = require('node:path');
const { Collection } = require('discord.js');

const handleCommands = (client) => {
client.commands = new Collection();
client.cooldowns = new Collection();
const foldersPath = path.join(__dirname, '..', 'commands');
const commandFolders = fs.readdirSync(foldersPath);
client.commands = new Collection();
client.cooldowns = new Collection();
const foldersPath = path.join(__dirname, '..', 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
}
else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
}
else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
};

module.exports = handleCommands;
Loading

0 comments on commit 6ac023d

Please sign in to comment.