Skip to content

Commit

Permalink
Made slash commands toggleable.
Browse files Browse the repository at this point in the history
and fix double apostrophy in the wrong places
  • Loading branch information
Coosanta17 committed Jul 6, 2024
1 parent 8059ced commit 7296f74
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 41 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Coosanta"s License (pls read if you want to do anything with the software!!)
Coosanta's License (pls read if you want to do anything with the software!!)

Copyright (c) 2024 Coosanta

Expand Down
4 changes: 2 additions & 2 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActivityType, REST, Routes, Events } from "discord.js";
import { checkConfigFile, config } from "./config.js";
await checkConfigFile();
// Import and run checkConfigFile() before all other imports to set config variable.
// If this isn"t done, it will crash.
// If this isn't done, it will crash.

const { discordClient } = await import("./api/client.js");
const { serverStart } = await import("./api/start_server.js");
Expand Down Expand Up @@ -31,7 +31,7 @@ discordClient.on("ready", async (c) => {

discordClient.on("messageCreate", async (message) => {
if (!message.content.startsWith(config.commands.text.prefix) || !config.commands.text.enabled) {
return; // Exit early if the message doesn"t start with the prefix or text commands are disabled.
return; // Exit early if the message doesn't start with the prefix or text commands are disabled.
}
// only text commands beyond this point
const command = message.content.substring(1).toLowerCase();
Expand Down
12 changes: 11 additions & 1 deletion src/commands/command_handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fileURLToPath, pathToFileURL } from "url";
import path from "path";
import fs from "fs/promises";
import { fileURLToPath, pathToFileURL } from "url";
import { commandsDisabled } from "./functions/disabled_commands.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -48,3 +49,12 @@ export async function getCommands() {
}
return commandsCache;
}

export async function handleCommand(interaction, commandName, executeFunction) {
const areCommandsDisabled = commandsDisabled(commandName);
if (areCommandsDisabled) {
await interaction.reply({ content: areCommandsDisabled, ephemeral: true });
return;
}
await executeFunction(interaction);
}
6 changes: 3 additions & 3 deletions src/commands/functions/disabled_commands.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { config } from "../../config.js";

export function commandsDisabled(command) {
if (!config.slash.enabled) {
if (!config.commands.slash.enabled) {
let disabledResponse;

if (!config.text.enabled) { // Idiot proofing
if (!config.commands.text.enabled) { // Idiot proofing
disabledResponse = "Commands are disabled! :sob: \nTry setting one of the command values to `true` in `config.json` to enable commands.";
return;
}

disabledResponse = `Slash commands are disabled.\nTry \`${config.text.prefix}${command}\` instead`
disabledResponse = `Slash commands are disabled.\nTry \`${config.commands.text.prefix}${command}\` instead`
return disabledResponse;
}
}
16 changes: 0 additions & 16 deletions src/commands/functions/help_reponse.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/commands/functions/help_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function help(prefix) {
.addFields(
{ name: "START", value: `*Starts the server.*\nUsage: \`${prefix}start\`` },
{ name: "HELP", value: `*Shows the available commands.*\nUsage: \`${prefix}help\`` },
{ name: "That"s all for now.", value: "*New features will be added soon!*" }
{ name: "That's all for now.", value: "*New features will be added soon!*" }
)
// .setTimestamp();

Expand Down
16 changes: 6 additions & 10 deletions src/commands/registry/help.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { SlashCommandBuilder } from "discord.js";
import { help } from "../functions/help_reponse.js";
import { commandsDisabled } from "../functions/disabled_commands.js";
import { help } from "../functions/help_response.js";
import { handleCommand } from "../command_handler.js";

export default {
data: new SlashCommandBuilder()
.setName("help")
.setDescription("Shows the available commands."),
async execute(interaction) {
let areCommandsDisabled = commandsDisabled("help");
if (areCommandsDisabled) {
await interaction.reply(areCommandsDisabled, { ephemeral: true });
return;
}

await interaction.reply(help("/"));
await handleCommand(interaction, "help", async (interaction) => {
await interaction.reply(help("/"));
});
},
}
};
11 changes: 4 additions & 7 deletions src/commands/registry/start.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { SlashCommandBuilder } from "discord.js";
import { serverStart } from "../../api/start_server.js";
import { commandsDisabled } from "../functions/disabled_commands.js";
import { handleCommand } from "../command_handler.js";

export default {
data: new SlashCommandBuilder()
.setName("start")
.setDescription("Starts the server."),
async execute(interaction) {
const areCommandsDisabled = commandsDisabled("start");
if (areCommandsDisabled) {
interaction.reply(areCommandsDisabled, { ephemeral: true });
return;
}
await interaction.reply(await serverStart());
await handleCommand(interaction, "start", async (interaction) => {
await interaction.reply(await serverStart());
});
},
};

0 comments on commit 7296f74

Please sign in to comment.