Skip to content

Commit

Permalink
AutoCommit: Bump Repository (Update Repository Files)
Browse files Browse the repository at this point in the history
  • Loading branch information
thealternatedev committed Oct 17, 2023
1 parent ac2ae92 commit 8c95728
Show file tree
Hide file tree
Showing 21 changed files with 318 additions and 18 deletions.
44 changes: 44 additions & 0 deletions bin/CommandBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class CommandBuilder {

static createBuilder(commandName) {
return new CommandBuilder(commandName);
}

constructor(name) {
this.name = name;
this.args = [];
this.description = "";
this.callback = () => {};
}

setName(name) {
this.name = name;
return this;
}

setArguments(args) {
this.args = args;
return this;
}

setDescription(description) {
this.description = description;
return this;
}

setCallback(callback) {
this.callback = callback;
return this;
}

toJSON() {
return {
name: this.name,
args: this.args,
description: this.description,
callback: this.callback
};
}
}

module.exports = CommandBuilder;
34 changes: 34 additions & 0 deletions bin/CommandsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { Collection } = require("discord.js");

function RegisterCommand(program, data) {

const { name, args, description, callback } = data;


if (!description) {

}

if (!callback) {

}

let l = program.command(name)
.description(description)
.action(callback);

if (args && args.length) {
for (const arg of args) {
l.argument(arg);
}
}
}

function RegisterJSONCommand(program, builder) {
RegisterCommand(program, builder.toJSON());
}

module.exports = {
RegisterCommand,
RegisterJSONCommand
}
56 changes: 56 additions & 0 deletions bin/commands/InitCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const CommandBuilder = require("../CommandBuilder");
const Nanospinner = require("nanospinner");
const spinner = Nanospinner.createSpinner();
const InitializationTools = require('../../globals/InitializationUtils');

const maxSteps = 4;
let currentStep = 1;

function startStep(text) {
spinner.start({
text: `${text} (${currentStep}/${maxSteps})`
});
}

function step(text) {
currentStep++;
if (currentStep >= maxSteps)
return end(text, true);
spinner.update({
text: `${text} (${currentStep}/${maxSteps})`
});
}

function end(text, success) {

if (success) {
spinner.success({
text: `${text} (${currentStep}/${maxSteps})`
});
} else {
spinner.stop({
text: `${text} (${currentStep}/${maxSteps})`
});
}

}

module.exports = () => CommandBuilder.createBuilder("init")
.setDescription("Creates a new Redactcord project")
.setCallback(() => {

startStep("Creating Directories...");

InitializationTools.makeDirectories();

step("Creating Files");

InitializationTools.writeFiles();

step("Creating configuraitons");

InitializationTools.createConfigurationFiles();

step("Done! Created All Files");

});
8 changes: 8 additions & 0 deletions bin/commands/RegisterCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { RegisterCommand, RegisterJSONCommand } = require("../CommandsHandler");
const InitCommand = require("./InitCommand");
const VersionCommand = require("./VersionCommand")

module.exports = (program) => {
RegisterJSONCommand(program, VersionCommand());
RegisterJSONCommand(program, InitCommand());
}
9 changes: 9 additions & 0 deletions bin/commands/VersionCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const CommandBuilder = require("../CommandBuilder")

module.exports = () => {
return CommandBuilder.createBuilder("version")
.setDescription("Displays CLI Version")
.setCallback(() => {
console.log(require("../../package").version);
});
}
17 changes: 17 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { program } = require("commander");
const { getCommands, ExecuteCommand } = require("./CommandsHandler");
const RegisterCommands = require("./commands/RegisterCommands");

async function init() {
await RegisterCommands(program);


}

function run() {
program.parse();
}

init().then(_ => {
run();
})
9 changes: 9 additions & 0 deletions globals/CommandLine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { program } = require("commander");

function command(name, opts) {
return program.command(name, opts);
}

module.exports = {
command,
}
66 changes: 66 additions & 0 deletions globals/InitializationUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { writeFileSync, mkdirSync } = require("fs");
const path = require("path");

const configName = "redact.config.json";
const environmentName = ".redact.env";
const files = [
environmentName,
"src/index.js",
];
const directories = [
"src",
"commands",
"events",
];

const packageConfig = {
name: path.basename(path.dirname(process.cwd())),
version: "1.0.0",
description: "Generated Bot Project by Redactcord",
main: "src/index.js",
scripts: {
test: "echo No Test script was found & exit 0"
},
license: "MIT",
author: "",
keywords: ["Bot"]
};

const redactconfig = {
$schema: "https://cdn.redact.tools/libs/redactcord/RedactcordConfig.schema.json",
main: "src/index.js",
suspendCommandsLoading: false,
suspendEventsLoading: false,
eventsFolderPath: "events",
commandsFolderPath: "commands",
allowEnvironmentLoading: true,
commands: []
};

function toJson(data) {
return JSON.stringify(data, null, 4);
}

function createConfigurationFiles() {
writeFileSync(path.join(process.cwd(), "package.json"), toJson(packageConfig));
writeFileSync(path.join(process.cwd(), "redactcord.config.json"), toJson(redactconfig));
}

function writeFiles() {
for (const file of files) {
writeFileSync(path.join(process.cwd(), file), "");
}
}

function makeDirectories() {
for (const directory of directories) {
mkdirSync(path.join(process.cwd(), directory), { recursive: true });
}
}

module.exports = {
createConfigurationFiles,
writeFiles,
toJson,
makeDirectories
}
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"commander": "^11.1.0",
"discord.js": "^14.13.0",
"dotenv": "^16.3.1",
"immutable": "^5.0.0-beta.4",
"inquirer": "^9.2.11",
"lodash": "^4.17.21",
"nanospinner": "^1.1.0",
Expand Down
8 changes: 8 additions & 0 deletions src/client/RedactClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class RedactClient {
private startedSpinner: boolean = false;
private commandsManager: CommandsManager;
private eventManager: EventManager;
private readyEvent?: () => void;

constructor(options: RedactOptions) {
this.client = new Client(options);
Expand Down Expand Up @@ -51,6 +52,10 @@ export class RedactClient {
return true;
}

public onReadyEvent(event: () => void) {
this.readyEvent = event;
}

public startOrUpdate(spinnerOptions?: NanoSpinner.Options) {
if (this.isSpinnerStarted())
{
Expand Down Expand Up @@ -130,6 +135,9 @@ export class RedactClient {
this.endSpinner({
text: "Logged in as: " + this.getBotUsername()
}, true);

if (this.readyEvent)
this.readyEvent();
});

this.endSpinner({
Expand Down
7 changes: 7 additions & 0 deletions src/client/commands/CommandsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ export class CommandsManager extends Loader<RedactCommand> {

private redactClient: RedactClient;
private commands: Collection<string, RedactCommand> = new Collection();
private registeredCommandNames: string[];

constructor(redactClient: RedactClient) {
super();
this.redactClient = redactClient;
let v = this.redactClient.getRedactConfig().getArray<string>("commands");
if (!v)
v = [];
this.registeredCommandNames = v;
}

public registerCommand(command: RedactCommand) {
Expand Down Expand Up @@ -58,6 +63,8 @@ export class CommandsManager extends Loader<RedactCommand> {
public loadAndCall(commandFolderPath: string, callback: (command: RedactCommand) => void) {
const commands = this.loadFrom(commandFolderPath, false);
for (const command of commands) {
if (!this.registeredCommandNames.includes(command.getCommandData().getName()))
throw new RedactError("Command Loader Error", "Couldn't Register command \"" + command.getCommandData().getName() + "\" because it's not found in redact.config.json commands array");
callback(command);
this.registerCommand(command);
}
Expand Down
Loading

0 comments on commit 8c95728

Please sign in to comment.