Feature/48 database #114

Merged
Vylpes merged 12 commits from feature/48-database into develop 2022-03-29 18:19:54 +01:00
17 changed files with 111 additions and 20 deletions
Showing only changes of commit 1387200c00 - Show all commits

View file

@ -6,6 +6,7 @@ commands.disabled.message: The message to show when a disabled command is ran (D
role.assignable: List of roles assignable to user (Default: []) role.assignable: List of roles assignable to user (Default: [])
role.moderator: The moderator role name (Default: "Moderator") role.moderator: The moderator role name (Default: "Moderator")
role.administrator: The administrator role name (Default: "Administrator")
role.muted: The muted role name (Default: "Muted") role.muted: The muted role name (Default: "Muted")
rules.file: The location of the rules file (Default: "data/rules/rules") rules.file: The location of the rules file (Default: "data/rules/rules")

View file

@ -4,7 +4,7 @@
"port": 3306, "port": 3306,
"username": "dev", "username": "dev",
"password": "dev", "password": "dev",
"database": "droplet", "database": "vylbot",
"synchronize": true, "synchronize": true,
"logging": false, "logging": false,
"entities": [ "entities": [

View file

@ -21,7 +21,7 @@ export class Events {
// Emit when a message is sent // Emit when a message is sent
// Used to check for commands // Used to check for commands
public onMessage(message: Message, commands: ICommandItem[]): IEventResponse { public async onMessage(message: Message, commands: ICommandItem[]): Promise<IEventResponse> {
if (!message.guild) return { if (!message.guild) return {
valid: false, valid: false,
message: "Message was not sent in a guild, ignoring.", message: "Message was not sent in a guild, ignoring.",
@ -43,7 +43,7 @@ export class Events {
message: "Command name was not found", message: "Command name was not found",
}; };
const res = this._util.loadCommand(name, args, message, commands); const res = await this._util.loadCommand(name, args, message, commands);
if (!res.valid) { if (!res.valid) {
return { return {

View file

@ -7,6 +7,10 @@ import { Event } from "../type/event";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
import ICommandItem from "../contracts/ICommandItem"; import ICommandItem from "../contracts/ICommandItem";
import IEventItem from "../contracts/IEventItem"; import IEventItem from "../contracts/IEventItem";
import SettingsHelper from "../helpers/SettingsHelper";
import StringTools from "../helpers/StringTools";
import { CommandResponse } from "../constants/CommandResponse";
import ErrorMessages from "../constants/ErrorMessages";
export interface IUtilResponse extends IBaseResponse { export interface IUtilResponse extends IBaseResponse {
context?: { context?: {
@ -18,7 +22,7 @@ export interface IUtilResponse extends IBaseResponse {
// Util Class // Util Class
export class Util { export class Util {
public loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]): IUtilResponse { public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]): Promise<IUtilResponse> {
if (!message.member) return { if (!message.member) return {
valid: false, valid: false,
message: "Member is not part of message", message: "Member is not part of message",
@ -51,13 +55,26 @@ export class Util {
const requiredRoles = item.Command._roles; const requiredRoles = item.Command._roles;
for (const i in requiredRoles) { for (const i in requiredRoles) {
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) { if (message.guild) {
message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`); const setting = await SettingsHelper.GetSetting(`role.${requiredRoles[i]}`, message.guild?.id);
return { if (!setting) {
valid: false, message.reply("Unable to verify if you have this role, please contact your bot administrator");
message: `You require the \`${requiredRoles[i]}\` role to run this command`
}; return {
valid: false,
message: "Unable to verify if you have this role, please contact your bot administrator"
};
}
if (!message.member.roles.cache.find(role => role.name == setting)) {
message.reply(`You require the \`${StringTools.Capitalise(setting)}\` role to run this command`);
return {
valid: false,
message: `You require the \`${StringTools.Capitalise(setting)}\` role to run this command`
};
}
} }
} }
@ -67,6 +84,27 @@ export class Util {
message: message message: message
}; };
const precheckResponse = item.Command.precheck(context);
const precheckAsyncResponse = await item.Command.precheckAsync(context);
if (precheckResponse != CommandResponse.Ok) {
message.reply(ErrorMessages.GetErrorMessage(precheckResponse));
return {
valid: false,
message: ErrorMessages.GetErrorMessage(precheckResponse)
};
}
if (precheckAsyncResponse != CommandResponse.Ok) {
message.reply(ErrorMessages.GetErrorMessage(precheckAsyncResponse));
return {
valid: false,
message: ErrorMessages.GetErrorMessage(precheckAsyncResponse)
};
}
item.Command.execute(context); item.Command.execute(context);
return { return {

View file

@ -12,7 +12,7 @@ export default class Ban extends Command {
super._category = "Moderation"; super._category = "Moderation";
super._roles = [ super._roles = [
process.env.ROLES_MODERATOR! "moderator"
]; ];
} }

View file

@ -11,7 +11,7 @@ export default class Clear extends Command {
super._category = "Moderation"; super._category = "Moderation";
super._roles = [ super._roles = [
process.env.ROLES_MODERATOR! "moderator"
]; ];
} }

View file

@ -1,5 +1,6 @@
import { Guild } from "discord.js"; import { Guild } from "discord.js";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { CommandResponse } from "../constants/CommandResponse";
import DefaultValues from "../constants/DefaultValues"; import DefaultValues from "../constants/DefaultValues";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
import ICommandReturnContext from "../contracts/ICommandReturnContext"; import ICommandReturnContext from "../contracts/ICommandReturnContext";
@ -13,6 +14,25 @@ export default class Config extends Command {
constructor() { constructor() {
super(); super();
super._category = "Administration"; super._category = "Administration";
super._roles = [
"administrator"
]
}
public override async precheckAsync(context: ICommandContext): Promise<CommandResponse> {
if (!context.message.guild) {
return CommandResponse.ServerNotSetup;
}
const server = await Server.FetchOneById<Server>(Server, context.message.guild?.id, [
"Settings",
]);
if (!server) {
return CommandResponse.ServerNotSetup;
}
return CommandResponse.Ok;
} }
public override async execute(context: ICommandContext) { public override async execute(context: ICommandContext) {
@ -25,8 +45,6 @@ export default class Config extends Command {
]); ]);
if (!server) { if (!server) {
const embed = new ErrorEmbed(context, "This server hasn't been setup yet, please run the setup command");
embed.SendToCurrentChannel();
return; return;
} }

View file

@ -12,7 +12,7 @@ export default class Kick extends Command {
super._category = "Moderation"; super._category = "Moderation";
super._roles = [ super._roles = [
process.env.ROLES_MODERATOR! "moderator"
]; ];
} }

View file

@ -12,7 +12,7 @@ export default class Mute extends Command {
super._category = "Moderation"; super._category = "Moderation";
super._roles = [ super._roles = [
process.env.ROLES_MODERATOR! "moderator"
]; ];
} }

View file

@ -18,7 +18,7 @@ export default class Rules extends Command {
super._category = "Admin"; super._category = "Admin";
super._roles = [ super._roles = [
process.env.ROLES_MODERATOR! "administrator"
]; ];
} }

View file

@ -9,6 +9,9 @@ export default class Setup extends Command {
constructor() { constructor() {
super(); super();
super._category = "Administration"; super._category = "Administration";
super._roles = [
"moderator"
]
} }
public override async execute(context: ICommandContext) { public override async execute(context: ICommandContext) {

View file

@ -12,7 +12,7 @@ export default class Unmute extends Command {
super._category = "Moderation"; super._category = "Moderation";
super._roles = [ super._roles = [
process.env.ROLES_MODERATOR! "moderator"
]; ];
} }

View file

@ -11,7 +11,7 @@ export default class Warn extends Command {
super._category = "Moderation"; super._category = "Moderation";
super._roles = [ super._roles = [
process.env.ROLES_MODERATOR! "moderator"
]; ];
} }

View file

@ -0,0 +1,5 @@
export enum CommandResponse {
Ok,
Unauthorised,
ServerNotSetup,
}

View file

@ -1,5 +1,5 @@
export default class DefaultValues { export default class DefaultValues {
public static readonly values: ISettingValue[] = []; public static values: ISettingValue[] = [];
public static GetValue(key: string): string | undefined { public static GetValue(key: string): string | undefined {
this.SetValues(); this.SetValues();
@ -22,6 +22,7 @@ export default class DefaultValues {
// Role (Command) // Role (Command)
this.values.push({ Key: "role.assignable", Value: "" }); this.values.push({ Key: "role.assignable", Value: "" });
this.values.push({ Key: "role.moderator", Value: "Moderator" }); this.values.push({ Key: "role.moderator", Value: "Moderator" });
this.values.push({ Key: "role.administrator", Value: "Administrator"});
this.values.push({ Key: "role.muted", Value: "Muted" }); this.values.push({ Key: "role.muted", Value: "Muted" });
// Rules (Command) // Rules (Command)

View file

@ -1,5 +1,21 @@
import { CommandResponse } from "./CommandResponse";
export default class ErrorMessages { export default class ErrorMessages {
public static readonly InsufficientBotPermissions = "Unable to do this action, am I missing permissions?"; public static readonly InsufficientBotPermissions = "Unable to do this action, am I missing permissions?";
public static readonly ChannelNotFound = "Unable to find channel"; public static readonly ChannelNotFound = "Unable to find channel";
public static readonly RoleNotFound = "Unable to find role"; public static readonly RoleNotFound = "Unable to find role";
public static readonly UserUnauthorised = "You are not authorised to use this command";
public static readonly ServerNotSetup = "This server hasn't been setup yet, please run the setup command";
public static GetErrorMessage(response: CommandResponse): string {
switch (response) {
case CommandResponse.Unauthorised:
return this.UserUnauthorised;
case CommandResponse.ServerNotSetup:
return this.ServerNotSetup;
default:
return "";
}
}
} }

View file

@ -1,3 +1,4 @@
import { CommandResponse } from "../constants/CommandResponse";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
export class Command { export class Command {
@ -9,6 +10,14 @@ export class Command {
this._roles = []; this._roles = [];
} }
public precheck(context: ICommandContext): CommandResponse {
return CommandResponse.Ok;
}
public async precheckAsync(context: ICommandContext): Promise<CommandResponse> {
return CommandResponse.Ok;
}
public execute(context: ICommandContext) { public execute(context: ICommandContext) {
} }