Feature/48 database (#114)

* Add database and default values

* Add ability to save a setting to the database

* Get commands and events to use database

* Setup and config command

* Update commands to check roles per server

* Different rules per server

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Different prefix per server

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Add verification system

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Disabled commands per server

* Add devmode for default prefix

* Update embeds

* Fix broken tests
This commit is contained in:
Vylpes 2022-03-29 18:19:54 +01:00 committed by GitHub
parent c8edd1b4c5
commit 6a00c49ef3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 1816 additions and 373 deletions

View file

@ -0,0 +1,50 @@
import { getConnection } from "typeorm";
import DefaultValues from "../constants/DefaultValues";
import Server from "../entity/Server";
import Setting from "../entity/Setting";
export default class SettingsHelper {
public static async GetSetting(key: string, serverId: string): Promise<string | undefined> {
const server = await Server.FetchOneById(Server, serverId, [
"Settings"
]);
if (!server) {
return DefaultValues.GetValue(key);
}
const setting = server.Settings.filter(x => x.Key == key)[0];
if (!setting) {
return DefaultValues.GetValue(key);
}
return setting.Value;
}
public static async SetSetting(key: string, serverId: string, value: string): Promise<void> {
const server = await Server.FetchOneById(Server, serverId, [
"Settings"
]);
if (!server) {
return;
}
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);
}
}
}

View file

@ -12,4 +12,17 @@ export default class StringTools {
return result.join(" ");
}
public static RandomString(length: number) {
let result = "";
const characters = 'abcdefghkmnpqrstuvwxyz23456789';
const charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
}

View file

@ -7,7 +7,7 @@ export default class ErrorEmbed extends MessageEmbed {
constructor(context: ICommandContext, message: String) {
super();
super.setColor(process.env.EMBED_COLOUR_ERROR!);
super.setColor(0xd52803);
super.setDescription(message);
this.context = context;

View file

@ -1,4 +1,6 @@
import { MessageEmbed, TextChannel, User, Guild } from "discord.js";
import { ICommandContext } from "../../contracts/ICommandContext";
import SettingsHelper from "../SettingsHelper";
export default class EventEmbed extends MessageEmbed {
public guild: Guild;
@ -6,7 +8,7 @@ export default class EventEmbed extends MessageEmbed {
constructor(guild: Guild, title: string) {
super();
super.setColor(process.env.EMBED_COLOUR!);
super.setColor(0x3050ba);
super.setTitle(title);
this.guild = guild;
@ -38,15 +40,33 @@ export default class EventEmbed extends MessageEmbed {
channel.send(this);
}
public SendToMessageLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MESSAGE!)
public async SendToMessageLogsChannel() {
const channelName = await SettingsHelper.GetSetting("channels.logs.message", this.guild.id);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
public SendToMemberLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MEMBER!)
public async SendToMemberLogsChannel() {
const channelName = await SettingsHelper.GetSetting("channels.logs.member", this.guild.id);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
public SendToModLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MOD!)
public async SendToModLogsChannel() {
const channelName = await SettingsHelper.GetSetting("channels.logs.mod", this.guild.id);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
}

View file

@ -1,6 +1,7 @@
import { MessageEmbed, TextChannel, User } from "discord.js";
import ErrorMessages from "../../constants/ErrorMessages";
import { ICommandContext } from "../../contracts/ICommandContext";
import SettingsHelper from "../SettingsHelper";
import ErrorEmbed from "./ErrorEmbed";
export default class LogEmbed extends MessageEmbed {
@ -9,7 +10,7 @@ export default class LogEmbed extends MessageEmbed {
constructor(context: ICommandContext, title: string) {
super();
super.setColor(process.env.EMBED_COLOUR!);
super.setColor(0x3050ba);
super.setTitle(title);
this.context = context;
@ -46,15 +47,33 @@ export default class LogEmbed extends MessageEmbed {
channel.send(this);
}
public SendToMessageLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MESSAGE!)
public async SendToMessageLogsChannel() {
const channelName = await SettingsHelper.GetSetting("channels.logs.message", this.context.message.guild?.id!);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
public SendToMemberLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MEMBER!)
public async SendToMemberLogsChannel() {
const channelName = await SettingsHelper.GetSetting("channels.logs.member", this.context.message.guild?.id!);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
public SendToModLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MOD!)
public async SendToModLogsChannel() {
const channelName = await SettingsHelper.GetSetting("channels.logs.mod", this.context.message.guild?.id!);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
}

View file

@ -7,7 +7,7 @@ export default class PublicEmbed extends MessageEmbed {
constructor(context: ICommandContext, title: string, description: string) {
super();
super.setColor(process.env.EMBED_COLOUR!);
super.setColor(0x3050ba);
super.setTitle(title);
super.setDescription(description);