Get commands and events to use database

This commit is contained in:
Ethan Lane 2022-03-03 19:45:33 +00:00
parent 8b2da347b1
commit 6ca08bee78
Signed by: Vylpes
GPG key ID: EED233CC06D12504
15 changed files with 119 additions and 56 deletions

View file

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

View file

@ -72,7 +72,7 @@ export default class Kick extends Command {
await targetMember.kick(`Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`); await targetMember.kick(`Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`);
logEmbed.SendToModLogsChannel(); await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel(); publicEmbed.SendToCurrentChannel();
return { 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*"}`); await targetMember.roles.add(mutedRole, `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`);
logEmbed.SendToModLogsChannel(); await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel(); publicEmbed.SendToCurrentChannel();
return { 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*"}`); await targetMember.roles.remove(mutedRole, `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`);
logEmbed.SendToModLogsChannel(); await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel(); publicEmbed.SendToCurrentChannel();
return { 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(); const user = context.message.mentions.users.first();
if (!user) { if (!user) {
@ -60,7 +60,7 @@ export default class Warn extends Command {
const publicEmbed = new PublicEmbed(context, "", `${user} has been warned`); const publicEmbed = new PublicEmbed(context, "", `${user} has been warned`);
publicEmbed.AddReason(reason); publicEmbed.AddReason(reason);
logEmbed.SendToModLogsChannel(); await logEmbed.SendToModLogsChannel();
publicEmbed.SendToCurrentChannel(); publicEmbed.SendToCurrentChannel();
return { return {

View file

@ -15,17 +15,6 @@ export default class DefaultValues {
private static SetValues() { private static SetValues() {
if (this.values.length == 0) { 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 // Commands
this.values.push({ Key: "commands.disabled", Value: "" }); this.values.push({ Key: "commands.disabled", Value: "" });
this.values.push({ Key: "commands.disabled.message", Value: "This command is disabled." }); 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 { Column, Entity, getConnection, ManyToOne } from "typeorm";
import DefaultValues from "../constants/DefaultValues";
import BaseEntity from "../contracts/BaseEntity"; import BaseEntity from "../contracts/BaseEntity";
import Server from "./Server";
@Entity() @Entity()
export default class Setting extends BaseEntity { export default class Setting extends BaseEntity {
@ -17,6 +17,9 @@ export default class Setting extends BaseEntity {
@Column() @Column()
Value: string; Value: string;
@ManyToOne(() => Server, x => x.Settings)
Server: Server;
public UpdateBasicDetails(key: string, value: string) { public UpdateBasicDetails(key: string, value: string) {
this.Key = key; this.Key = key;
this.Value = value; this.Value = value;

View file

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

View file

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

View file

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

View file

@ -1,28 +1,37 @@
import { getConnection } from "typeorm"; import { getConnection } from "typeorm";
import DefaultValues from "../constants/DefaultValues"; import DefaultValues from "../constants/DefaultValues";
import Server from "../entity/Server";
import Setting from "../entity/Setting"; import Setting from "../entity/Setting";
export default class SettingsHelper { export default class SettingsHelper {
public static async GetSetting(key: string): Promise<string | undefined> { public static async GetSetting(key: string, serverId: string): Promise<string | undefined> {
const connection = getConnection(); const server = await Server.FetchOneById(Server, serverId, [
"Settings"
]);
const repository = connection.getRepository(Setting); if (!server) {
const single = await repository.findOne({ Key: key });
if (!single) {
return DefaultValues.GetValue(key); return DefaultValues.GetValue(key);
} }
return single.Value; const setting = server.Settings.filter(x => x.Key == key)[0];
if (!setting) {
return DefaultValues.GetValue(key);
} }
public static async SetSetting(key: string, value: string): Promise<void> { return setting.Value;
const connection = getConnection(); }
const repository = connection.getRepository(Setting); public static async SetSetting(key: string, serverId: string, value: string): Promise<void> {
const server = await Server.FetchOneById(Server, serverId, [
"Settings"
]);
const setting = await repository.findOne({ Key: key }); if (!server) {
return;
}
const setting = server.Settings.filter(x => x.Key == key)[0];
if (setting) { if (setting) {
setting.UpdateBasicDetails(key, value); setting.UpdateBasicDetails(key, value);
@ -32,6 +41,10 @@ export default class SettingsHelper {
const newSetting = new Setting(key, value); const newSetting = new Setting(key, value);
await newSetting.Save(Setting, newSetting); 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 { MessageEmbed, TextChannel, User, Guild } from "discord.js";
import { ICommandContext } from "../../contracts/ICommandContext";
import SettingsHelper from "../SettingsHelper";
export default class EventEmbed extends MessageEmbed { export default class EventEmbed extends MessageEmbed {
public guild: Guild; public guild: Guild;
@ -38,15 +40,33 @@ export default class EventEmbed extends MessageEmbed {
channel.send(this); channel.send(this);
} }
public SendToMessageLogsChannel() { public async SendToMessageLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MESSAGE!) const channelName = await SettingsHelper.GetSetting("channels.logs.message", this.guild.id);
if (!channelName) {
return;
} }
public SendToMemberLogsChannel() { this.SendToChannel(channelName);
this.SendToChannel(process.env.CHANNELS_LOGS_MEMBER!)
} }
public SendToModLogsChannel() { public async SendToMemberLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MOD!) const channelName = await SettingsHelper.GetSetting("channels.logs.member", this.guild.id);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
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 { MessageEmbed, TextChannel, User } from "discord.js";
import ErrorMessages from "../../constants/ErrorMessages"; import ErrorMessages from "../../constants/ErrorMessages";
import { ICommandContext } from "../../contracts/ICommandContext"; import { ICommandContext } from "../../contracts/ICommandContext";
import SettingsHelper from "../SettingsHelper";
import ErrorEmbed from "./ErrorEmbed"; import ErrorEmbed from "./ErrorEmbed";
export default class LogEmbed extends MessageEmbed { export default class LogEmbed extends MessageEmbed {
@ -46,15 +47,33 @@ export default class LogEmbed extends MessageEmbed {
channel.send(this); channel.send(this);
} }
public SendToMessageLogsChannel() { public async SendToMessageLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MESSAGE!) const channelName = await SettingsHelper.GetSetting("channels.logs.message", this.context.message.guild?.id!);
if (!channelName) {
return;
} }
public SendToMemberLogsChannel() { this.SendToChannel(channelName);
this.SendToChannel(process.env.CHANNELS_LOGS_MEMBER!)
} }
public SendToModLogsChannel() { public async SendToMemberLogsChannel() {
this.SendToChannel(process.env.CHANNELS_LOGS_MOD!) const channelName = await SettingsHelper.GetSetting("channels.logs.member", this.context.message.guild?.id!);
if (!channelName) {
return;
}
this.SendToChannel(channelName);
}
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. */ // "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ // "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. */ // "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. */ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */