Setup and config command
This commit is contained in:
parent
6ca08bee78
commit
d111363c87
12 changed files with 253 additions and 39 deletions
118
src/commands/config.ts
Normal file
118
src/commands/config.ts
Normal file
|
@ -0,0 +1,118 @@
|
|||
import { Guild } from "discord.js";
|
||||
import { readFileSync } from "fs";
|
||||
import DefaultValues from "../constants/DefaultValues";
|
||||
import { ICommandContext } from "../contracts/ICommandContext";
|
||||
import ICommandReturnContext from "../contracts/ICommandReturnContext";
|
||||
import Server from "../entity/Server";
|
||||
import Setting from "../entity/Setting";
|
||||
import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
|
||||
import PublicEmbed from "../helpers/embeds/PublicEmbed";
|
||||
import { Command } from "../type/command";
|
||||
|
||||
export default class Config extends Command {
|
||||
constructor() {
|
||||
super();
|
||||
super._category = "Administration";
|
||||
}
|
||||
|
||||
public override async execute(context: ICommandContext) {
|
||||
if (!context.message.guild) {
|
||||
return;
|
||||
}
|
||||
|
||||
const server = await Server.FetchOneById<Server>(Server, context.message.guild?.id, [
|
||||
"Settings",
|
||||
]);
|
||||
|
||||
if (!server) {
|
||||
const embed = new ErrorEmbed(context, "This server hasn't been setup yet, please run the setup command");
|
||||
embed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
const key = context.args[0];
|
||||
const action = context.args[1];
|
||||
const value = context.args.splice(2).join(" ");
|
||||
|
||||
if (!key) {
|
||||
this.SendHelpText(context);
|
||||
} else if (!action) {
|
||||
this.GetValue(context, server, key);
|
||||
} else {
|
||||
switch(action) {
|
||||
case 'reset':
|
||||
this.ResetValue(context, server, key);
|
||||
break;
|
||||
case 'set':
|
||||
if (!value) {
|
||||
const errorEmbed = new ErrorEmbed(context, "Value is required when setting");
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
this.SetValue(context, server, key, value);
|
||||
break;
|
||||
default:
|
||||
const errorEmbed = new ErrorEmbed(context, "Action must be either set or reset");
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async SendHelpText(context: ICommandContext) {
|
||||
const description = readFileSync(`${process.cwd()}/data/config.txt`).toString();
|
||||
|
||||
const embed = new PublicEmbed(context, "Config", description);
|
||||
|
||||
embed.SendToCurrentChannel();
|
||||
}
|
||||
|
||||
private async GetValue(context: ICommandContext, server: Server, key: string) {
|
||||
const setting = server.Settings.filter(x => x.Key == key)[0];
|
||||
|
||||
if (setting) {
|
||||
const embed = new PublicEmbed(context, "", `${key}: ${setting.Value}`);
|
||||
embed.SendToCurrentChannel();
|
||||
} else {
|
||||
const embed = new PublicEmbed(context, "", `${key}: ${DefaultValues.GetValue(key)} <DEFAULT>`);
|
||||
embed.SendToCurrentChannel();
|
||||
}
|
||||
}
|
||||
|
||||
private async ResetValue(context: ICommandContext, server: Server, key: string) {
|
||||
const setting = server.Settings.filter(x => x.Key == key)[0];
|
||||
|
||||
if (!setting) {
|
||||
const embed = new PublicEmbed(context, "", "Setting has been reset");
|
||||
embed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
await Setting.Remove(Setting, setting);
|
||||
|
||||
const embed = new PublicEmbed(context, "", "Setting has been reset");
|
||||
embed.SendToCurrentChannel();
|
||||
}
|
||||
|
||||
private async SetValue(context: ICommandContext, server: Server, key: string, value: string) {
|
||||
const setting = server.Settings.filter(x => x.Key == key)[0];
|
||||
|
||||
if (setting) {
|
||||
setting.UpdateBasicDetails(key, value);
|
||||
|
||||
await setting.Save(Setting, setting);
|
||||
} else {
|
||||
const newSetting = new Setting(key, value);
|
||||
|
||||
await newSetting.Save(Setting, newSetting);
|
||||
|
||||
server.AddSettingToServer(newSetting);
|
||||
|
||||
await server.Save(Server, server);
|
||||
}
|
||||
|
||||
const embed = new PublicEmbed(context, "", "Setting has been set");
|
||||
embed.SendToCurrentChannel();
|
||||
}
|
||||
}
|
34
src/commands/setup.ts
Normal file
34
src/commands/setup.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { ICommandContext } from "../contracts/ICommandContext";
|
||||
import ICommandReturnContext from "../contracts/ICommandReturnContext";
|
||||
import Server from "../entity/Server";
|
||||
import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
|
||||
import PublicEmbed from "../helpers/embeds/PublicEmbed";
|
||||
import { Command } from "../type/command";
|
||||
|
||||
export default class Setup extends Command {
|
||||
constructor() {
|
||||
super();
|
||||
super._category = "Administration";
|
||||
}
|
||||
|
||||
public override async execute(context: ICommandContext) {
|
||||
if (!context.message.guild) {
|
||||
return;
|
||||
}
|
||||
|
||||
const server = await Server.FetchOneById(Server, context.message.guild?.id);
|
||||
|
||||
if (server) {
|
||||
const embed = new ErrorEmbed(context, "This server has already been setup, please configure using the config command");
|
||||
embed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
const newServer = new Server(context.message.guild?.id);
|
||||
|
||||
await newServer.Save(Server, newServer);
|
||||
|
||||
const embed = new PublicEmbed(context, "Success", "Please configure using the config command");
|
||||
embed.SendToCurrentChannel();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue