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 21, 2023
1 parent 00d7f23 commit 0552ddb
Show file tree
Hide file tree
Showing 13 changed files with 397 additions and 7 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

Fast And Powerful Discord.js Bot Builder to SImplify and Beautify your discord bot with many features

# Documentation
Check out https://js.redact.tools for more info about redactcord or it's documentation

## Features

These are the features included into the package or will be included in the meantime:

1. [x] Command System
1. [x] Event System
1. [x] Configuration System
1. [] CLI & Files Watcher (WIP)
1. [] Database System (WIP)
1. [x] CLI (WIP)
1. [x] Database System
1. [] Error Handler (WIP)
1. [] Economy System (Planned)
1. [] Music System (Planned). Note: Using the music system, Youtube api is going to be removed from the music system. (To not get banned of course)
Expand Down
2 changes: 2 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

const { program } = require("commander");
const { getCommands, ExecuteCommand } = require("./CommandsHandler");
const RegisterCommands = require("./commands/RegisterCommands");
Expand Down
22 changes: 22 additions & 0 deletions globals/InitializationUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { execSync } = require("child_process");
const { writeFileSync, mkdirSync } = require("fs");
const path = require("path");
const yaml = require('yaml');

const configName = "redactcord.config.json";
const environmentName = ".redact.env";
Expand Down Expand Up @@ -41,13 +42,34 @@ const redactconfig = {
commands: []
};

const redactDatabaseConfig = {
enabled: false,
type: "flat",
flat: {
filePath: "example.db"
},
mongo: {
url: "mongo:?/"
},
postgre: {
url: "postgres://"
},
mysql: {
host: "127.0.0.1",
user: "root",
database: "example",
password: "password"
}
}

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(), configName), toJson(redactconfig));
writeFileSync(path.join(process.cwd(), "redact.database.yml"), yaml.stringify(redactDatabaseConfig));
}

function writeFiles() {
Expand Down
11 changes: 10 additions & 1 deletion package-lock.json

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

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
"description": "Fast and Powerful Discord.js Utilities to make simple and good looking bots",
"main": "out/index.js",
"typings": "./typings/index.d.ts",
"bin": "./bin/index.js",
"bin": {
"redactcord": "bin/index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"watch": "tsc --watch",
"build-watch": "npm run build & npm run watch",
"start": "node bin/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/redactDev/Redactcord"
},
"keywords": [],
"author": "Redact Team",
"license": "MIT",
Expand All @@ -37,6 +43,7 @@
"quickmongo": "^5.2.0",
"quickpostgres": "^3.0.2",
"typescript": "^5.2.2",
"watch": "^1.0.2"
"watch": "^1.0.2",
"yaml": "^2.3.3"
}
}
10 changes: 10 additions & 0 deletions src/client/RedactClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EventManager } from "./events/EventManager";
import path from "path";
import { Logger } from "../logger/Logger";
import Color from "cli-color";
import { RedactErrorHandler } from "./errors/RedactErrorHandler";

type RedactOptions = ClientOptions & {
token: string;
Expand All @@ -25,6 +26,7 @@ export class RedactClient {
private eventManager: EventManager;
private readyEvent?: () => void;
private logger: Logger = Logger.getLogger();
private errorHandler?: RedactErrorHandler;

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

public initErrorHandler(errorHandler: RedactErrorHandler) {
this.errorHandler = errorHandler;
}

public getErrorHandler(): RedactErrorHandler | undefined {
return this.errorHandler;
}

public onReadyEvent(event: () => void) {
this.readyEvent = event;
}
Expand Down
76 changes: 76 additions & 0 deletions src/client/errors/RedactErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ChannelType, EmbedBuilder, EmbedField } from "discord.js";
import { RedactClient } from "../RedactClient";

export class RedactErrorHandler {

private redactClient: RedactClient;
private guildId: string;
private channelId: string;
private description: string;
private errorField: EmbedField;
private dateField: EmbedField;

constructor(redactClient: RedactClient, guildId: string, channelId: string) {
this.redactClient = redactClient;
this.guildId = guildId;
this.channelId = channelId;
this.description = "An error occured while trying to run the bot.";
this.errorField = {
name: "Error",
value: "%error_value%",
inline: true
};
this.dateField = {
name: "Date",
value: "%date%",
inline: true
};
}

setDescription(description: string) {
this.description = description;
}

getDescription(): string {
return this.description;
}

setErrorField(field: EmbedField) {
this.errorField = field;
}

getErrorField(): EmbedField {
return this.errorField;
}

setDateField(field: EmbedField) {
this.dateField = field;
}

getDateField(): EmbedField {
return this.dateField;
}

async reportError(error: Error) {
const stack = error.stack ? error.stack : "";
this.errorField.value = this.errorField.value.replace("%error_value%", `\`\`\`${stack}\`\`\``);
this.dateField.value = this.dateField.value.replace("%date%", new Date().toUTCString())
const embed = new EmbedBuilder()
.setDescription(this.description)
.setFields(this.errorField, this.dateField);

const guild = this.redactClient.getClient().guilds.cache.get(this.guildId);
if (!guild)
throw error;
const channel = await guild.channels.fetch(this.channelId);
if (!channel)
throw error;
if (!channel.isTextBased() || channel.type !== ChannelType.GuildText)
throw error;
channel.send({
embeds: [embed]
});
return true;
}

}
39 changes: 38 additions & 1 deletion src/configuration/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CollectionConstructor } from "@discordjs/collection";
import { Collection } from "discord.js";
import { readFileSync, writeFileSync } from "fs";
import { existsSync, readFileSync, writeFileSync } from "fs";
import lodash from "lodash";
import yaml from "yaml";

export abstract class Configuration extends Collection<string, any> {

Expand Down Expand Up @@ -76,6 +78,14 @@ export abstract class Configuration extends Collection<string, any> {
return value;
}

updateData(data: Record<string, any>) {
this.content = data;
this.clear();
for (const [key, value] of Object.entries(data)) {
this.set(key, value);
}
}

public abstract save(): void;

toJson(): Record<string, any> {
Expand Down Expand Up @@ -119,4 +129,31 @@ export class FullConfiguration extends Configuration {
public save(): void {
writeFileSync(this.filePath, JSON.stringify(this.toJson(), null, 4));
}
}

export class YamlConfiguration extends Configuration {

constructor(filePath: string) {
super(filePath, {}, true);
let data;
if (existsSync(filePath))
{
data = yaml.parse(readFileSync(filePath, "utf-8"));
if (!Object.keys(data).length)
this.empty = true;
else
this.empty = false;
}
else
{
this.empty = true;
data = {};
}
this.updateData(data);
}

public save(): void {
writeFileSync(this.filePath, yaml.stringify(this.toJson()));
}

}
Loading

0 comments on commit 0552ddb

Please sign in to comment.