Feature/48 database #114

Merged
Vylpes merged 12 commits from feature/48-database into develop 2022-03-29 18:19:54 +01:00
15 changed files with 119 additions and 56 deletions
Showing only changes of commit 6ca08bee78 - Show all commits

View file

@ -69,7 +69,7 @@ export default class Ban extends Command {
await targetMember.ban({ reason: `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}` });
logEmbed.SendToModLogsChannel();
await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel();
return {

View file

@ -72,7 +72,7 @@ export default class Kick extends Command {
await targetMember.kick(`Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`);
logEmbed.SendToModLogsChannel();
await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel();
return {

View file

@ -85,7 +85,7 @@ export default class Mute extends Command {
await targetMember.roles.add(mutedRole, `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`);
logEmbed.SendToModLogsChannel();
await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel();
return {

View file

@ -85,7 +85,7 @@ export default class Unmute extends Command {
await targetMember.roles.remove(mutedRole, `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`);
logEmbed.SendToModLogsChannel();
await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel();
return {

View file

@ -15,7 +15,7 @@ export default class Warn extends Command {
];
}
public override execute(context: ICommandContext): ICommandReturnContext {
public override async execute(context: ICommandContext): Promise<ICommandReturnContext> {
const user = context.message.mentions.users.first();
if (!user) {
@ -60,7 +60,7 @@ export default class Warn extends Command {
const publicEmbed = new PublicEmbed(context, "", `${user} has been warned`);
publicEmbed.AddReason(reason);
logEmbed.SendToModLogsChannel();
await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel();
return {

View file

@ -15,17 +15,6 @@ export default class DefaultValues {
private static SetValues() {
if (this.values.length == 0) {
// Bot
this.values.push({ Key: "bot.prefix", Value: "v!" });
this.values.push({ Key: "bot.version", Value: "3.0" });
this.values.push({ Key: "bot.author", Value: "Vylpes" });
this.values.push({ Key: "bot.date", Value: "28 Feb 2022" });
this.values.push({ Key: "bot.owner", Value: "147392775707426816" });
// Folders
this.values.push({ Key: "folders.commands", Value: "src/commands" });
this.values.push({ Key: "folders.events", Value: "src/events" });
// Commands
this.values.push({ Key: "commands.disabled", Value: "" });
this.values.push({ Key: "commands.disabled.message", Value: "This command is disabled." });

19
src/entity/Server.ts Normal file
View file

@ -0,0 +1,19 @@
import { Column, Entity, getConnection, OneToMany } from "typeorm";
import BaseEntity from "../contracts/BaseEntity";
import Setting from "./Setting";
@Entity()
export default class Server extends BaseEntity {
constructor(serverId: string) {
super();
this.Id = serverId;
}
@OneToMany(() => Setting, x => x.Server)
Settings: Setting[];
public AddSettingToServer(setting: Setting) {
this.Settings.push(setting);
}
}

View file

@ -1,6 +1,6 @@
import { Column, Entity, getConnection } from "typeorm";
import DefaultValues from "../constants/DefaultValues";
import { Column, Entity, getConnection, ManyToOne } from "typeorm";
import BaseEntity from "../contracts/BaseEntity";
import Server from "./Server";
@Entity()
export default class Setting extends BaseEntity {
@ -17,6 +17,9 @@ export default class Setting extends BaseEntity {
@Column()
Value: string;
@ManyToOne(() => Server, x => x.Settings)
Server: Server;
public UpdateBasicDetails(key: string, value: string) {
this.Key = key;
this.Value = value;

View file

@ -9,37 +9,37 @@ export default class MemberEvents extends Event {
super();
}
public override guildMemberAdd(member: GuildMember): IEventReturnContext {
public override async guildMemberAdd(member: GuildMember): Promise<IEventReturnContext> {
const embed = new EventEmbed(member.guild, "Member Joined");
embed.AddUser("User", member.user, true);
embed.addField("Created", member.user.createdAt);
embed.setFooter(`Id: ${member.user.id}`);
embed.SendToMemberLogsChannel();
await embed.SendToMemberLogsChannel();
return {
embeds: [embed]
};
}
public override guildMemberRemove(member: GuildMember): IEventReturnContext {
public override async guildMemberRemove(member: GuildMember): Promise<IEventReturnContext> {
const embed = new EventEmbed(member.guild, "Member Left");
embed.AddUser("User", member.user, true);
embed.addField("Joined", member.joinedAt);
embed.setFooter(`Id: ${member.user.id}`);
embed.SendToMemberLogsChannel();
await embed.SendToMemberLogsChannel();
return {
embeds: [embed]
};
}
public override guildMemberUpdate(oldMember: GuildMember, newMember: GuildMember): IEventReturnContext {
public override async guildMemberUpdate(oldMember: GuildMember, newMember: GuildMember): Promise<IEventReturnContext> {
const handler = new GuildMemberUpdate(oldMember, newMember);
if (oldMember.nickname != newMember.nickname) { // Nickname change
handler.NicknameChanged();
await handler.NicknameChanged();
}
return {

View file

@ -11,7 +11,7 @@ export default class GuildMemberUpdate {
this.newMember = newMember;
}
public NicknameChanged(): IEventReturnContext {
public async NicknameChanged(): Promise<IEventReturnContext> {
const oldNickname = this.oldMember.nickname || "*none*";
const newNickname = this.newMember.nickname || "*none*";
@ -21,7 +21,7 @@ export default class GuildMemberUpdate {
embed.addField("After", newNickname, true);
embed.setFooter(`Id: ${this.newMember.user.id}`);
embed.SendToMemberLogsChannel();
await embed.SendToMemberLogsChannel();
return {
embeds: [embed]

View file

@ -8,7 +8,7 @@ export default class MessageEvents extends Event {
super();
}
public override messageDelete(message: Message): IEventReturnContext {
public override async messageDelete(message: Message): Promise<IEventReturnContext> {
if (!message.guild) {
return {
embeds: []
@ -30,14 +30,14 @@ export default class MessageEvents extends Event {
embed.addField("Attachments", `\`\`\`${message.attachments.map(x => x.url).join("\n")}\`\`\``);
}
embed.SendToMessageLogsChannel();
await embed.SendToMessageLogsChannel();
return {
embeds: [embed]
};
}
public override messageUpdate(oldMessage: Message, newMessage: Message): IEventReturnContext {
public override async messageUpdate(oldMessage: Message, newMessage: Message): Promise<IEventReturnContext> {
if (!newMessage.guild){
return {
embeds: []
@ -62,7 +62,7 @@ export default class MessageEvents extends Event {
embed.addField("Before", `\`\`\`${oldMessage.content || "*none*"}\`\`\``);
embed.addField("After", `\`\`\`${newMessage.content || "*none*"}\`\`\``);
embed.SendToMessageLogsChannel();
await embed.SendToMessageLogsChannel();
return {
embeds: [embed]

View file

@ -1,28 +1,37 @@
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): Promise<string | undefined> {
const connection = getConnection();
public static async GetSetting(key: string, serverId: string): Promise<string | undefined> {
const server = await Server.FetchOneById(Server, serverId, [
"Settings"
]);
const repository = connection.getRepository(Setting);
const single = await repository.findOne({ Key: key });
if (!single) {
if (!server) {
return DefaultValues.GetValue(key);
}
return single.Value;
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, value: string): Promise<void> {
const connection = getConnection();
public static async SetSetting(key: string, serverId: string, value: string): Promise<void> {
const server = await Server.FetchOneById(Server, serverId, [
"Settings"
]);
const repository = connection.getRepository(Setting);
if (!server) {
return;
}
const setting = await repository.findOne({ Key: key });
const setting = server.Settings.filter(x => x.Key == key)[0];
if (setting) {
setting.UpdateBasicDetails(key, value);
@ -32,6 +41,10 @@ export default class SettingsHelper {
const newSetting = new Setting(key, value);
await newSetting.Save(Setting, newSetting);
server.AddSettingToServer(newSetting);
await server.Save(Server, server);
}
}
}

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;
@ -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 {
@ -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

@ -30,7 +30,7 @@
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */