Rewrite in TypeScript

This commit is contained in:
Ethan Lane 2021-07-14 13:55:36 +01:00
parent 7870e35599
commit 29f80fba5c
32 changed files with 354 additions and 910 deletions

View file

@ -1,64 +0,0 @@
// Required Components
const { Client } = require('discord.js');
const { validateConfig } = require('./validation');
const events = require('./events');
const util = require('./util');
// Required JSON
const expectedConfig = require('../json/expectedConfig.json');
// Client Class
class client extends Client {
constructor(config, commandConfig) {
// Call Discord.JS Client
super();
// Set the client's configuration, initialise events, initialise utilities
this.config = config;
this.commandConfig = commandConfig;
this.events = new events();
this.util = new util(this);
}
// Method to start the bot
start() {
// Check the bot is ready to start
if (!this._config) throw "Config has not been set";
if (!this._commandConfig) throw "Command Config has not been set";
// Events to handle commands
super.on("message", this.events.message);
super.on("ready", this.events.ready);
// Login to discord using Discord.JS
super.login(this._config.token);
// Load events
this.util.loadEvents();
}
// Config
get config() {
return this._config;
}
set config(config) {
// Validate the config
const val = validateConfig(config, expectedConfig);
if (!val.valid) throw val.message;
this._config = config;
}
// Command Config
get commandConfig() {
return this._commandConfig;
}
set commandConfig(config) {
this._commandConfig = config;
}
}
module.exports = client;

32
src/client/client.ts Normal file
View file

@ -0,0 +1,32 @@
import { Client } from "discord.js";
import * as dotenv from "dotenv";
import { Events } from "./events";
import { Util } from "./util";
export class CoreClient extends Client {
private _events: Events;
private _util: Util;
constructor() {
super();
dotenv.config();
this._events = new Events();
this._util = new Util();
}
public start() {
if (!process.env.BOT_TOKEN) throw "BOT_TOKEN is not defined in .env";
if (!process.env.BOT_PREFIX) throw "BOT_PREFIX is not defined in .env";
if (!process.env.FOLDERS_COMMANDS) throw "FOLDERS_COMMANDS is not defined in .env";
if (!process.env.FOLDERS_EVENTS) throw "FOLDERS_EVENTS is not defined in .env";
super.on("message", this._events.onMessage);
super.on("ready", this._events.onReady);
super.login(process.env.BOT_TOKEN);
this._util.loadEvents(this);
}
}

View file

@ -1,42 +0,0 @@
// Events Class
class event {
// Emit when a message is sent
// Used to check for commands
message(message) {
// Make sure command is sent within a guild and not by a bot, otherwise return and ignore
if (!message) return false;
if (!message.guild) return false;
if (message.author.bot) return false;
// Get the prefix from the config
const prefix = this.config.prefix;
// If the message starts with the prefix, then treat it as a command
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
// Get the arguments in the message, after the first space (after the command name)
const args = message.content.substring(prefix.length).split(" ");
const name = args.shift();
// Load the command from the util class
const res = this.util.loadCommand(name, args, message);
if (!res.valid) {
if (res.message != 'File does not exist') throw res.message;
}
return {
"prefix": prefix,
"name": name,
"args": args,
"message": message
};
}
}
// Emit when bot is logged in and ready to use
ready() {
console.log("Ready");
}
}
module.exports = event;

80
src/client/events.ts Normal file
View file

@ -0,0 +1,80 @@
import { Message } from "discord.js";
import { IBaseResponse } from "../contracts/IBaseResponse";
import { Util } from "./util";
export interface IEventResponse extends IBaseResponse {
context?: {
prefix: string;
name: string;
args: string[];
message: Message;
}
}
export class Events {
private _util: Util;
constructor() {
this._util = new Util();
}
// Emit when a message is sent
// Used to check for commands
public onMessage(message: Message): IEventResponse {
if (!message) return {
valid: false,
message: "Message was not supplied.",
};
if (!message.guild) return {
valid: false,
message: "Message was not sent in a guild, ignoring.",
};
if (message.author.bot) return {
valid: false,
message: "Message was sent by a bot, ignoring.",
};
const prefix = process.env.BOT_PREFIX as string;
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
const args = message.content.substring(prefix.length).split(" ");
const name = args.shift();
if (!name) return {
valid: false,
message: "Command name was not found",
};
const res = this._util.loadCommand(name, args, message);
if (!res.valid) {
if (res.message != 'File does not exist') return {
valid: false,
message: res.message,
};
}
return {
valid: true,
context: {
prefix: prefix,
name: name,
args: args,
message: message,
},
};
}
return {
valid: true,
message: "Message was not a command, ignoring.",
}
}
// Emit when bot is logged in and ready to use
public onReady() {
console.log("Ready");
}
}

View file

@ -1,123 +0,0 @@
// Required Components
const { readdirSync, existsSync } = require('fs');
function generateResponse(isValid, message) {
return {
"valid": isValid,
"message": message || "No message was given"
}
}
// Util Class
class util {
constructor(client) {
// Set the client
this._client = client;
}
// Load a command and send the arguments with it
loadCommand(name, args, message) {
// Get the current folder to check
const folder = this._client.config.commands;
// If the folder exists
if (existsSync(`${process.cwd()}/${folder}/`)) {
// If the file exists inside the folder
if (existsSync(`${process.cwd()}/${folder}/${name}.js`)) {
// Require the command file, now that we know it exists and initialise it
const commandFile = require(`${process.cwd()}/${folder}/${name}.js`);
const command = new commandFile();
// Require the command config file and get the config for the current command
const configJson = this._client.commandConfig;
const config = configJson[name];
// Get the list of required configurations the command needs
const commandConfigs = command.configs;
// Loop through all the required configs of the command
for (const i in commandConfigs) {
// If the command doesn't have the configs in the config string, throw an error
if (!config) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`);
if (!config[commandConfigs[i]]) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`);
}
// Get the roles required for this command to run
const requiredRoles = command.roles;
// Get the category, if there is no category, set it to a default string
if (!command.category) command.category = "none";
// Loop through all roles required
for (const i in requiredRoles) {
// If the user doesn't have a required role, don't run the command and let the user know
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) {
message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`);
return generateResponse(false, `You require the \`${requiredRoles[i]}\` role to run this command`);
}
}
// Get the ids of the users that are only permitted to run this command
const users = command.users;
// If the command has any limits, limit the command, otherwise default to anyone
if (users.length > 0) {
if (!users.includes(message.member.id)) {
message.reply(`You do not have permission to run this command`);
return generateResponse(false, "You do not have permission to run this command");
}
}
// Run the command and pass the command context with it
command[command.run]({
"command": name,
"arguments": args,
"client": this._client,
"message": message,
"config": config,
"commandConfigs": commandConfigs
});
return generateResponse(true, `loaded command '${name}' with arguments '${args}'`);
} else {
return generateResponse(false, 'File does not exist');
}
} else {
return generateResponse(false, 'Command folder does not exist');
}
}
// Load the events
loadEvents() {
// Get the current folder to check
const folder = this._client.config.events;
// If the folder exists
if (existsSync(`${process.cwd()}/${folder}/`)) {
// Get the files inside of this folder
const eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
// Loop through all the files in the folder
for (let i = 0; i < eventFiles.length; i++) {
// Ignore non-javascript files
if (eventFiles[i].includes('.js')) {
// Get the event name, by taking the command file and removing the ".js" from the end
const eventName = eventFiles[i].split('.')[0];
// Get the file of the event
const file = require(`${process.cwd()}/${folder}/${eventName}.js`);
// Initialise the event class
const event = new file;
// Set the client to emit to this event
this._client.on(eventName, event[event.run]);
}
}
} else {
return generateResponse(false, 'Event folder does not exist');
}
}
}
module.exports = util;

109
src/client/util.ts Normal file
View file

@ -0,0 +1,109 @@
// Required Components
import { Client, Message } from "discord.js";
import { readdirSync, existsSync } from "fs";
import { IBaseResponse } from "../contracts/IBaseResponse";
import { Command } from "../type/command";
import { Event } from "../type/event";
export interface IUtilResponse extends IBaseResponse {
context?: {
name: string;
args: string[];
message: Message;
}
}
// Util Class
export class Util {
public loadCommand(name: string, args: string[], message: Message): IUtilResponse {
if (!message.member) return {
valid: false,
message: "Member is not part of member",
};
const folder = process.env.FOLDERS_COMMANDS;
if (existsSync(`${process.cwd()}/${folder}/`)) {
if (existsSync(`${process.cwd()}/${folder}/${name}.ts`)) {
const commandFile = require(`${process.cwd()}/${folder}/${name}.ts`);
const command = new commandFile[name]() as Command;
const requiredRoles = command._roles;
if (!command._category) command._category = "none";
for (const i in requiredRoles) {
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) {
message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`);
return {
valid: false,
message: `You require the \`${requiredRoles[i]}\` role to run this command`,
};
}
}
// Run the command and pass the command context with it
command.execute(name, args, message);
return {
valid: true,
context: {
name: name,
args: args,
message: message,
}
}
} else {
return {
valid: false,
message: "File does not exist",
}
}
} else {
return {
valid: false,
message: "Comamnd folder does not exist",
}
}
}
// Load the events
loadEvents(client: Client): IUtilResponse {
// Get the current folder to check
const folder = process.env.FOLDERS_EVENTS;
// If the folder exists
if (existsSync(`${process.cwd()}/${folder}/`)) {
// Get the files inside of this folder
const eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
// Loop through all the files in the folder
for (let i = 0; i < eventFiles.length; i++) {
// Ignore non-javascript files
if (eventFiles[i].includes('.ts')) {
// Get the event name, by taking the command file and removing the ".ts" from the end
const eventName = eventFiles[i].split('.')[0];
// Get the file of the event
const file = require(`${process.cwd()}/${folder}/${eventName}.ts`);
// Initialise the event class
const event = new file() as Event;
// Set the client to emit to this event
client.on(eventName, event.execute);
}
}
return {
valid: true,
}
} else {
return {
valid: false,
message: "Event folder does not exist",
}
}
}
}

View file

@ -1,33 +0,0 @@
function generateResponse(isValid, message) {
return {
"valid": isValid,
"message": message || "No message was given"
}
}
function validateConfig(config, expect) {
if (!config) return generateResponse(false, "Invalid config");
if (typeof config != "object") return generateResponse(false, "Invalid config");
if (!expect) return generateResponse(false, "Invalid expect");
if (typeof expect != "object") return generateResponse(false, "Invalid expect");
const keys = Object.keys(expect);
for (const i in keys) {
const e = expect[keys[i]];
if (!config[keys[i]])
return generateResponse(false, `'${keys[i]}' is not defined`);
if (typeof config[keys[i]] != e.type)
return generateResponse(false, `Invalid type of '${keys[i]}'. Was '${typeof config[keys[i]]}', Expected '${e.type}'`);
}
return generateResponse(true);
}
module.exports = {
generateResponse,
validateConfig
};

View file

@ -0,0 +1,4 @@
export interface IBaseResponse {
valid: boolean;
message?: string;
}

View file

@ -1,5 +0,0 @@
module.exports = {
client: require('./client/client'),
command: require('./type/command'),
event: require('./type/event')
}

3
src/index.ts Normal file
View file

@ -0,0 +1,3 @@
export { CoreClient } from "./client/client";
export { Command } from "./type/command";
export { Event } from "./type/event";

View file

@ -1,14 +0,0 @@
{
"token": {
"type": "string"
},
"prefix": {
"type": "string"
},
"commands": {
"type": "string"
},
"events": {
"type": "string"
}
}

View file

@ -1,75 +0,0 @@
class command {
constructor(run) {
this.run = run;
this._roles = [];
this._configs = [];
this._users = [];
}
// Description
get description() {
return this._description;
}
set description(description) {
this._description = description;
}
// Category
get category() {
return this._category;
}
set category(category) {
this._category = category;
}
// Usage
get usage() {
return this._usage;
}
set usage(usage) {
this._usage = usage;
}
// Roles
get roles() {
return this._roles;
}
set roles(role) {
this._roles.push(role);
}
// Config
get configs() {
return this._configs;
}
set configs(conf) {
this._configs.push(conf);
}
get requiredConfigs() {
console.warn("'requiredConfigs' is deprecated and will be removed in a future version. Please use 'configs' instead.");
return this._configs;
}
set requiredConfigs(conf) {
console.warn("'requiredConfigs' is deprecated and will be removed in a future version. Please use 'configs' instead.");
this._configs.push(conf);
}
// Users
get users() {
return this._users;
}
set users(userid) {
this._users.push(userid);
}
}
module.exports = command;

15
src/type/command.ts Normal file
View file

@ -0,0 +1,15 @@
import { Message } from "discord.js";
export class Command {
public _roles: string[];
public _category?: string;
constructor() {
this._roles = [];
}
public execute(name: string, args: string[], message: Message) {
}
}

View file

@ -1,7 +0,0 @@
class event {
constructor(run) {
this.run = run;
}
}
module.exports = event;

5
src/type/event.ts Normal file
View file

@ -0,0 +1,5 @@
export class Event {
public execute() {
}
}